简体   繁体   English

as3当前帧检查

[英]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. 我有一个砖,它在第一个击中被球击中了2次,到达了第2帧,在第二个击中了到帧3,并且动画播放了到第40帧。然后,当前的frame属性应该执行_root.brickAmt-在第40帧上,但是它不起作用。 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 --;
        }

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.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 1.1-如果您希望它执行每个框架而不创建数十个关键帧并将其复制粘贴到任何地方,则应将其放入一个函数中,并让该函数在某个侦听器(理想情况下在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? 2-如果检查框架是否为40,是否在检查命中率的内部,您确定在框架40上,球和砖头是否接触?

3- I'm also not sure you are using AS2 or AS3, in AS3 _root should be replaced by MovieClip(root) 3-我也不确定您使用的是AS2还是AS3,在AS3中,_root应该替换为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: 编辑:我无法打开.fla,因为我使用的是旧版本,但根据您在注释中所说的内容,我很确定代码应该像这样:

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 --;
  }
}

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. 如果这个代码不运行的每一帧的功能里面,有超过40架,你可能会错过它传递框架40.如果没有,检查时间表,并确保没有其他中断它的进度达到帧前40 。

You can also put a trace statement into ENTER_FRAME function to see if it stops anywhere between. 您也可以将trace语句放入ENTER_FRAME函数中,以查看其之间是否停止。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM