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

2014/2/21

i tryin to get new train on my world when is the first trein disappers. can anyone help me?

samir samir

2014/2/21

#
public class Trein extends Treins { public void act() { move(); } public void move() { move(5); if (getX() <= 5 || getX() >= getWorld().getWidth() - 5) { getWorld().removeObject(this); createnewTrein(); } } public void createnewTrein() { Trein newTrein; newTrein = new Trein(); World world; world = getWorld(); world.addObject(newTrein, 423, 220); } }
danpost danpost

2014/2/21

#
Once you use 'getWorld().removeObject(this)', 'getWorld()' will start returning a 'null' value. Therefore, when you call 'createnewTrein' afterwards, and it tries to 'addObject' on a 'null' value, you get an error. Call 'createnewTrein' before 'removeObject'.
samir samir

2014/2/21

#
thanks, it seems always too easy when u get the answer,,
samir samir

2014/2/21

#
but now i wanna to get trein stop for a while and then move,, how is that possible?
danpost danpost

2014/2/21

#
Yes, you will need to put a condition of the 'move' call in the act method:
public void act()
{
    if (/* some condition(s) */) move();
}
The condition(s) is/are determined by you; whether by location or intersection with some type of object or maybe by a time control. Cannot help further without more info.
You need to login to post a reply.