RSS link icon

Enemy following me actionscript 

 

Okay I yesterday I was doing some actionscripting to make a game, (cant reveal it yet though because its not my own, I was just hired to make some parts of it). But I would like to share some of the code with you, like the source code I share today.

This flash actionscript source code is to make an enemy follow your mouse, and some of the great things to notice is that it not only follows that mouse path, it also rotates so its front view is always facing the mouse, thats pretty cool.

So first we need to do some small preparations like creating our enemy, Im going to let this up to you, do whatever you want. When you are done, right click and convert it to a movieclip and give it an instance name, I named mine "enemy_mc"

flash actionscript enemy following me

Now we are ready to do some actionscripting, but remember this is for actionscript 3.0, for it to work with previous versions you will need to do some changes and Im not doing it for you. :-)

You can just copy and past the code into your flash actionscript panel, or you can try reading the comments.

 

The actionscript source code:

First an eventlistener to call the function to make the enemy move, this is an enterframe event.

enemy_mc.addEventListener(Event.ENTER_FRAME, do_stuff);

 

The function we call

function do_stuff(event:Event):void {
    // here we do some calculations to make the ememy rotate based on the mouse angle, centered to the ememy.
    var myRadians:Number = Math.atan2(mouseY-enemy_mc.y, mouseX-enemy_mc.x);
    var myDegrees:Number = Math.round((myRadians*180/Math.PI));
    
    // this is the stuff making the enmy move towards the mouse.
    var yChange:Number = Math.round(mouseY-enemy_mc.y);
    var xChange:Number = Math.round(mouseX-enemy_mc.x);
    var yMove:Number = Math.round(yChange/20);
    var xMove:Number = Math.round(xChange/20);
    
    // Nothing will work without using all this previous calculations, so we set the x and y axis and the rotation.
    enemy_mc.y += yMove;
    enemy_mc.x += xMove;
    enemy_mc.rotation = myDegrees+90;
}

Dave says: Thursday, November 06, 2008

AS2 sucks!


amit says: Friday, July 25, 2008

good one............


shimja says: Monday, July 21, 2008

I want to know this animtion in action script 2


Jim says: Friday, July 11, 2008

Any one have this code for Action Script 2

   


 

 

 

 6

 
 
   Web Premium
 
 

All rights reserved, Copyright 2008. Contact