Search code examples
actionscript-3flash-cs5

How to define the position of a movieclip in relation to that of another of movieclip


I am trying to make a movieclip follow a custom mouse pointer (which is a movieclip) but always stay at a defined postion (in terms of coordinates), a distance away from the mouse pointer on easing to where the mouse pointer is. Below is the code:

import flash.display.MovieClip;
import flash.events.Event;
Mouse.hide();



var mouseCounter:int = 0;
var mouseDelay:int = 5;// how many frames the mouse must stay still before the follow code is run.    
var speed:Number = 5;

stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);

stage.addEventListener(Event.ENTER_FRAME,follow);

// set counter back to zero whenever the mouse is moved. 
function mouseMove(e:MouseEvent):void
{
    wand.x = stage.mouseX;
    wand.y = stage.mouseY;
    e.updateAfterEvent();
    mouseCounter = 0;
}

function follow(e:Event):void
{

    // increment the counter each frame
    mouseCounter++;

    // now run the follow block if the mouse has been still for enough frames. 
    if (mouseCounter >= mouseDelay)
    {
        orb_mc.x -= (orb_mc.x - mouseX) / speed;
        orb_mc.y -= (orb_mc.y - mouseY) / speed;

        orb_mc.x = mouseX + 46.5;
        orb_mc.y = mouseY +50.95;
    }
}

The last two lines of codes (26 & 27), is what I used to define the orb_mc's position in relation to the custom mouse pointer which is "wand" however it seems to have resulted in the ease movement of the orb been hampered, so dont know if the positioning code I used is wrong


Solution

  • function follow(e:Event):void
    {
    
        // increment the counter each frame
        mouseCounter++;
    
        // now run the follow block if the mouse has been still for enough frames. 
        if (mouseCounter >= mouseDelay)
        {
            // Do this:
            orb_mc.x -= (orb_mc.x - mouseX + 46.5) / speed;
            orb_mc.y -= (orb_mc.y - mouseY + 50.95) / speed;
    
            // OR this:
            //orb_mc.x = orb_mc.x - (orb_mc.x - mouseX + 46.5) / speed;
            //orb_mc.y = orb_mc.y - (orb_mc.y - mouseY + 50.95) / speed;
    
            // but not both.
        }
    }
    

    You see, once you use one of the increment assignment operators (-=,+=,/=,*=), immediately following that by a regular assignment operator will obviously override any value it had before. You understand that actionscript gets "read" by the computer (this is probably the wrong verbiage but you get my drift) and it reads each block (an area inside a curly brace set { }) from top to bottom. So the very first time that your follow method is called, it is doing 4 things, in order:

    1. increment the counter
    2. evaluate if mouseCounter >= mouseDelay
    3. increments orb_mc x/y coords
    4. assigns orb_mc x/y coords to final destination

    It is doing this on the very first time the follow block is read. This is why both acceptable bits of code in my answer do 2 things:

    1. they increment the x/y coords (don't set them to a final value)
    2. they change based on a variable factor (speed)

    Glad it's working!