ForumsProgramming ForumHelp with moving character and mov

17 5158
herofmb
offline
herofmb
30 posts
Nomad

Hello to all.

First I'm kinda new to flash so I don't know if this is ok to ask =p.

Here it goes:

I'm having a problem to make my character move and his weapon follow the mouse while in movement. I have no problem when I dont put the gun inside the movie clip(the hero) and make a stage.addChild(..) instead of Hero.addChild(..). I'm using AS3 and using classes. I think the code will explain better.

//The class of the Pistol
public class Pistol extends Weapon
{
private var angleBetweenMousePistol:Number;
private var radianToDegree = (180/Math.PI);

public function Pistol()
{
addEventListener(Event.ENTER_FRAME, eFrame);

x = 55;
y = 10;
}

public function eFrame(e:Event):void
{
angleBetweenMousePistol = Math.atan2(stage.mouseY - this.y, stage.mouseX - this.x );
angleBetweenMousePistol *= radianToDegree;
this.rotation = angleBetweenMousePistol;
}
}

What I do next is create a hero using the Hero class and make the hero add the pistol

//class Main
var hero:Hero = new Hero();
var pistol:Pistol = new Pistol();
hero.addChild(pistol);
//

The problem is that everything happens like if the pistol movie clip have been added by the stage (it follows the mouse, but it acts like it'd be at its position I set earlier ( x = 55 and y = 10).
I think this is a bit weird because it moves along the hero, but acts like if it weren't moving. Thanks to all.

  • 17 Replies
herofmb
offline
herofmb
30 posts
Nomad

But don't you agree that all three instructions are supoose to be equal (from the pistol class, wich is child of hero, wich is child of stage) ?

//this instruction works
angleBetweenMousePistol = Math.atan2(parent.mouseY - this.y, parent.mouseX - this.x);

//this instruction works
angleBetweenMousePistol = Math.atan2(stage.mouseY - (parent.y + this.y), stage.mouseX - (parent.x + this.x));

//this instruction DOES NOT work
angleBetweenMousePistol = Math.atan2(mouseX, mouseY);

I still don't get why only the one that uses the mouseX and mouseY of the object beeing rotated (the pistol) gives crazy results.

PixelSmash
offline
PixelSmash
566 posts
Nomad

Can't help you with that, sorry... I know a bit about AS3, but hardly everything I often do what I know works, and if I learn something new I try to update it in the code I'm using

Showing 16-17 of 17