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

2013/5/4

Object Spinning?

SWAG SWAG

2013/5/4

#
I want a boomerang to automatically move half way up the screen, then switch image of it facing the other way and then return to same place at the bottom of the screen (this process should keep continuing without any key inputs) and I want it to spin from the center. How would I do all this?
danpost danpost

2013/5/4

#
Using 'setRotation' or 'turn' on an object repeatedly (by placing the code in the 'act' method) will make the object spin from the center. By setting limits on the returned value of 'getY', you can detect when to change the direction of the actor (by negating the x-speed and y-speed) and flip the image (either by using 'mirrorHorizontally' on the image or by changing the image source).
SWAG SWAG

2013/5/5

#
danpost wrote...
Using 'setRotation' or 'turn' on an object repeatedly (by placing the code in the 'act' method) will make the object spin from the center. By setting limits on the returned value of 'getY', you can detect when to change the direction of the actor (by negating the x-speed and y-speed) and flip the image (either by using 'mirrorHorizontally' on the image or by changing the image source).
I understanding the spinning thanks. How do I make by boomerang go up and down automatically and mirror horizontally coming down. Could you write some code? I don't have any code in my boomerang class.
danpost danpost

2013/5/5

#
You need to decide where you want it to start from (the lower location -- or the highest y value) and reach its apex at (the higher location -- or the lowest y value). I will call these points 'hiY' and 'loY'. Then, use the following for moving it.
// declare these instance fields
private int xSpeed = 0; // this can be any value
private int ySpeed = -1; // this must be less than zero
// in the act method (or a method it calls)
setLocation(getX()+xSpeed, getY()+ySpeed); // move
turn(5); // spin
if (getY() < loY || getY() > hiY) // check limits
{
    xSpeed = -xSpeed; // change direction (part 1)
    ySpeed = -ySpeed; // change direction (part 2)
    getImage().mirrorHorizontally(); // mirror image
}
The limit values (for loY and hiY) can be hard-coded (replaced with actual values), or saved in instance fields, or brought in via the constructor parameters and saved in instance fields.
SWAG SWAG

2013/5/5

#
danpost wrote...
You need to decide where you want it to start from (the lower location -- or the highest y value) and reach its apex at (the higher location -- or the lowest y value). I will call these points 'hiY' and 'loY'. Then, use the following for moving it.
// declare these instance fields
private int xSpeed = 0; // this can be any value
private int ySpeed = -1; // this must be less than zero
// in the act method (or a method it calls)
setLocation(getX()+xSpeed, getY()+ySpeed); // move
turn(5); // spin
if (getY() < loY || getY() > hiY) // check limits
{
    xSpeed = -xSpeed; // change direction (part 1)
    ySpeed = -ySpeed; // change direction (part 2)
    getImage().mirrorHorizontally(); // mirror image
}
The limit values (for loY and hiY) can be hard-coded (replaced with actual values), or saved in instance fields, or brought in via the constructor parameters and saved in instance fields.
Thanks for your help. I have one more question. How would I play a boomerang sound if my character is close to it.
danpost danpost

2013/5/5

#
First, you would need to hold the GreenfootSound in an instance field in the class of your character (I will call it 'sound'). Then, in the class of the character, use 'getObjectsInRange' to see if a boomerang is in the desired range (I will use '80' for the range).
// declare in class of your character
GreenfootSound sound = new GreenfootSound("boomerang.wav);
// in the act method (or a method it calls)
if (getObjectsInRange(80, Boomerang.class).isEmptyy() && sound.isPlaying()) sound.stop();
if (!getObjectsInRange(80, Boomerang.class).isEmpty() && !sound.isPlaying()) sound.play();
You need to login to post a reply.