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

2013/10/6

[Help] Make object Follow Another Object Movement

fayde fayde

2013/10/6

#
Hello I have a problem about the movement of Object I'm confused because when the body is moving object, the hand does not want to follow the movements of the body In the hands of this I have some coding What I want is when the body moves, the hand gestures also follow but the direction of the hand follows the mouse I have read on topic http://www.greenfoot.org/topics/1821 But I'm still confused It's my image hand and body separately Please help, thank you It's the hand code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class BRS_RH here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class BRS_RH extends Actor
{
    private int mouseY;
    private int mouseX;
    GreenfootImage shg_brs = new GreenfootImage("/Right Hand/Right Hand_3.png");
    /**
     * Act - do whatever the BRS_RH wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void shg(){
    if(Greenfoot.getMouseInfo() != null){
    mouseY = Greenfoot.getMouseInfo().getY();
    mouseX = Greenfoot.getMouseInfo().getX();
    turnTowards(mouseX,mouseY);
    }
    }
    public void act()
    {
        // Add your action code here.
        setImage(shg_brs);
        shg();
    }   
}
danpost danpost

2013/10/6

#
Either the body would have to set the location of the hand (adjusting it as the body moves) or the hand would have to position itself properly with respect to the body each act cycle. You may be experiencing the problem that the hand gets disconnected from the body as it rotates to turn toward the mouse. To compensate for this, make sure the pivot point is at the exact center of the image so when it turns the pivot point remains in place.
fayde fayde

2013/10/6

#
I still do not understand How to keep the hand in the position Sorry, i'm still noob
danpost danpost

2013/10/6

#
You need to add code to move the hand with the body. Whichever direction the body moves, the hand needs to move the same amount in the same direction. So, when the body moves with 'setLocation(getX()+dx, getY()+dy)', the hand should move with 'hand.setLocation(hand.getX()+dx, hand.getY()+dy)'. The 'hand' reference either needs to be passed to the class of the body or obtained through method calls '(Actor)getWorld().getObjects(BRS_RH.class).get(0)'.
fayde fayde

2013/10/6

#
now I have understood, thankyou for help
darkdaser darkdaser

2015/5/12

#
danpost wrote...
You need to add code to move the hand with the body. Whichever direction the body moves, the hand needs to move the same amount in the same direction. So, when the body moves with 'setLocation(getX()+dx, getY()+dy)', the hand should move with 'hand.setLocation(hand.getX()+dx, hand.getY()+dy)'. The 'hand' reference either needs to be passed to the class of the body or obtained through method calls '(Actor)getWorld().getObjects(BRS_RH.class).get(0)'.
What do you meen with "dx" and "dy"?
danpost danpost

2015/5/12

#
darkdaser wrote...
What do you meen with "dx" and "dy"?
The refer to the positional offsets (the amount of movement) along the horizontal and vertical axes -- the change in value of the location coordinates for the given movement.
You need to login to post a reply.