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.
What probably happens here is that you use the x and y of the weapon - which is good, ofcourse - but not relative to the stage but to the parent - in this case the hero. I can't be exactly sure - it's late and I'm a bit tired - but I think that's the problem.
What you should do then is change the code to something like this: public function eFrame(e:Event):void { //create a point and fill it with the gun's x and y var point:Point = new Point(x,y); //now convert the x and y of point to the corresponding x and y if it were on the stage point = this.localToGlobal(point); //if that didn't work right, use parent.localToGlobal angleBetweenMousePistol = Math.atan2(stage.mouseY - point.y, stage.mouseX - point.x ); angleBetweenMousePistol *= radianToDegree; this.rotation = angleBetweenMousePistol; }
That'it it worked, I made some trace(this.x, this.y) and, like you said, I was getting the coordinates x and y of the pistol relative to the hero, wich, obvisouly, would never change because they moved together.
Thanks =), I didnt know about this method "localToGlobal" I think will help me a lot in the next stages of developiment.
There's still a small problem that when I point the mouse near the pistol, its gets crazy =p. But I think I can fix that.
No prob! I heard of it by accident myself (I saw someone use it, and thought it sounded cool, that's all :P) but it turned out to help save my ass a couple of times.
And the mouse acting weird is probably 'cause of your mouse being inside of the spot the weapon could rotate... making it really fast and uncontrollable. But, like you said, it´s pretty easy to fix. Good luck!
Hmm, something here made me think about. I fixed the problem with the mouse going crazy easily 3 days ago simply using
//the code above var point:Point = new Point(x,y); point = parent.localToGlobal(point); //the code below
instead of using
//the code above var point:Point = new Point(x,y); point = localToGlobal(point); //the code below
Like PixelSmash suggested.
I made some trace(point) in both cases, and only the one that uses the parent to execute the method kept the coordinates constant when the charcater is not moving, and that's what I want so everything works fine and when I point the mouse near the gun it doesnt get crazy.
I think this is a bit weird and I didn't find a satisfatory answer WHY when I use the gun object (when I use point = localToGlobal(point) is the same as using point = this.localToGlobal(point)) the coordinates keep making small changes even if I dont move the charcater and thats explain why it gets crazy when the mouse is too near the gun object.
The Stage is a sprite which has its own (x, y) co-ordinates within it The Hero is a movie clip, which extends Sprite, which means it has its own internal (x,y) grid.
If in the Stage code you wrote addChild(Hero); then in the hero .as file went addChild(Gun);
The Hero is a child of the stage, and the Gun is a child of the hero. The Stage is the parent of the Hero and the Hero is the parent of the Gun
So if your adding bullets to the Stage, you need to know the Gun's location on the stage to know the starting location. You can think of this like a house on a street. The stage is the street and there are rooms in the house. If you want to drop a bomb on your little brother bedroom you cant just say the room in the house, you also have to say where the house is on the street.
So you'd say 100 Spooner street, then first room on the left from the front door.
If in flash you were looking at adding the bullet to the stage, from the stage you could do the following. Bullet.x = Hero.x + Gun.x; addChild(Bullet); or if you were creating the bullet from the gun object you could say Bullet.x = x + parent.x; parent.parent.addChild(Bullet);
Hopefully this helped you visualize the child/parent relationship. In your example the parent.localToGlobal would have taken that (x,y) position on the parent object and translated it to the stage co-ordinates rather than that (x,y) on the gun object and then translated to the stage.
I think Sparkky explained it pretty well Better than I could, in fact. Anyway, I'm glad it worked out (even if it took some trying) and you can use the hero as it was intended. If you need some more help, just ask
I think you used a mouseX that doesn't really exist... instead of using a ref to stage.mouseX you use parent.mouseX - and if I'm not mistaken this is called from within the Pistol class. Which means you try to use hero.mouseX >_>
And your previous post works just as well indeed, and for what you're trying to do right now it's perfect - and quite possibly even a bit faster. Once you nest your movieclips more however, say [stage >> world >> characters >> hero >> weapon], using localToGlobal becomes easier to read - otherwise you'd have a whole lot of parent.parents.