Actionscript 3 向事件actionscript 3添加定时延迟?
如果语句完成,我希望它先播放死亡动画帧,而不是立即消失。我该怎么做?这是我的自动取款机代码Actionscript 3 向事件actionscript 3添加定时延迟?,actionscript-3,timer,delay,Actionscript 3,Timer,Delay,如果语句完成,我希望它先播放死亡动画帧,而不是立即消失。我该怎么做?这是我的自动取款机代码 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].hitTestObjec
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();
addEventListener(Event.ENTER_FRAME,move飞弹);
功能(evt:事件){
对于(变量i=1;i所需时间秒*1000){
入侵者x=200000
}
}
停止();
这些是我收到的错误:“场景1,层‘动作’,第1帧,第164行,第181136列:参数数量不正确。预期为2。”,“场景1,层‘动作’,第1帧,第164行,第181067列:隐式强制flash类型的值。事件:事件到不相关的类型字符串。”任何帮助都将不胜感激!“我猜,
入侵者死亡
是死亡动画的帧标签,您将x设置为200000以使敌人消失。如果是这样,不要立即将x设置为200000。在死亡动画的末尾包含另一个帧脚本,以使入侵者消失。让我们开始向您展示一些方法来干燥(D在我们自己身上)
首先,帮你自己一个忙,学习如何使用数组。数组是存储物品集合的一种方式。让我们为你的导弹和入侵者制作一个数组
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];
现在,在enter frame处理程序中,循环遍历导弹阵列中的每个项目:
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
}
}
}
}
现在您已经完成了这项工作,您需要接受的建议,并在死亡动画的末尾添加一个帧脚本
因此,在入侵者死亡动画的最后一帧,添加以下脚本:
this.visible = false;
这将隐藏入侵者,并且由于您已经将其从阵列中移除,因此不再检查其导弹命中率
现在你的干衣机,你也可以添加更多的导弹或入侵者,只需添加更多到你的阵列(无需额外的代码)
如评论中所述,您的错误是因为这一行:
addEventListener(Event(root["missile"].hitTestObject(invader)));
AddEventListener需要一个字符串作为第一个参数,一个回调函数作为第二个参数。您试图给它一个转换为事件的布尔值(真/假),这是不可能的
如果(为了更直接地回答您的问题)您想延迟某个操作,您可以使用或
例如:
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是最好避免的,因为它很容易变得草率和低效。我的第一个例子和Kyle的答案会让你受益匪浅。你的错误来自这一行
addEventListener(事件(根[“导弹])。hitTestObject(入侵者)))
。这不是您使用事件的方式。@lukeet-您找到解决方案了吗?