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

2012/12/28

attaching objects

BradH BradH

2012/12/28

#
Hello, I am making a game where I have one actor (the main character) and then I have a second actor (the gun) I would like to aim the gun by moving the mouse but somehow i would also like to attach the gun to the other actor (main character) so when the main character moves the gun moves with it. How would I do this???
Duta Duta

2012/12/28

#
To aim the gun, try looking at something along the lines of:
public class Gun extends Actor {
	public void act() {
		MouseInfo m = Greenfoot.getMouseInfo();
		// We have to check if m isn't null,
		// because if the mouse is offscreen
		// then m will be null, and so the
		// "m.getX()" and "m.getY()" calls
		// will cause a NullPointerException.
		if(m != null) {
			int mouseX = m.getX();
			int mouseY = m.getY();
			turnTowards(mouseX, mouseY);
		}
	}
}
To make the gun move with the character, there are many approaches. One method is to, in your Gun class, have a reference to the character. Then, every act() cycle you can move to the offset from the character. E.g.:
public class Gun extends Actor {
	private Player player;

	public Gun(Player player) {
		this.player = player;
	}

	public void act() {
		int playerX = player.getX();
		int playerY = player.getY();
		// Modify the xOffset and
		// yOffset to make the gun
		// appear in the correct
		// position.
		int xOffset = 30;
		int yOffset = 20;
		int x = playerX + xOffset;
		int y = playerY + yOffset;
		setLocation(x, y);
	}
}
BradH BradH

2012/12/29

#
SpacemarineModel1 is the character Gun is the gun's actor, the one I want to attach to SpacemarineModel1 So in the Gun class I would type the code up above. Would I switch player with SpacemarineModel1??? so then the gun can keep track of the location of SpacemarineModel1 Thanks for your time.
Duta Duta

2012/12/29

#
Well, yes :P Try to look at the code and see what it's doing, don't just blindly copy and paste it in there :) If you look at it, you should work out that yes; you need to replace Player with SpacemarineModel1.
BradH BradH

2012/12/29

#
just makin sure, thanks
BradH BradH

2012/12/29

#
Below is the code for the GUN class (but I have changed the x and yOffset) the Gun attaches perfectly but it is not following the mouse as it should when executing the turnTowards method. Would a "thread" help? (Sorry for not putting it in the code format, everytime I tried screen turned blue and I had to refresh the page) public class Gun extends Spacemarinemodel1 { private Spacemarinemodel1 spacemarinemodel1; public Gun(Spacemarinemodel1 spacemarinemodel1) { this.spacemarinemodel1 = spacemarinemodel1; } public void act() { attach(); } public void attach(){ int spacemarinemodel1X = spacemarinemodel1.getX(); int spacemarinemodel1Y = spacemarinemodel1.getY(); //declare spacemarinemodel to store location for x any y on coordinate plane //Offset location where the gun will be attached on the body int xOffset = 5; int yOffset = 4; int x = spacemarinemodel1X + xOffset; int y = spacemarinemodel1Y + yOffset; setLocation(x, y); } public void turnTowards (int x, int y) { double dx = x - getX(); double dy = y - getY(); double angle = Math.atan2(dy,dx)*360.0/Math.PI; setRotation( (int)angle ); } public void turnTowards (MouseInfo mi) { turnTowards(mi.getX(), mi.getY()); } }
danpost danpost

2012/12/29

#
You can manually key the tag before pasting the code; and then key the tag after.
vonmeth vonmeth

2012/12/29

#
You need to actually call turnTowards with the MouseInfo. Add such lines to your act method. It will work after you do so.
    MouseInfo info = Greenfoot.getMouseInfo();
    if (info != null)    turnTowards(info);
danpost danpost

2012/12/29

#
@vonmeth, line 2 should be
if (info != null) turnTowards(info.getX(), info.getY());
vonmeth vonmeth

2012/12/29

#
@danpost, he wrote a turnTowards method with a MouseInfo parameter (not exactly needed, but figured I'd use it if he had it.)
BradH BradH

2012/12/29

#
thanks for the tip danpost, and thanks vonmeth for the help
danpost danpost

2012/12/29

#
Actually, the turnTowards method does not have to be written out either; as it is included as a method in the Actor class. Makes no sense to override it just to do what it does anyway.
You need to login to post a reply.