0
votes

I've spent 14 hours on this problem. This is a basic collision checker that sets a MovieClip animation to play when a collision occurs. The clip is

currentBallObject.clip.

It works. The clips plays. But it repeats over and over.

private function checkCollisions():void
        {
            var i = balls.length;
            while (i--)
            {
                var currentBallObject = balls[i];



                if (currentBallObject.contact)
                { 

                    //condition hits ground
                    if (currentBallObject.ground)
                    {

                    currentBallObject.body.SetPosition(new b2Vec2(currentBallObject.startX / PIXELS_TO_METRE, currentBallObject.startY / PIXELS_TO_METRE));
                    currentBallObject.body.SetLinearVelocity(new b2Vec2(0, 0));
                    currentBallObject.body.SetAngularVelocity(0);

                    //currentBallObject.texture.pop();                                              
                    }
                    else // it hit player
                    {


                            // assign clip texture to current position  
                            currentBallObject.clip.x = currentBallObject.body.GetPosition().x * PIXELS_TO_METRE;
                            currentBallObject.clip.y = currentBallObject.body.GetPosition().y * PIXELS_TO_METRE;

                            // whisk old object away
                            currentBallObject.body.SetPosition(new b2Vec2(currentBallObject.startX / PIXELS_TO_METRE, currentBallObject.startY / PIXELS_TO_METRE));
                            currentBallObject.body.SetLinearVelocity(new b2Vec2(0, 0));
                            currentBallObject.body.SetAngularVelocity(0);
                            currentBallObject.contact = false;


                    }

                }
            }
        }

I added this code to delete the MovieClip or somehow get rid of it after it has played through once. (42 frames). I also tried to add a frameListener and at least a dozen other suggestions. When I add

stop()

The animation doesn't play. It just loads the last frame. The code I have now is:

private function updateClips():void
        {
            var i = balls.length;
            while (i--)
            {
                var currentBallObject = balls[i];

                if(currentBallObject.clip)
                {


                   var frame:int = currentBallObject.clip.currentFrame;
                    //trace(currentBallObject.clip.currentFrame);
                    if(frame == 42)

                      {

                          currentBallObject.clip._visible = false;
                          currentBallObject.clip.removeMovieClip();
                          currentBallObject.clip.enabled = -1;



                      }

                }
            }
        }

I've tried counting the frames, putting it a run-once function, a frame exit listener, I am out of ideas. I just want to make a MovieClip run one time through. I also tried putting stop() in the timeline and then the animation didn't play. It just loaded the last frame.

Right now the collisions work but the animations stay on the screen forever, looping forever.

Edit: I got the code by Patrick to run without errors.

I added the event listener with the others like this:

_input = new Input(stage);
                    ...
                    addEventListener(Event.ENTER_FRAME, oEF);
                    addEventListener(Event.ENTER_FRAME, update);
                    time_count.addEventListener(TimerEvent.TIMER, on_time);
                    time_count.start();
                }

And then created a function:

private function oEF(e:Event):void{

            var i = balls.length;
            while (i--)
            {
                var currentBallObject = balls[i];



            if (currentBallObject.clip.currentFrame >= currentBallObject.clip.totalFrames)
            {
              currentBallObject.clip.stop();
              currentBallObject.clip.removeEventListener(Event.ENTER_FRAME, oEF);
              if (currentBallObject.clip.parent) currentBallObject.clip.parent.removeChild(currentBallObject.clip);
            }
        }
        }

But I still get the same problem as any other result. The MovieClip disappears on contact without the animation happening.

Through debugging I've learned more. The value of currentFrame starts off going 1-40 then stays at 40 for the rest of the execution. So the MovieClip is always on the last frame for every object.

2

2 Answers

0
votes

if clip is a MovieClip then you can use clip.totalFrames in an enterframe listener.

function oEF(e:Event):void{
    if (this.currentFrame >= this.totalFrames){
      this.stop();
      this.removeEventListener(Event.ENTER_FRAME, oEF);
      if (this.parent) this.parent.removeChild(this);
    }
}

this.addEventListener(Event.ENTER_FRAME, oEF);
0
votes

I figured it out.

if (currentBallObject.clip.currentFrame == currentBallObject.clip.totalFrames)

                   {
                       trace("Frame before kill: ", currentBallObject.clip.currentFrame);
                       currentBallObject.clip.x = 0;
                       currentBallObject.clip.y = 0;                               
                   }

The above block goes right after

var currentBallObject = balls[i];

It checks if the movieClip is on the last frame and then gets rid of the clip by setting clip.x & clip.y to 0. I could find no other way to stop the movie in this particular case.