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

2021/2/15

adding an object from the code

gorilA876 gorilA876

2021/2/15

#
when my score gets to 500 I wants to add another missile but my code doesn't work for some reason
 public void add()
    {
        thisScore.newMissile = thisScore.score - 500;
        if (thisScore.newMissile == 500)
        {
            Actor missile;
            World world = getWorld();
            world.addObject(this,5,5);
            thisScore.newMissile = 0;
        }
    }
is my current code
RcCookie RcCookie

2021/2/15

#
What you do add the object itself to the world it is already in, so you do nothing. You first need to create a new instance of Missile and then add that instance to the world:
// Replace lines 6 - 8
Actor missile = new Missile(); // Instantiate a new instance of Missile
World world = getWorld(); // Get the current world
world.addObject(missile, 5, 5); // Add that new instance into the world
Or shorter:
getWorld().addObject(new Missile(), 5, 5);
You need to login to post a reply.