A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [f8] Modify AS to add speed variable ?

  1. #1
    Member
    Join Date
    Jan 2003
    Posts
    85

    [f8] Modify AS to add speed variable ?

    I got this code on these forums. I use it for moving a movieclip at random directions inside a given boundary.

    I would like to add a speed variable cuz I think it moves to fast as it is now at my framerate.
    Can anyone help me with that ?

    Code:
    onClipEvent (load) {
    //define direction function
    function changeDirection(){
    this.dx = Math.random()*2-1;
    this.dy = Math.random()*2-1;
    }
    
    // set boundaries
    this.xMax = this._x+60;
    this.xMin = this._x;
    this.yMax = this._y+20;
    this.yMin = this._y;
    }
    
    onClipEvent (enterFrame) {
    
    //if at boundaries, invert direction
    if(this._x + this.dx > this.xMax || this._x + this.dx < this.xMin){
    this.dx = -this.dx;
    }
    if(this._y + this.dy > this.yMax || this._y + this.dy < this.yMin){
    this.dy = -this.dy;
    }
    
    this._x += this.dx
    this._y += this.dy
    
    if (Math.random()>.9) {
    changeDirection();
    }
    }
    Thanks...

  2. #2
    Senior Member
    Join Date
    Apr 2005
    Location
    FL, USA
    Posts
    443
    It already has speed variables. Change the values of dx and dy for xspeed and yspeed, respectively.

  3. #3
    Member
    Join Date
    Jan 2003
    Posts
    85
    Yes I should have been able to see that - thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center