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

2012/6/20

I came up with a simple spawning code that does what i need but...

Treena.Lake Treena.Lake

2012/6/20

#
I have a code that spawns coins for a game im working on. It works fine until i remove a coin, then the terminal window shows up because my actor is not in the world. I dont know what to do :/
if (random <= 4)
        {
            addObject(new Coin(), 800, Greenfoot.getRandomNumber(400) + 1);
        }
Its wrote in the act() method of my world. Any advice on how to modify this code so it doesn't crash my game when i remove a coin would be great!
danpost danpost

2012/6/20

#
I doubt the error is with the code you posted. Help us out, please -- copy/paste the error message in a reply.
steved steved

2012/6/20

#
If your remove object method is in the coin its self put it into something else or make it the last thing in your act method. For example you could do all of its methods and then add a variable called dead (or anything else). Then when it is eaten dead is changed to true or 1 or anything you want. After that at the end of the act method you put an if statement that says if it has been eaten (if dead equals true) to remove this object. Hope this helps (this will fix the actor not being in world but if you have different errors this might only fix one)
davmac davmac

2012/6/20

#
java.man java.man

2012/6/20

#
Maybe you should solve your problem changing "if" for "else" in second statement. If the first is "true" the second will not be carried out. I guess this is more elegant solution for your problem.
Treena.Lake Treena.Lake

2012/6/20

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:656) at greenfoot.Actor.getOneIntersectingObject(Actor.java:905) at Coin.checkCollision(Coin.java:37) at Coin.act(Coin.java:22) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) That is the error that shows up.
davmac davmac

2012/6/20

#
The stack trace shows where the error is happening: at Coin.checkCollision(Coin.java:37) I.e. it's in the "Coin" class ("checkCollision" method), line 37. That's the code that you should look at.
You need to login to post a reply.