ForumsProgramming ForumHelp with moving character and mov

17 5161
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

Sorry about the title, I didnt find an edit button

PixelSmash
offline
PixelSmash
566 posts
Nomad

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;
}

herofmb
offline
herofmb
30 posts
Nomad

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.

Cya

PixelSmash
offline
PixelSmash
566 posts
Nomad

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!

herofmb
offline
herofmb
30 posts
Nomad

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.

Anyone knows why =o?

Sparkky
offline
Sparkky
14 posts
Nomad

let me see if I can help

________________
| ________ |<---Stage
| | | |
| | ____ |<-+----Hero
| | | | | |
| | |____|<+--+----Gun
| |________| |
|________________|

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.

Sparkky
offline
Sparkky
14 posts
Nomad

__________
| ______ |<---Stage
| | __ |<+----Hero
| | | | | |
| | |__|<+-+----Gun
|_|______|_|
Lets see if that diagram worked better.

Sparkky
offline
Sparkky
14 posts
Nomad

http://img148.imageshack.us/img148/7789/parentchilddiag.png
there we go, paint to the rescue

PixelSmash
offline
PixelSmash
566 posts
Nomad

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

herofmb
offline
herofmb
30 posts
Nomad

Like PixelSmash said, very well explained, helped me a lot. Thanks Sparky.

Certainly I'll ask again, great forum we have here. I hope I'll be able to help as well in a short future =D.

Cya guys.

herofmb
offline
herofmb
30 posts
Nomad

and with Sparky explanations I got an alternative without using the localToGlobal method.

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

works exactly as

point = new Point(this.x,this.y);
point = parent.localToGlobal(point);
anguloEntreMousePistola = Math.atan2(stage.mouseY - point.y, stage.mouseX - point.x );

Wow, I lerned a lot from this topic ^^

herofmb
offline
herofmb
30 posts
Nomad

Haha another way:

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

The only one I couldn't make work was is using the mouseX and mouseY of the pistol. It was supose to be the easiest:

//It doesn't work
angleBetweenMousePistol = Math.atan2 (this.mouseY, this.mouseX);

From the pistol class I made some tests:

trace(mouseX, mouseY);
Got 249.25 -268

trace(parent.mouseY - this.y, parent.mouseX - this.x);
Got 135 63

trace(stage.mouseY - (parent.y + this.y), stage.mouseX - (parent.x + this.x));
Got 135 63

??!!

PixelSmash
offline
PixelSmash
566 posts
Nomad

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.

Keep experimenting though!

herofmb
offline
herofmb
30 posts
Nomad

Im finding out flash very addict, heheh.

But the pistol is a movieClip (pistol extends Weapon, which is a movieClip), and it has its own mouseX and mouseY, just like the hero.

So it does exist, or not?

PixelSmash
offline
PixelSmash
566 posts
Nomad

It might exist, yes, but the mouseX you want to use is that of mouse.mouseX or stage.mouseX, I think.

Never really thought about it ^^;

Showing 1-15 of 17