而不是在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,图层'动作',第1帧,第164行,第18列1136:参数数量不正确。预期2.”,“场景1,图层'动作',第1帧,第164行,第18列1067:将flash.events:Event类型的值隐式强制转换为不相关的类型字符串。“任何帮助将不胜感激!“
答案 0 :(得分:2)
让我们首先向您展示一些 DRY 的方式( D on't R epeat 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期望一个字符串作为它的第一个参数,一个回调函数作为第二个参数。您试图给它一个转换为事件的布尔值(true / false) - 这是不可能的。
如果(为了更直接地回答您的问题)您想延迟某项操作,则可以使用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,因为它很容易变得邋and和低效。我的第一个例子和Kyle的回答让你好多了。
答案 1 :(得分:1)
我猜Invader Dead
是死亡动画帧标签,你将x设置为200000以使敌人消失。如果是这种情况,请不要立即将x设置为200000。在死亡动画结束时包含另一个框架脚本,使入侵者消失。