mrBoxes : Animation Tutorials
 

main box | registration box | members box | drop box | 

          
 
This set of tutorials demonstrate a variety of ways to use the mouse and keyboard to control the movement of an object.

Download the control_blank.fla which just contains the spaceship movieClip and background graphic. Save each of these projects under a different name

download control_blank.fla

 

[1] Follow the Cursor

T.

   

    
 
    



 

 
       
This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse
 


 

and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse

onClipEvent (load) {
     // this example demonstrates an object moving in the x axis

     // define a variable which will control the speed
     // of this object
     // Note : > number moves slower < number moves faster

     speed=10;
}
onClipEvent (enterFrame) {

     // find the distance between the mouse and the spaceship
     // then calculate a fraction of that distance
     // by dividing by speed

     xdist = (_root._xmouse-_x)/speed;

    
// update the x position of this object
     _x=_x+xdist;


     // collision detection with right edge
     // if this object

     if(_x>(400-(_width/2)))
          {
          _x=(400-(_width/2));
          }


     // collision detection with left edge
     if(_x<(_width/2))
          {
          _x=(_width/2);
          }

}

Challenge
The code above allows the spaceship to follow the mouse only in the X axis. Try modifying the code to allow the spaceship to follow the mouse in both the X and Y axis.

[2] Follow the Cursor

and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.

           

    
 
    



 

 
    
tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the

 

 

 

 

mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.


onClipEvent (enterFrame) {
     // find the distance between the mouse and the spaceship
     // then calculate a fraction of that distance
     // by dividing by speed

     if(Key.isDown(Key.RIGHT))
          {
          _x=_x+5;
          }

     if(Key.isDown(Key.LEFT))
          {
          _x=_x-5;
          }

     if(Key.isDown(Key.UP))
          {
          _y=_y-5;
          }

     if(Key.isDown(Key.DOWN))
          {
          _y=_y+5;
          }

     // collision detection checks if the spaceship has passed
     // a specific numerical boundary (left, right, bottom and
     // top). If it has then the spaceship is placed back          
     // inside the boundary.

     // collision detection with right edge
     if(_x>(400-(_width/2)))
          {
          _x=(400-(_width/2));
          }

     // collision detection with left edge
     if(_x<(_width/2))
          {
          _x=(_width/2);
          }

     // collision detection with bottom edge
     if(_y>(210-(_height/2)))
          {
          _y=(210-(_height/2));
          }

     // collision detection with top edge
     if(_y<(_height/2))
          {
          _y=(_height/2);
          }
}

 

Challenge
Try modifying the code to

 

 

[3] Follow the Cursor

This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.

    
 
    



 

 
     
  This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This tutorial demonstrates a variety of ways to use the mouse and keyboard to control an object controls.This

 

onClipEvent (load) {
     angle=8;
     velocity=6;
     thrust=0;
     deceleration=0.99;
     decay=0.7;
}


onClipEvent (enterFrame) {

     if(Key.isDown(Key.UP))
     {
     thrust=1;
     }
     else if(Key.isDown(Key.DOWN))
     {
     thrust=-1;
     }
     else
     {
     thrust*=deceleration;
     }

     if(Key.isDown(Key.RIGHT))
     {
     direction=1;
     }
     else if(Key.isDown(Key.LEFT))
     {
     direction=-1;;
     }
     else
     {
     direction*=decay;
     }

     _rotation+=(angle*direction);
     xdist=Math.cos(Math.PI*_rotation/180)*(velocity*thrust);
     ydist=Math.sin(Math.PI*_rotation/180)*(velocity*thrust);
     _x+=xdist;
     _y+=ydist;

     // Collision Detection with 4 Edges.
     // Object will disappear off one side and reappear
    
// on the opposite side - diagonally opposite!

     if(_x<-30)
     {
     _x=430;
     _y=200-_y;
     }
     if(_x>430)
     {
     _x=-30;
     _y=200-_y;
     }
     if(_y<-30)
     {
     _y=230;
     _x=400-_x;
     }
     if(_y>230)
     {
     _y=-30;
     _x=400-_x;
     }
}

Challenge
Try modifying the code to

 

 

 
     

 

 Download File
    login to download