我希望它首先播放死亡动画帧,而不是在 if 语句完成后立即消失,我该怎么做?这是我的 atm 代码。
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,“ Action ”层,第 1 帧,第 164 行,第 18 列 1136:参数数量不正确。预期为 2。”、“场景 1,“ Action ”层,第 1 帧,第 164 行,第 18 列 1067:将 flash.events:Event 类型的值隐式强制转换为不相关的 String 类型。”任何帮助将不胜感激!”
最佳答案
首先让我们向您展示一些DRY的方法(Don't 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 已经完成,您还可以通过简单地向阵列添加更多导弹或入侵者来添加更多导弹或入侵者(无需额外代码)。
正如评论中提到的,您的错误是因为这一行:
addEventListener(Event(root["missile"].hitTestObject(invader)));
AddEventListener 需要一个字符串作为它的第一个参数,以及一个回调函数作为第二个参数。您试图为其提供一个转换为事件的 bool 值(真/假) - 这是不可能的。
如果(为了更直接地回答您的问题)您想延迟操作,您可以使用 flash.utils.setTimeout或 Timer .
例如:
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,因为它很容易变得草率和低效。通过我的第一个示例并遵循凯尔的回答,您会感觉更好。
关于actionscript-3 - 向事件 ActionScript 3 添加定时延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47594715/