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

2012/11/14

While loops

BradH BradH

2012/11/14

#
Hello this a my Castle class and it spawns BadKnight1 after a set ammount of time, private int KnightSpawn = (8); the problem with this code is that it only spawns one badKnight1, so to fix this problem I need a while loop. I watched the tutorial on loops and I know the code. The thing I have a question about is where to put the loop I have tried multiple places in the code none of which seemed to work. I want the int KnightSpawn get to 0 and Spawn a BadKnight1, but after that I need the value of KnightSpawn to return to 8 so the process can be repeated and spawn another knight and so forth. import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class BadCastle here. * * @author (your name) * @version (a version number or a date) */ public class BadCastle extends BadTroops { private int KnightSpawn = (8); /** * Act - do whatever the BadCastle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { KnightSpawn -- ; if(KnightSpawn == 0) { getWorld().addObject(new BadKnight1(), 758 , 306); } } }
danpost danpost

2012/11/14

#
Right after your 'getWorld().addObject(new BadKnight1(),...);' statement, add the following
KnightSpawn = 8;
which resets the variable, so it can count back down to zero again. Be advised, however, that 8 counts will happen very, very quickly, and you will probably end up with hundreds of BadKnight1 object in a New York second. You might want to start with a number somewhere in the thousands, and adjust it to suit your needs.
BradH BradH

2012/11/14

#
Thanks a lot
You need to login to post a reply.