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

2013/1/26

set speed for single actor

bonana bonana

2013/1/26

#
Hey guys, I was just wondering if it's possible to set the speed for one single actor. I don't want to use move(speed) because it just moves into the direction the actor is facing. Is there another method?
danpost danpost

2013/1/26

#
You could try using:
setLocation(getX()+xSpeed, getY()+ySpeed);
bonana bonana

2013/1/27

#
I just want to set the speed (like the Greenfoot.setSpeed(speed) method does) but only for one actor. The actor should just move faster, in every direction.
danpost danpost

2013/1/27

#
Then you need a direction field and a speed field for the actor. The value of the direction field should be the angle in which the actor is currently moving (0 to 359). This angle can be broken down to its 'x' and 'y' by using the Math trig functions 'cos' and 'sin'. Multiplying these results by the speed field will give the 'xSpeed' and 'ySpeed' you will need in the code of my last post. You can increase the speed of the actor by increasing the value of the speed field.
bonana bonana

2013/1/27

#
Ähh, could you give me an example please? :D
danpost danpost

2013/1/27

#
I thought I had spelled it out pretty clearly above; and I do not wish to spoil your continued education by doing your homework for you now. I will give you this, though. From the angle 'direction', the ratio of movement in the 'y' direction to the 'x' direction (the slope of the line formed by the movement) is Math.cos(Math.toRadians(direction)) divided by Math.sin(Math.toRadians(direction)); and since the values returned from these functions result in a real number in the range of -1 to 1, they are returned as type 'double'. Multiplying these 'double' values by the speed will return to amount of movement in the x and y directions.
bonana bonana

2013/1/29

#
Maybe I'm still doing it wrong, but your method lets move my actor only in positive x and y direction. The actor should also move in negative direction.
danpost danpost

2013/1/29

#
Both the 'sin' and 'cos' function return values between -1 and 1, so it must be in what you are doing; and without seeing what you are trying, we cannot help.
bonana bonana

2013/1/29

#
Not necessary anymore. Now I got it (took a long time :D). I'm too stupid! Your first answer was sufficient. I was just on a totally wrong path... Thank you. It's so easy, I could do it myself... I was too confused and didn't think of easy solutions. :D Sorry
You need to login to post a reply.