Johnnien
Johnnien

Reputation: 19

as3 current frame check

I have brick that is hit by ball 2 times on the first hit it goes to frame 2, on the second hit goes to frame 3 and animation plays up to frame 40. Then the current frame property is supposed to execute _root.brickAmt --on frame 40, but it does not work. Can you tell me why? and how can I fix this? Thanks!

if (this.hitTestObject(_root.mcBall)){ 
   if (this.currentFrame == 1){   
   this.gotoAndStop(2)
   } else if (this.currentFrame == 2) {                     
              this.gotoAndPlay(3)
        } 
        if (this.currentFrame == 40) {                     

removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
              _root.brickAmt --;
        }

Upvotes: 0

Views: 1476

Answers (2)

Ignasi
Ignasi

Reputation: 731

As the comments mentioned, writting code on the timeline is not usually a good practice, nevertheless here are a few things to notice:

1- Unless you put your code in every keyframe, it will execute exactly once. 1.1- If you want it to execute every frame without making dozens of keyframes and copypasting it everywhere you should put it inside a function and let this function be called on some listener, ideally on event.enter_frame

2- The if checking if frame is 40, is inside the if checking the hittest, are you sure on frame 40, the ball and the brick are touching?

3- I'm also not sure you are using AS2 or AS3, in AS3 _root should be replaced by MovieClip(root)

EDIT: I couldn't open the .fla because I'm using an older version but accordin to what you said in the comments I'm pretty sure the code should be like this:

private function enterFrameEvents(event:Event):void{
  // [...] previous code
  if (this.hitTestObject(_root.mcBall)){ 
    if (this.currentFrame == 1){   
      this.gotoAndStop(2)
    } else if (this.currentFrame == 2) {                     
      this.gotoAndPlay(3)
    }
  } // <- Notice this
  if (this.currentFrame == 40) {                     
    removeEventListener(Event.ENTER_FRAME, enterFrameEvents);
    _root.brickAmt --;
  }
}

Upvotes: 1

Umur Karag&#246;z
Umur Karag&#246;z

Reputation: 3190

If this code is not inside a function that runs every frame, and there's more than 40 frame, you may miss it passing frame 40. If not, check the timeline of this and make sure nothing else is interrupting it's progress before it reaches frame 40.

You can also put a trace statement into ENTER_FRAME function to see if it stops anywhere between.

// in a function inside the object that runs every frame
trace(this.currentFrame);

Upvotes: 0

Related Questions