Link to home
Start Free TrialLog in
Avatar of Brian Lin
Brian LinFlag for United States of America

asked on

Random moving within stage are v2

Hi, Experts

I try to make a MC to move randomly. How do I make it move only within the stage area ? if it is possible to modify the code      speed = random(5)+8;

Thanks

Fla @ http://www.4shared.com/file/8344914/f5d6fcae/jet_perspective_fly_test.html

Code:

function changeDirection() {
     trace("Here is change");
     // X and Y within the Stage area
     //newX = random(Stage.width-_root.jet_img.ene_jet._width)+_root.jet_img.ene_jet._width*20;
     //trace("New X = "+newX);
     // left and right
     dirX = Math.floor(Math.random()*4);
     trace("X Direction = "+dirX);
     // top and down
     dirY = Math.floor(Math.random()*4);
     trace("Y Direction = "+dirY);
     // Speed
     speed = random(5)+8;
     trace("Speed"+speed);
}
intervalId = setInterval(this, "changeDirection", 300);
mouseListener = new Object();
_root.onEnterFrame = function() {
     // X
     if (dirX == 0) {
          // Down
          _root.jet_img.ene_jet._x = _root.jet_img.ene_jet._x-speed;
          _root.jet_img.ene_jet.gotoAndPlay("left_turn");
          trace("Jet X Position = "+_root.jet_img.ene_jet._x);
     } else if (dirX == 1) {
          // Up
          _root.jet_img.ene_jet._x = _root.jet_img.ene_jet._x+speed;
          _root.jet_img.ene_jet.gotoAndPlay("right_turn");
     }
     // Y                                                          
     if (dirY == 0) {
          // left
          _root.jet_img.ene_jet._y = _root.jet_img.ene_jet._y-speed;
     } else if (dirY == 1) {
          // Right
          _root.jet_img.ene_jet._y = _root.jet_img.ene_jet._y+speed;
     }
};
ASKER CERTIFIED SOLUTION
Avatar of Aneesh Chopra
Aneesh Chopra
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial