Johnnien
Johnnien

Reputation: 19

Collision event twice as3

I have a brick clip that goes to frame 2 when hit by a ball clip. This code is inside the brick class, which is why why it is referred as "this":

if (this.hitTestObject(_root.mcBall)){
    _root.ballYSpeed *= -1;
    this.gotoAndStop(2);
}

My question is when it is hit the second time how can it go to frame 3? What code do I need to add?

Upvotes: 0

Views: 76

Answers (2)

akmozo
akmozo

Reputation: 9839

You can verify the current frame of your brick and then if it's frame 2 go to frame 3, like this :

if (this.currentFrame === 2){     
    this.gotoAndStop(3)
}

You can also use a boolean to indicate if your brick has been hit. If true, go to frame 3.

EDIT

AS code :

- Using a boolean :

...

var hit:Boolean = false

...

if (this.hitTestObject(_root.mcBall)){
    _root.ballYSpeed *= -1
    if(!hit){        // this is the 1st time so set hit to true and go to frame 2
        hit = true
        this.gotoAndStop(2)
    } else {         // this is the 2nd time so go to frame 3
        this.gotoAndStop(3)
    }
}

- Using currentFrame :

if (this.hitTestObject(_root.mcBall)){  
    _root.ballYSpeed *= -1  
    if (this.currentFrame == 1){    // we are in the 1st frame so go to frame 2 
        this.gotoAndStop(2)
    } else {                        // we are certainly not in the 1st frame so go to frame 3
        this.gotoAndStop(3)
    }
}

I hope that is more clearer.

Upvotes: 0

Abraham Hamidi
Abraham Hamidi

Reputation: 13789

Try a "clean" approach, like this:

if (this.hitTestObject(_root.mcBall)){
    _root.ballYSpeed *= -1;
    if (this.currentFrame !== 3) {
        this.nextFrame();
    }
}

This makes the clip go to its next frame if its current frame isn't 3.

Upvotes: 1

Related Questions