Skip to main content
Inspiring
April 5, 2010
Answered

character movement with mouse click

  • April 5, 2010
  • 4 replies
  • 5509 views

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

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

I've just tried that code. It worked almost all the time but once it didn't.

I've tried my code loads of times and works all the time - don't you like it???


As I said, my code doesn't deal with detecting when to stop well. Perhaps, yours does it better. But your code is not as optimal as mine in terms of places where variables are declared and calculated (particularly, angle and unnecessary declaration of distanceY and distanceX). You should reconsider that part.

4 replies

Participant
February 2, 2016

codeBeastAdobeAndrei1‌can i get the project or file ?? i really need example same as your problemm.. for character movement with mouse click, please i really need that.. thankyouu... i am newbie in programming,

Inspiring
April 5, 2010

OK cheers. I'll have a look later. At least it works for now but yeah it'll have to get more professional.

Inspiring
April 5, 2010

In addition to my previous post, if you need the object to move toward mouse, you, perhaps, need to use trigonometry to change movement direction and speed.

Inspiring
April 5, 2010

Thanks for contacting but the code more or less works already apart from the comments below.

The position I get comparing mousex position and movieclip position. BUT it goes loopy when I move the mouse around. ie: I need the coordinates when I click and not a variable that changes while I move mouse around.

I will check it out now.

import flash.display.Stage;
import flash.events.Event;

stage.addEventListener(MouseEvent.CLICK, myClickReaction);

function myClickReaction (e:MouseEvent):void{
 

//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)
            // if you click to the right of sunny then mouse is > than sunny.x so x = 5
   if (root.stage.mouseX > sunny.x) {
    sunny.gotoAndPlay("walk right")
    sunny.x += 5;
    sunny.y += 0;
    xDistance = root.stage.mouseX - sunny.x
    //if (xDistance <= 0 ) {
    //sunny.gotoAndPlay("static")
    //removeEventListener(Event.ENTER_FRAME, onEnterFrame);
   //}
        }
}

Inspiring
April 5, 2010

Here is code that moves object toward mouse click - it uses trigonometry:

stage.addEventListener(MouseEvent.CLICK, myClickReaction);
// speeds ALONG NYPOTENUSE
var v:Number = 5;
// mouse click point
var clickPoint:Point = new Point();
function myClickReaction (e:MouseEvent):void {
     clickPoint.x = mouseX;
     clickPoint.y = mouseY;
     addEventListener(Event.ENTER_FRAME, onEnterFrame);
}

function onEnterFrame(event:Event):void {
     var xDistance:Number = clickPoint.x - sunny.x;
     var yDistance:Number = clickPoint.y - sunny.y;
     var angle:Number = Math.atan2(yDistance, xDistance);
     sunny.x += v * Math.cos(angle);
     sunny.y += v * Math.sin(angle);
}

Inspiring
April 5, 2010

Here is a quick an dirty example of changing directions depending on mouse click. It doesn't cover animation stopping though:

stage.addEventListener(MouseEvent.CLICK, myClickReaction);
// speeds
var vx:Number = 5;
var vy:Number = 5;
function myClickReaction (e:MouseEvent):void {
     vx = mouseX > sunny.x ? 5 : -5;
     vy = mouseY > sunny.y ? 5 : -5;
     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 += vx;
     sunny.y += vy;
     xDistance = Math.abs(mouseX - sunny.x);
     yDistance = Math.abs(mouseY - sunny.y);
     trace (xDistance)
     if (xDistance == 0 ) {
          removeEventListener(Event.ENTER_FRAME, onEnterFrame);
     }
}