I’m really new to flash and as3…
I’m trying to create 2 scenes in flash – one where my movieclip moves away from the mouse whenever it goes near it, and the other where the movie clip is attracted to the mouse. I have found an answer on actionscript 2 but i cannot use this in my as3 file…
Any help or ideas?
Cheers!
I’m really new to flash and as3… I’m trying to create 2 scenes in
Share
Here is an example I made of a display object being “pushed” and “pulled” in relation to the mouse’s position.
Main.as(Document class):
In the
init()method we add a new display object to the stage, this is the display object we will be “pulling” and “pushing” in relation to the mouse’s position.Then we set the
_forceproperty to ourPULLconstant. The_forceproperty will determine whether the display object is “pulled” or “pushed”.Next we add our mouse event listeners to the stage. On
MouseEvent.MOUSE_DOWNwe call theonStageMouseDown()event handler. When the handler is called, we add anEvent.ENTER_FRAMEandMouseEvent.MOUSE_UPevent listeners to the stage.When the
MouseEvent.MOUSE_UPevent handler is called, the previousEvent.ENTER_FRAMEandMouseEvent.MOUSE_UPevent listeners are removed from the stage. Then depending on the_forceproperty’s value, its value is alternated betweenPUSHandPULL.Lastly, the
onStageEnterFrameevent handler. This is where we calculate the new position of the display object in relation to the mouse.There are different ways to go about this, but I decided to use the
Pointclass to simplify things. First we have to get aPointobject for the display object’s position and anotherPointobject for mouse’s position.Next we have to get the difference between the points using the
Pointobject’ssubtract()method.With this new point, we can use the
Pointobject’snormalize()method to scale the line segment between the display object’s position and the mouse’s position to a set length.Finally depending on the _force property’s value we either subtract or add the point’s
xandyproperties from the display object’sxandyproperties.