A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: What's wrong with this animation code?

Hybrid View

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    16

    What's wrong with this animation code?

    Being the 'pro' I am I always think I have the solution, only to keep running into problems. The following AS3 code is supposed to shrink a ball to no less than 15 px, and once it reaches 15px, it's supposed to grow to no bigger than its original size, and then shrink again, grow again, etc.

    Actionscript Code:
    var originalsize:Number=ball_mc.size; [I]//original size of ball to be animated[/I]

    ball_mc.addEventListener(Event.ENTER_FRAME, grow_shrink);

    function grow_shrink() { [I]//holds both shrink and grow functions[/I]

        function start_shrink() {

                       ball_mc.size-=5; //shrink by 5 per onEnterFrame
                       
                        if(ball_mc.size<=15) {
                          ball_mc.size=15 [I]//not to shrink below 15px[/I]
                  start_grow(); [I]// if ball reaches 15 in size, call start_grow()[/I]
                       }
               }
           
         }
       
        function start_grow() {
                    ball_mc.size+=5;  [I]//grow by 5 per onEnterFrame[/I]
                   
            if(ball_mc.size>=originalsize) {
                   ball_mc.size=originalsize [I]//not to exceed original size[/I]
                       start_shrink() [I]//call shrink function above[/I]
       
    }


         }
    }


    First of all, the ball shrinks to only 20px not 15 as it's supposedly programmed to. And after that it never grows back to original size as intended, but keeps getting resided randomly between 18 and 22px, as I'm tracing it. Where is the logic here wrong?
    I've tried other variations, all have failed with the objective. Plus I'm getting this output
    "18.6
    ArgumentError: Error #1063: Argument count mismatch on HT_fla::text_2/grow_shrink(). Expected 0, got 1."




    Also, the[i] I've no idea why they're showing, the italic was meant to separate comments better, but created more mess instead.
    Last edited by flashhog01; 11-23-2011 at 04:51 AM.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You've added growShrink as an event listener, but it does not take an event argument. All event listeners must take a single argument of type Event (or some more specific subclass like MouseEvent).

    Even if you had correctly declared growShrink, it doesn't do anything. It defines two locally-scoped functions, and does not call them. Those functions are not defined outside of growShrink, so nothing else can call them either. And even worse, those locally-scoped functions are created over and over again each frame because growShrink is an enter_frame listener!

    I'm not sure what size is supposed to be, but I'll give you the benefit of the doubt and assume you've set up a getter/setter function pair that calculates some value and sets a scaling factor respectively.

    Throw it all away.

    Code:
    ball_mc.addEventListener(Event.ENTER_FRAME, shrink);
    function shrink(e:Event):void {
      var b:DisplayObject = DisplayObject(e.currentTarget); 
      if (b.width <= 15){
        b.removeEventListener(Event.ENTER_FRAME, shrink);
        b.addEventListener(Event.ENTER_FRAME, grow);
      }else{
        b.scaleX = b.scaleY = b.scaleY*0.9;
      }
    }
    
    function grow(e:Event):void {
      var b:DisplayObject = DisplayObject(e.currentTarget); 
      if (b.scaleX >= 1){
        b.removeEventListener(Event.ENTER_FRAME, grow);
        b.addEventListener(Event.ENTER_FRAME, shrink);
      }else{
        b.scaleX = b.scaleY = b.scaleY*1.1;
      }
    }

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Ok Thanks. But what's weird, even though I do see the point of the function not having an event as argument, the target object does shrink!

    It just doesn't grow, but once shrunk to about 20px (and not 15 as intended for some reason, which it never reaches) it gets resized randomly to sizes between about 18 and 21, and keeps at that.


    I also see the point of declaring the functions over and over. In fact, previously I tried declaring the grow and shrink functions separately, and then have the main function (grow_shrink()) just call them rather than construct them. But the code didn't do what's intended.

    The add and remove event listener seems like a good solution.

    How does flash calculate currentTarget though, since it's not a mouse event?

    Anyway, I'll try that.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't see how the code you posted would ever do anything. Are you sure that there wasn't other code that was doing something?

    currentTarget is calculated in this case in the same way it's calculated in every other case: it is the instance you added the listener to.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    16
    Perhaps I didn't save in between? It seems the script 'cache' doesn't clear unless you save new changes, I just noticed, as the trace statement kept executing even though I'd erased it from the AS panel.

    Just a quick question. Should you place all AS code on the main time-line? Currently I've one set of AS code on the main time line for one object, and another set of AS code for the ball on its own time-line. After I changed your grow/shrink numbers, the animation went whack (objects on the main time line flip and disappear), so wondering if that could be it. Thanks, your code does grow shrink the object btw.

Tags for this Thread

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