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

2014/1/11

I want my actor holding the gun

nungzabza1916 nungzabza1916

2014/1/11

#
I move my actor(Human) right 10 pixel but the gun does not follow the human. Please give me the code that make the gun follow the another actor. Thank you.
Euan Euan

2014/1/11

#
What you've said here comes across as a little unclear. Do you want a class to follow another class, have a class move with Human, have bullets shoot at the same rotation as Human or something else?
nungzabza1916 nungzabza1916

2014/1/11

#
Class move with human.
nungzabza1916 nungzabza1916

2014/1/11

#
I mean group them together.
Euan Euan

2014/1/11

#
I take it "Human" is that class you play as?
nungzabza1916 nungzabza1916

2014/1/11

#
Yes
Gevater_Tod4711 Gevater_Tod4711

2014/1/11

#
Therefore you need to create the gun in your human class to keep the reference to the gun. Then you can override the setLocation method so that the gun is moved with your human:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//in your human class:
private Gun gun;
 
public Human() {
    gun = new Gun();
}
 
public void addedToWorld(World world) {
    getWorld().addObject(gun, getX(), getY()); //maybe you have to use other coordinates here;
}
 
public void setLocation(int x, int y) {
    super.setLocation(x, y);
    gun.setLocation(x, y);//again maybe some other coordinates;
}
nungzabza1916 nungzabza1916

2014/1/11

#
Thank you for all replies. ^^
You need to login to post a reply.