character movement with mouse click
This will be for an MMO online game/ world. So it needs to be good.
Hi guys. I'm really happy I got this to work as I'm a neebie to AS3 but getting to love its simplicity. My problem is not my AS3 but my maths.
I click the mouse and the movieclip runs to the mouse's x position. However, it only goes in one direction ie: downwards and to the right.
I need to it go in all directions depending on where I click. I suppose it has to do with comparing x and y positions of both mouse and mc - ie: if one is bigger than the other then you can tell where both are positioned and then put the code in. Am I right and does anybody hace a little code like this flying around for me?
import flash.display.Stage;
import flash.events.Event;
stage.addEventListener(MouseEvent.CLICK, myClickReaction);
function myClickReaction (e:MouseEvent):void{
sunny.gotoAndPlay("runback")
//sunny.x = root.stage.mouseX
//sunny.y = root.stage.mouseY
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onEnterFrame(event:Event):void {
var xDistance:Number
var yDistance:Number
//mc increments by 5 until it reaches mouse - mc (ie the distance)
sunny.x += 5;
sunny.y += 5;
xDistance = root.stage.mouseX - sunny.x
trace (xDistance)
if (xDistance <= 0 ) {
trace("works")
sunny.gotoAndPlay("static")
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
}
}
