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

2012/11/5

Right to Left Movement

DPD4AU DPD4AU

2012/11/5

#
My World Size is 900x600. My object, in this case a zombie is 138 pixels tall. If i want to generate new objects coming out of the bottom right hand side of the screen, then move to the left, how would i go about doing this? It isn't necessarily the movement I am having trouble with but generating new objects randomly in a certain area of the screen.
Gevater_Tod4711 Gevater_Tod4711

2012/11/5

#
If you just want the zombies to move from left to right you just have to use setLocation(getX(), getY()-1); Or a higher number if you want the zombies to be faster.
danpost danpost

2012/11/5

#
DPD4AU wrote...
It isn't necessarily the movement I am having trouble with but generating new objects randomly in a certain area of the screen.
In an 'act' method or a method an 'act' method calls, use the following code parts (values can be adjusted as neccessary and using 'getWidth' and 'getHeight' on the world object is preferable):
// coded as if in the world class 
// random chance to spawn
if (Greenfoot.getRandomNumber(1000) < 6) // 6 in 1000 chance
// defining variables
// these first four define the area of the screen to spawn
int xLo = 700; // getWidth - something   (or 1, if only at right edge)
int xHi = 800; // getWidth()
int yLo = 300; // getHeight() / 2
int yHi = 600; // getHeight()
// random location to spawn
int x = xLo + Greenfoot.getRandomNumber(xHi - xLo);
int y = yLo + Greenfoot.getRandomNumber(yHi - yLo);
// adding zombie
addObject(new Zombie(), x, y);
You need to login to post a reply.