A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Stuck trying to animate mc's - see code

  1. #1
    Member
    Join Date
    Jul 2009
    Posts
    69

    [RESOLVED] Stuck trying to animate mc's - see code

    Here's the full code, basically there are four shapes in the library. Something's not working in my loop and/or my animateShapes function. The goal is to have the shapes in the loop placed on stage in random colors/transparencies and move around randomly on the stage (varying velocity) while staying within the stage boundaries. Right now, I have one little shape (the first one in the loop, I think) making an anemic motion and the rest don't move at all.

    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.text.*;

    var tl:MovieClip = new Topleft();
    var tr:MovieClip = new Topright();
    var bl:MovieClip = new Bottomleft();
    var br:MovieClip = new Bottomright();

    var myShapes:Array = [tl, tr, bl, br];
    //trace(myShapes);

    var xVelocity: Number = 0;
    var yVelocity:Number = 0;


    var long:uint = myShapes.length;
    for (var i:uint = 0; i < long; i++){
    var shape:MovieClip = myShapes[i];
    shape.alpha = 0.3 + Math.random() * 0.9;

    var colorInfo:ColorTransform = shape.transform.colorTransform;
    colorInfo.color = 0xffffff * Math.random();
    shape.transform.colorTransform = colorInfo;

    shape.velX = Math.random()*10-5
    shape.velY = Math.random()*10-5

    shape.posX = shape.x = Math.random() * stage.stageWidth;
    shape.posY = shape.y = Math.random() * stage.stageHeight;
    addChild(shape);

    shape.addEventListener(Event.ENTER_FRAME, animateShapes);
    }

    function animateShapes(evt:Event):void
    {
    var s:MovieClip = MovieClip(evt.currentTarget);
    shape.posX += shape.velX;
    shape.posY += shape.velY;

    if(shape.y <= stage.stageHeight-shape.length/2){
    shape.posY -= shape.velY;}
    if(shape.x <= stage.stageWidth-shape.width/2){
    shape.posX -= shape.velX;}
    shape.x = shape.posX;
    shape.y = shape.posY;
    }
    Last edited by Leftyplayer; 09-10-2009 at 04:13 PM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    In animateShapes, you need to either use s instead of shape, or make the first line redeclare shape instead of s. As it is now, it's only manipulating the last shape through the previous loop.

  3. #3
    Member
    Join Date
    Jul 2009
    Posts
    69
    Thanks, yes, that was a typo I overlooked. I made a few changes and things are working fine, EXCEPT, my code for the velocity is only sending the shapes in one direction (toward the right), and my attempt to keep the shapes from going off-stage is not working - I'd like the shapes to slightly bounce back from the wall boundaries in a random direction. Will be working on that this morning, if anyone has any suggestions. I've got the shapes staying on the stage (well, just the right-side boundary of the stage from what I can tell), but they basically crawl right along it once they hit it, rather than come back in.

    New code for the animateShapes function is:

    function animateShapes(evt:Event):void {
    var shape:MovieClip = MovieClip(evt.currentTarget);
    shape.posX += shape.velX;
    shape.posY += shape.velY;

    if(shape.y >= stage.stageHeight-shape.length/2){
    shape.velY = shape.velY * Math.random()*20+5;}
    if(shape.x >= stage.stageWidth-shape.width/2){
    shape.velX -= shape.velX;}
    shape.x = shape.posX;
    shape.y = shape.posY;
    }

  4. #4
    Member
    Join Date
    Jul 2009
    Posts
    69
    Ok, I corrected this:

    shape.velX = (Math.random()*20)-10
    shape.velY = (Math.random()*20)-10
    instead of "10" I had a smaller number there before, which resulted in a bias in the random numbers created, thus shapes going mostly in one direciton.

    Still having trouble with this ... my understanding of Math.random is clearly not good ... off to google:

    if(shape.y >= stage.stageHeight-shape.length/2){
    shape.velY = shape.velY*Math.random()*20+5;}

    if(shape.y <= stage.stageHeight-shape.length/2){
    shape.velY = shape.velY*Math.random()*20+5;}

    if(shape.x >= stage.stageWidth-shape.width/2){
    shape.velX -= shape.velX*Math.random()*20+5;;}

    if(shape.x <= stage.stageWidth-shape.width/2){
    shape.velX -= shape.velX*Math.random()*20+5;;}
    yes, I'm at Actionscript for dummies level : ) ... gotta start somewhere. Thanks for any help.
    Last edited by Leftyplayer; 09-10-2009 at 10:56 AM.

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