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

2021/3/29

Variable with different values inheritance issue

CreatorMoon CreatorMoon

2021/3/29

#
I'm having a bit of an issue with a Space Invaders game that I have to make for school. All three of my aliens add 50 points to the score, when I want them to all add different values. Can anyone help me with this? My first alien's code (parent):
import greenfoot.*;
public class Alien1 extends Actor
{
    int points = 50;
    int wait;
    int moving = 10;
    int moveTimer = 60;
    int shoot;
    public void act()
    {
        wait++;
        if (isTouching(PlayerBullet.class))
        {
            removeTouching(PlayerBullet.class);
            Greenfoot.playSound("destroy.mp3");
            ScoreCounter counter = getWorld().getObjects(ScoreCounter.class).get(0);
            counter.score = counter.score + points;
            getWorld().removeObject(this);
        }
        if(wait == 20)
        {
            wait = 0;
            movement();
            shoot();
        }
    }
    public void movement()
    {
        moveTimer--;
        if (moveTimer >= 30)
        {
            move(moving);
        }
        if (moveTimer <= 30)
        {
            move(-moving);
        }
        if (moveTimer == -1)
        {
            moveTimer = 61;
        }
        if (moveTimer == 0)
        {
            moving = 0;
        }
        if (moveTimer == 30)
        {
            setLocation(getX(), getY()+25);
        }
        if (moveTimer == 61)
        {
            setLocation(getX(), getY()+25);
            moving = 10;
        }
    }
    public void shoot()
    {
        if (Greenfoot.getRandomNumber(29) == 1)
        {
            getWorld().addObject(new AlienBullet(), getX(), getY()-1);
            Greenfoot.playSound("shoot.mp3");
        }
    }
}
My second alien's code (child):
import greenfoot.*;
public class Alien2 extends Alien1
{
    int points = 25;
}
My third alien's code (child):
import greenfoot.*;
public class Alien3 extends Alien1
{
    int points = 10;
}
My score actor's code:
import greenfoot.*;
public class ScoreCounter extends Actor
{
    int score = 0;
    public void act() 
    {
        setImage(new GreenfootImage("Score: "+score,25,Color.WHITE,Color.BLACK));
    }
}
CreatorMoon CreatorMoon

2021/3/29

#
If it wasn't already obvious, the "points" variable is the one I'm having issues with.
danpost danpost

2021/3/29

#
Remove "int" from line 4 in both the Alien2 and Alien3 class codes above.
CreatorMoon CreatorMoon

2021/3/29

#
When I remove the int, the error "<identifier> expected" shows up. I've also tried using "public Alien2" and "public Alien3" but then it doesn't add to the score. It used to work perfectly fine, which is what confuses me.
danpost danpost

2021/3/29

#
CreatorMoon wrote...
When I remove the int, the error "<identifier> expected" shows up. I've also tried using "public Alien2" and "public Alien3" but then it doesn't add to the score. It used to work perfectly fine, which is what confuses me.
Oh, yeah -- they need to be in constructor blocks. For example:
import greenfoot.*;
public class Alien2 extends Alien1
{
    public Alien2()
    {
        points = 25;
    }
}
CreatorMoon CreatorMoon

2021/3/29

#
Wow, I can't believe I didn't think to do that. It works perfectly now, thank you so much!
You need to login to post a reply.