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

2019/5/21

Unable to add object to MyWorld

Joyfu1 Joyfu1

2019/5/21

#
Hello, I am working on a simple game. I am unable to add Level 2 Aliens to myWorld after the previous aliens have been destroyed. I have created a counter which works for the stars but not for the SmallAliens. How may I fix this issue? Thank you!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ScoreBoard here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ScoreBoard
{
    private MyWorld world;

    private int nextStarXCoord = 50;
    public static int alien_counter = 10;

    public ScoreBoard(MyWorld world)
    {
        this.world = world;
    }

    public void addStar()
    {
        if (alien_counter > 0) {
            world.addObject(new Star(), nextStarXCoord, 45);
            nextStarXCoord += 50;
            alien_counter--; 
        }
    }
    private void AddSmallAlien() {
        if (alien_counter == 6) {
            SmallAlien smallalien = new SmallAlien();
            world.addObject(smallalien, 900, 100);

            SmallAlien smallalien2 = new SmallAlien();
            world.addObject(smallalien2, 900, 500);

            SmallAlien smallalien3 = new SmallAlien();
            world.addObject(smallalien3, 100, 500);

            SmallAlien smallalien4 = new SmallAlien();
            world.addObject(smallalien4, 100, 100);
        }
    }
}
Super_Hippo Super_Hippo

2019/5/21

#
The 'addStar' method is probably called from somewhere if it works. The 'AddSmallAlien" method is not called from anywhere. Add a call to the method after line 26.
danpost danpost

2019/5/21

#
What does any of the code in the class have to do with a scoreboard? and why would you not place the codes to add objects into your world in your MyWorld class?
Joyfu1 Joyfu1

2019/5/21

#
danpost wrote...
What does any of the code in the class have to do with a scoreboard? and why would you not place the codes to add objects into your world in your MyWorld class?
If the scoreboard hits a certain number, we would like to add the object. We tried from MyWorld as well but it doesn't add the SmallAlienobject after the scoreboard hits a certain number. However, the AddStar works perfectly fine. Thank you!
You need to login to post a reply.