This site requires JavaScript, please enable it in your browser!
Greenfoot back
Notted
Notted wrote ...

2019/5/1

Change an images color.

1
2
3
danpost danpost

2019/5/2

#
I cannot tell what you have where. I can tell that the Space class act method is shown last and it is quite obvious that shown first is the Invader class constructor; but, what Actor subclass did you put the moveRow method in
Notted Notted

2019/5/2

#
Do you need more context? Space:
public class Space extends World
{
    Row[] rows = new Row[8];
    int timer, direction, level = 1;
    boolean drop;

    public Space()
    {
        super(450, 500, 1);
        prepare();
    }
    
    private void prepare()
    {
        removeObjects(getObjects(null));
        for (int j=0; j<3+level; j++)
        {
            rows[j] = new Row();
            addObject(rows[j], 225, 25+j*50);
            for (int i=0; i<3; i++) addObject(new Invader(Color.RED, 10, 5), 50+i*50, 20+j*50);
        }
        addObject(new Player(Color.WHITE), 30, 475);
        timer = 0;
        direction = 1;
        drop = false;
    }
    
    public void act()
    {
        if (timer == 0 && drop)
        {
            for (int i=0; i<3+level; i++)
            {
                rows[i].dropRow();
                drop = false;
            }
            direction = -direction;
        }
        if (timer%3 == 0 && timer < 3*(3+level)) drop = rows[timer/3].moveRow(direction) || drop;
        timer = (timer+1)%32;
        if (getObjects(Invader.class).isEmpty())
        {
            if (level < 5) level++;
            prepare();
        }
        if (getObjects(Player.class).isEmpty()) prepare();
    }
}
Invader:
public class Invader extends Actor
{
    public Invader(Color invaderColor, int invaderX, int invaderY)
    {
        GreenfootImage image = new GreenfootImage(invaderX, invaderY);
        image.setColor(invaderColor);
        image.fill();
        setImage(image);
    }
    
    public boolean moveInvader(int dir)
    {
        setLocation(getX()+dir*5, getY());
        return getX() < 20 || getX() > getWorld().getWidth()-20;
    }
    
    public void act()
    {
        Actor player = getOneIntersectingObject(Player.class);
        if (player != null) getWorld().removeObject(player);
        if (Greenfoot.getRandomNumber(80) == 0) shoot();
    }
    
    private void shoot()
    {
        int dist = getWorld().getHeight()-getY();
        Feeler feeler = new Feeler();
        feeler.setImage(new GreenfootImage(1, dist));
        getWorld().addObject(feeler, getX(), getY()+dist/2+30);
        int count = feeler.invaderCount;
        getWorld().removeObject(feeler);
        if (count == 0)
        {
            Bullet bullet = new Bullet(Color.RED);
            bullet.setRotation(90);
            getWorld().addObject(bullet, getX(), getY());
            bullet.move(15);
        }        
    }
    
    public class Feeler extends Actor
    {
        public int invaderCount;
        
        public void addedToWorld(World w)
        {
            invaderCount = getIntersectingObjects(Invader.class).size();
        }
    }
Player:
public class Player extends Actor
{
    int shotTimer;
    
    public Player(Color playerColor)
    {
        GreenfootImage image = new GreenfootImage(29, 15);
        image.setColor(playerColor);
        int[] xs = {  0,  3, 12, 14, 16, 25, 28 };
        int[] ys = { 14,  4,  4,  0,  4,  4, 14 };
        image.fillPolygon(xs, ys, 7);
        setImage(image);
    }

    public void act()
    {
        runShotTimer();
        int dx = 0;
        if (Greenfoot.isKeyDown("left")) dx--;
        if (Greenfoot.isKeyDown("right")) dx++;
        if (dx == 0 || getX()+dx < 15 || getX()+dx > getWorld().getWidth()-16) return;
        move(dx);
    }
    
    private void runShotTimer()
    {
        if (shotTimer > 0) shotTimer--;
        if (shotTimer > 0) return;
        if (Greenfoot.isKeyDown("space"))
        {
            Bullet bullet = new Bullet(Color.WHITE);
            bullet.setRotation(270);
            getWorld().addObject(bullet, getX(), getY());
            bullet.move(10);
            shotTimer = 60;
        }
    }
}
Row:
public class Row extends Actor
{
    public Row()
    {
        setImage(new GreenfootImage(450, 1));
    }
    
    public boolean moveRow(int dir)
    {
        boolean changeDir = false;
        for (Object obj : getIntersectingObjects(Invader.class))
        {
            Invader invader = (Invader) obj;
            changeDir = invader.moveInvader(dir) || changeDir;
        }
        return changeDir;
    }
    
    public void dropRow()
    {
        for (Object obj : getIntersectingObjects(Invader.class))
        {
            Actor invader = (Invader) obj;
            invader.setLocation(invader.getX(), invader.getY()+10);
        }
        setLocation(getX(), getY()+10);
    }
}
danpost danpost

2019/5/2

#
It would help if you placed your invaders on their rows. Apparently, you adjusted the vertical starting position of where invaders start; but you did not adjust the vertical starting position of the rows.
Notted Notted

2019/5/2

#
So set the Y of the rows equal to the invaders?
danpost danpost

2019/5/2

#
Notted wrote...
So set the Y of the rows equal to the invaders?
Yes, change line 19 so that the y-locations are equivalent (the last expression in the line should look exactly like that of line 20.
Notted Notted

2019/5/2

#
danpost wrote...
Notted wrote...
So set the Y of the rows equal to the invaders?
Yes, change line 19 so that the y-locations are equivalent (the last expression in the line should look exactly like that of line 20.
Should I use some sort of reference for the invaders Y position in order to make it set equal to that pos?
danpost danpost

2019/5/2

#
Notted wrote...
Should I use some sort of reference for the invaders Y position in order to make it set equal to that pos?
No. Just change the end of line 19 to match that of line 20 -- use '20+j*50'.
Notted Notted

2019/5/3

#
I guess I was testing the Y of the rows and didn't change that.
Notted Notted

2019/5/3

#
Ok. What I want now is to create invaders with different attributes, like HP and speed. These will be placed on individual rows, with bottom ones being of the lowest type. How could I do this?
danpost danpost

2019/5/3

#
Notted wrote...
I want now is to create invaders with different attributes, like HP and speed. These will be placed on individual rows, with bottom ones being of the lowest type. How could I do this?
You will need to use a block of code for the for loop at line 20 in your Space class. In it, you will need to use two lines of code for the current one line you have the for loop executing. The first line will create and place a reference to the new invader in a local variable; the second to add that same (already created) invader into the world. You can then continue to set the states for that invader (utilize the loop counter to determine which row it is in).
Notted Notted

2019/5/3

#
I'm not sure what to do exactly.
danpost danpost

2019/5/3

#
Notted wrote...
I'm not sure what to do exactly.
Okay, maybe you can get away with just passing the loop counter to the invaders you create by adding another parameter. That way the invader can then set its own state depending on that value.
Notted Notted

2019/5/7

#
My way will probably involve an int. Many games choose enemy types randomly in that way, so I'll do the same.
Notted Notted

2019/5/7

#
This works towards getting enemy types to spawn.
public Invader(int invaderType)
    {
        int invaderHP = 0;
        if (invaderType == 1)
        {
        GreenfootImage image = new GreenfootImage(5, 10);
        image.setColor(Color.RED);
        image.fill();
        setImage(image);
        invaderHP = 1;
        }
        else if (invaderType == 2)
        {
        GreenfootImage image = new GreenfootImage(7, 12);
        image.setColor(Color.BLUE);
        image.fill();
        setImage(image);
        invaderHP = 2;
        }
        else if (invaderType == 3)
        {
        GreenfootImage image = new GreenfootImage(9, 14);
        image.setColor(Color.ORANGE);
        image.fill();
        setImage(image);
        invaderHP = 3;
        }
        else if (invaderType == 4)
        {
        GreenfootImage image = new GreenfootImage(11, 16);
        image.setColor(Color.YELLOW);
        image.fill();
        setImage(image);
        invaderHP = 4;
        }
    }
Now we need to have some sort of way to implement health into the game. One of them would be to use this method:
if (getRotation() == 270)
        {
            Actor invader = getOneIntersectingObject(Invader.class);
            if (invader != null)
            {
                world.removeObject(invader);
                world.removeObject(this);
            }
            else if (getY() == 0) world.removeObject(this);
        }
in the bullet Actor to it.
Notted Notted

2019/5/7

#
We may also need to figure out how to implement walls.
There are more replies on the next page.
1
2
3