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

2018/10/18

Having trouble moving invaders left and right

ananyagarg ananyagarg

2018/10/18

#
Hi, I am making space invaders on greenfoot and am having trouble moving the invaders left to right while simultaneously shooting. This is my code in the MyWorld class (to set up the invaders):
Invader[] invaders = new Invader[5];
       for (int i=0; i<5; i++){
           invaders[i] = new Invader();
           addObject (invaders[i], 85+(i*100), 86);
}
       for (int i=0; i<5; i++){
           invaders[i] = new Invader();
           addObject (invaders[i], 85+(i*100), 180);
}
        for (int i=0; i<5; i++){
            invaders[i] = new Invader();
            addObject (invaders[i], 85+(i*100), 274);
        }
I am unsure even where to start in the invaders class to get them all to move to the left and right Thanks for the help!
danpost danpost

2018/10/18

#
ananyagarg wrote...
I am making space invaders on greenfoot and am having trouble moving the invaders left to right while simultaneously shooting. << Code Omitted >> I am unsure even where to start in the invaders class to get them all to move to the left and right
I am unsure as to why you create an array to add invaders into the world. The following would be equivalent to the code given:
for (int i=0; i<5; i++) addObject(new Invader(), 85+i*100, 86);
for (int i=0; i<5; i++) addObject(new Invader(), 85+i*100, 180);
for (int i=0; i<5; i++) addObject(new Invader(), 85+i*100, 274);
or even this:
for (int j=0; j<3; j++) for (int i=0; i<5; i++) addObject(new Invader(), 85+i*100, 86+j*94);
As far as movement and shooting, you may want to check out my Space Invaders Movement Demo scenario. It has one possible way to code it.
ananyagarg ananyagarg

2018/10/18

#
danpost wrote...
I am unsure as to why you create an array to add invaders into the world. The following would be equivalent to the code given: <<Code ommitted>>
Hi! Thank you for the reply. I had a look at your game and I am unsure about a few things. Firstly, what does the % sign in this code do/mean?
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();
        }
Secondly, what does the Feeler class in your invader class represent?
    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();
            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();
        }
    }
Thank you!
danpost danpost

2018/10/18

#
ananyagarg wrote...
what does the % sign in this code do/mean?
The percent symbol is the modulus operator. It takes the value before it and returns the remainder after dividing that value by the value after it. So, line 2 will have the timer increment to 31 and then start back over at zero. Line 1 controls the movement of the rows. Every third act cycle will have the next lower row move if not all rows have moved in the last set of 32 act cycles. The 'timer%3 == 0' part controls the timing between the movement of each row and the 'timer > 3*(3+level)' part ensures that a row is available to move. The 'timer/3' is the index of the row that is to be moved.
what does the Feeler class in your invader class represent?
It is an actor that an invader adds to the world to look for any invaders below it. An invader will only add one of this type actor into the world when it is triggered to fire a bullet. If any invaders are found below it, it will not shoot a bullet. The actor is immediately removed from the world once the count of invaders below it has been acquired -- during the same execution of the act method it was added into the world.
ananyagarg ananyagarg

2018/10/19

#
danpost wrote...
<<Code Omitted>>
In your game, why do your invaders shoot their bullets so fast, and where is the code in your game that determines the speed in which the bullets move? Secondly, I modified your code a bit to fit my game, but now when my invaders move (since I only have 5 in a row), the first 4 columns move but the last column doesn't move, and the other 4 columns stop at the last column. I am not sure how to fix this This is my code in the invader class:
import greenfoot.*;

public class Invader extends Actor
{
    public Invader()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth()-520, image.getHeight()-520);
        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(Shooter.class);
        if (player != null) getWorld().removeObject(player);
        if (Greenfoot.getRandomNumber(200) == 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();
            bullet.setRotation(90);
            getWorld().addObject(bullet, getX(), getY());
            bullet.move(2);
        }        
    }
    
    public class Feeler extends Actor
    {
        public int invaderCount;
        
        public void addedToWorld(World w)
        {
            invaderCount = getIntersectingObjects(Invader.class).size();
        }
    }
}
And this is my code in my Row class:
import greenfoot.*;

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

2018/10/19

#
ananyagarg wrote...
In your game, why do your invaders shoot their bullets so fast, and where is the code in your game that determines the speed in which the bullets move?
The demo was geared toward the movement -- not really anything else. However, the shooting code is in the Invader class. The last line in its act method partly controls when a shot is fired by having each invader shoot randomly, but on an average of once every 200 act cycles. In the shoot method, shooting is further limited to only if the invader is the lowest one in its column. For some unknown reason the speed of the scenario was bumped up. Add the following line to the world constructor after the super call:
Greenfoot.setSpeed(50);
That should slow things down to a more suitable speed.
I modified your code a bit to fit my game, but now when my invaders move (since I only have 5 in a row), the first 4 columns move but the last column doesn't move, and the other 4 columns stop at the last column. I am not sure how to fix this This is my code in the invader class: << Code Omitted >> And this is my code in my Row class: << Code Omitted >>
If your world is a different width than mine, that might have something to do with it. The world also has control over the movement of the invaders.
You need to login to post a reply.