A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Random movement in actionscript 3.0

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    11

    Random movement in actionscript 3.0

    Hi,

    How can i give random movements for a movieclip in actionscript 3.0

  2. #2
    Custom User Title Incrue's Avatar
    Join Date
    Feb 2004
    Posts
    973
    find a random point
    move your movieClip to it
    when he arrives, do it again forever

  3. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    That depends on whether or not you want A) movement in a random angle, or B) just want to bounce it around a random point on stage repeatedly...

    Code:
    // for A)
    //...
    var deg2rad:Number = Math.PI/180;
    var speed:Number = 3; // set to speed you want
    
    someClip.addEventListener(Event.ENTER_FRAME, moveIt);
    
    someClip.rotation = Math.random() * 360;
    var angle:Number = someClip.rotation * deg2rad;
    
    function moveIt(e:Event) {
         e.target.x -= speed*Math.cos(angle);
         e.target.y -= speed*Math.sin(angle);
    }
    // for B)
    // ...
    someClip.addEventListener(Event.ENTER_FRAME, bounceIt);
    
    function bounceIt(e:Event) {
         e.target.x = Math.random()*stage.stageWidth;
         e.target.y = Math.random()*stage.stageHeight;
    }
    Untested.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

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