【问题标题】:adding a timed delay to an event actionscript 3?向事件动作脚本 3 添加定时延迟?
【发布时间】:2017-12-02 04:54:10
【问题描述】:

而不是在 if 语句完成后立即消失,我希望它首先播放死亡动画帧,我该怎么做?这是我的自动取款机。

    addEventListener(Event.ENTER_FRAME, moveMissile);
function moveMissile(evt:Event) {
   for (var i=1; i<=missileCount; i++) {
      root["missile"+i].y+=-30;
      if (root["missile"+i].hitTestObject(invader)) {
          invader.gotoAndStop("Invader Dead");
      }
      if (root["missile"+i].hitTestObject(invader2)) {
          invader2.gotoAndStop("Invader Dead");
          invader2.x=200000
      }
      if (root["missile"+i].hitTestObject(invader3)) {
          invader3.gotoAndStop("Invader Dead");
          invader3.x=200000
      }
      if (root["missile"+i].hitTestObject(invader4)) {
          invader4.gotoAndStop("Invader Dead");
          invader4.x=200000
      }
   }
}   
var myStartTime = getTimer();
var requiredTimeSeconds = 5;
addEventListener(Event(root["missile"].hitTestObject(invader)));
function playGame(e:Event) {
    var tempTime = getTimer() - myStartTime;
    if (tempTime  >  requiredTimeSeconds * 1000) {
        invader.x=200000
    }   
}       
stop();

这些是我收到的错误“场景 1,‘动作’层,第 1 帧,第 164 行,第 18 1136 列:参数数量不正确。预期为 2。”,“场景 1,‘动作’层,第 1 帧,第 164 行,第 18 列 1067:将 flash.events:Event 类型的值隐式强制转换为不相关的 String 类型。”任何帮助将不胜感激!”

【问题讨论】:

  • 你的错误来自addEventListener(Event(root["missile"].hitTestObject(invader)));这一行。这不是您使用事件的方式。
  • @lukeet - 你找到解决方案了吗?

标签: actionscript-3 timer delay


【解决方案1】:

让我们首先向您展示一些DRY的方法(D不要R重复Y我们自己)

首先,帮自己一个忙,学习如何使用数组。数组是一种存储事物集合的方式。让我们为你的导弹做一个阵列,为你的入侵者做一个。

var missles:Array = [missile1, missile2, missile3, missile4]; //populate this with your missiles, even DRYer would be to create these missiles in a for-loop programatically

var invaders:Array = [invader1, invader2, invader3, invader4];

现在,在您的输入帧处理程序中,循环遍历导弹数组中的每个项目:

addEventListener(Event.ENTER_FRAME, moveMissile);
function moveMissile(e:Event):void {
    //arrays are 0 based, which means the first item is accessed with missles[0]
    //iterate backwards if you are going to potentially remove something from the array during the loop
    //so if there were 4 missiles, we iterate backwards from 3 to 0
    //before the loop, we give it a label: MissileLoop: , this gives you a way to exit the missile loop early if needed
    MissileLoop: for(var i:int=missiles.length-1;i>=0;i--){
        //move the missile
        missiles[i].y += 30;

        //iterate through each invader to check for a hit
        //iterate backwards so if you remove an item it doesn't throw off the iterator (j)
        for(var j:int=invaders.length-1;j>=0;j--){
            if(missile[i].hitTestObject(invaders[j])){
                //play the death animation
                invaders[j].gotoAndPlay("Invader Death");

                //if there was a hit, remove the missile from the array 
                //so it doesn't keep checking for hits on subsequent frame ticks
                invaders.removeAt(j); 

                //you may want to also remove the missile (if it can only destroy one invader)
                missiles[i].gotoAndPlay("Missile Explodes");
                missiles.removeAt(i);

                //this tells the outer loop which you labelled 'MissileLoop' to move on to the next item early (so we don't keep checking hittests with the other invaders)
                //only do this if you destroy the missile, if missiles can not be destroyed, omit this part
                continue MissileLoop;  //basically this says don't keep looping through the other invaders, as the missile is destroyed
            }
        }
    }
}

现在你已经完成了,你需要听取@Kyle's answer 的建议,并在你的死亡动画末尾添加一个帧脚本。

所以在入侵者死亡动画的最后一帧,添加以下脚本:

this.visible = false;

这将隐藏入侵者,并且由于您已将其从阵列中移除,因此将不再检查是否有导弹击中。

现在您的 DRYer,您还可以通过简单地向您的阵列添加更多来添加更多导弹或入侵者(无需额外代码)。


正如 cmets 中所述,您的错误是因为这一行:

addEventListener(Event(root["missile"].hitTestObject(invader)));

AddEventListener 需要一个字符串作为它的第一个参数,以及一个回调函数作为第二个参数。您正试图给它一个转换为事件的布尔(真/假)值 - 这是不可能的。

如果(为了更直接地回答您的问题)您想延迟某个操作,您可以使用flash.utils.setTimeoutTimer

例如:

if(root["missile"+i].hitTestObject(invader4)){
    invader4.gotoAndStop("Invader Dead");

    //call the function hideInvader 2 seconds (2000 milliseconds) from now, and pass the parameter invader4 to it
    flash.utils.setTimeout(hideInvader, 2000, invader4);
}

function hideInvader(invader:DisplayObject):void {
    invader4.x = 200000;
}

那个例子说,setTimeout 最好避免,因为它很容易变得草率和低效。使用我的第一个示例并遵循凯尔的回答,您会好得多。

【讨论】:

    【解决方案2】:

    我猜Invader Dead 是死亡动画帧标签,您将 x 设置为 200000 以使敌人消失。如果是这种情况,请不要立即将 x 设置为 200000。在死亡动画的末尾添加另一个帧脚本,使入侵者消失。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-05
      • 2014-01-26
      • 1970-01-01
      • 2020-09-22
      • 2014-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多