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

2016/11/24

how to make a swing object?

williandutras williandutras

2016/11/24

#
how to make this rope stay swinging for left and right?
Super_Hippo Super_Hippo

2016/11/24

#
You could change the image of it to have to have to midpoint (so the point where it is rotating) at the top. So just add a transparent part which is as long as the original image. In other words, create a new image twice as long as the original one and draw the original image on one half of it. Add a 'false' to the 'super' class call in the world constructor:
//change this...
super(600, 400, 1); //or some other numbers
//...to
super(600, 400, 1, false);
This way, part of your rope actor can be outside the world. (The visible part will be the same as it is right now.) Then you can use something like this for turning:
private double speed;

public void act()
{
    int r = getRotation();
    if (r>90) speed -= 0.15;
    else speed += 0.15;
    setRotation((int)(r+speed));
}
I used something similar to moving up and down continuously, so maybe it needs some adjustment in the numbers.
williandutras williandutras

2016/11/24

#
wooow, thanks Super_Hippo :D I'm very noob in greenfoot, how to change the eixo of object? Because in my rope, the eixo stay in the center, its not perfect :/
danpost danpost

2016/11/24

#
You probably need to double the width or height of the image for the rope. Try this in the Rope class constructor ('public Rope()'):
GreenfootImage base = getImage();
GreenfootImage image = new GreenfootImage(base.getWidth(), base.getHeight()*2);
image.drawImage(base, 0, base.getHeight());
setImage(image);
This will work if the image of the rope at zero rotation is hanging straight down. The code takes the original image and draws it on an image frame that is twice as tall, moving the point of rotation to the top of the original rope image.
You need to login to post a reply.