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

2013/10/21

Getting a method to work

winsticknova winsticknova

2013/10/21

#
I have a method in my world that changes the speed of my actor depending on how many points I have, but I cant get it to work because I need to put the method in my actor first. this is in my world: if(Greenfoot.getRandomNumber(1000) < enemySpawnRate) { Enemy e = new Enemy(); e.setSpeed(enemySpeed); addObject(e, Greenfoot.getRandomNumber(getWidth() - 20)+ 5, -30); scoreboard.addScore(5); } and i need to put setSpeed in my actor. what should the setSpeed method look like?
GreenHouse GreenHouse

2013/10/21

#
// in your actor
public void setSpeed(int speed) {

  // do something here

}
this way?
winsticknova winsticknova

2013/10/22

#
thanks! i got it to work. was a lot simpler than i thought it was
MontclairState MontclairState

2013/11/3

#
What are you suppose to write in the "//do something here"?
MontclairState MontclairState

2013/11/3

#
This is what i put but it doesn't work it just adds the enemySpeed to the Y value once and the enemies just stay in one spot and don't continually fall towards the bottom.
public void setSpeed(int enemySpeed)
	{
		setLocation(getX(), getY()+enemySpeed);
	}
danpost danpost

2013/11/3

#
The body you added to the method does not set the speed of the actor (it moves the actor down by the speed given). You need a statement that sets the speed of the actor (assigns the given value to the field that represents the speed of the actor)..
You need to login to post a reply.