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

2011/12/5

help on add object loop!

easymal easymal

2011/12/5

#
i want a loop that creates ONLY a car when it hits the middle of my world i want it to do this three times and only three times so i don't have a car pileup but when i compile and run the world it add 3 cars at once and piles up please help public void addMore() { if (atMiddle()) { addloop(); } } public void addloop() { for(int i=0; i<3; i++) { rand1 = Greenfoot.getRandomNumber(10); getWorld().addObject(new Car1(rand1),75,345); } } public boolean atMiddle() { if ((618==this.getX())) { return true; } else return false; } what am i doing wrong?
Andres Andres

2011/12/6

#
You are calling the addLoop() method every time the actor is in the middle of the screen (atMiddle() = true). The addLoop() method has a for() loop which iterates 3 times. So, each act() cycle, addLoop() is executed, and the loop is executed three times, which is not what you want. I won't tell you exactly how to do it, but try using booleans.
easymal easymal

2011/12/6

#
ok so i tried this and now when it hits the middle everything stops public void addMore() { if (atMiddle()) { addloop(); } } public void addloop() { boolean n= atMiddle() ; boolean tru= true; while (n==tru) { rand1 = Greenfoot.getRandomNumber(10); getWorld().addObject(new Car1(rand1),75,345); } }
easymal easymal

2011/12/6

#
ok now i just called add loop(); in the act method now I'm getting an error :Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space
easymal easymal

2011/12/6

#
nvm i fixed the error i tried this as well but I'm still getting a car pile up public void addloop() { boolean n= atMiddle(); boolean tru= true; int i=0; while (n==tru) { rand1 = Greenfoot.getRandomNumber(10); getWorld().addObject(new Car1(rand1),75,345); i=i+1; tru= (i<1); } }
davmac davmac

2011/12/6

#
If you want the cars to appear at different times, you need to add them separately during separate invocations of the act() method. That is: - once the middle position is reached, add one car and return; - the following few times act() is called, do nothing, to allow the first car to move away - then add another car - then wait a few more act() cycles - then add the final car For this to work you need to introduce state into your actor - that is, instance variables which keep track of what is happening. You could for instance have a counters which keeps track of how many cars need to be added, and another for how long to wait (how many cycles) before adding the next car. private int carsToAdd = 0; // instance variable - not inside a method! private int timeToWait = 0; // also an instance variable! public void addMore() { if (carsToAdd > 0) { if (timeToWait > 0) { timeToWait--; } else { rand1 = Greenfoot.getRandomNumber(10); getWorld().addObject(new Car1(rand1),75,345); carsToAdd--; timeToWait = 20; } } else if (atMiddle()) { carsToAdd = 3; timeToWait = 0; // add the first car immediately } }
You need to login to post a reply.