碰撞事件两次as3
我有一个砖夹,当被一个球夹击中时,它会转到第2帧。此代码是砖类中,这就是为什么它为什么被称为“这个”:碰撞事件两次as3
if (this.hitTestObject(_root.mcBall)){
_root.ballYSpeed *= -1;
this.gotoAndStop(2);
}
我的问题是,当它被击中,第二次怎么能转到第3帧?我需要添加哪些代码?
可以验证砖的当前帧,然后,如果它是帧2去框架3,像这样:
if (this.currentFrame === 2){
this.gotoAndStop(3)
}
你也可以使用一个boolean
来表示,如果你的砖有被击中。如果true
,去框3
编辑
AS代码:
- 使用一个布尔值:
...
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)
}
}
- 使用设置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)
}
}
我希望更清楚。
尝试一个“干净”的方式,像这样:
if (this.hitTestObject(_root.mcBall)){
_root.ballYSpeed *= -1;
if (this.currentFrame !== 3) {
this.nextFrame();
}
}
这使得夹到其下一帧如果当前帧不3.
可能更好一些,谢谢!但我需要再次打到第3帧,而不是在第2帧时自动打印。 – Johnnien 2014-11-02 14:55:54
@Johnnien第2次打到第2帧时,第2次打开第3帧......如果你不明白你至少可以试用它的代码......我不能用这段代码破坏你的系统,我可以吗? ;)无论如何,它基本上是在每次击中它时都会说“如果帧不是3,就去下一帧”。 – Cilan 2014-11-03 13:52:59
对不起!我不想听起来很有意思.....在我写信给你之前,我确实尝试了它,然后直接进入第3帧。无论如何。谢谢! – Johnnien 2014-11-03 22:41:10
谢谢!我不太了解布尔人。你能给我一个例子吗? – Johnnien 2014-11-02 15:01:29
我试过了:if(this.hitTestObject(_root.mcBall))&& \t \t if(this.currentFrame == 2); { this.gotoAndStop(3);它不会工作,你知道有没有像我可以使用的东西? – Johnnien 2014-11-02 15:05:24
你的代码应该是这样的:'if(this.hitTestObject(_root.mcBall)&& this.currentFrame == 2){this.gotoAndStop(3); ''。请注意,这只是对您的代码的更正。 – akmozo 2014-11-02 17:32:42