我有问题,我需要创建计时器,但我想传递一个变量,如何做到这一点? AS3有可能吗?
我试过这样:
bonusPlayer1Timer = new Timer(5000);
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER, bonusChanges(player1));
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER_COMPLETE, bonusChangesRemove(player1));
bonusPlayer1Timer.start();
function bonusChanges(event:TimerEvent, playerBonus:Player):void {
switch (playerBonus.bonus) {
case 0 :
playerBonus.multipleShooting = false;
playerBonus.bonus = -1;
break;
...}}
但我有错误:
1067: Implicit coercion of a value of type Player to an unrelated type flash.events:TimerEvent.
1136: Incorrect number of arguments. Expected 2.
此错误以粗体显示。
我可以这样使用吗?或者我必须为每个玩家创建两个相同的函数,因为我不允许将任何不同的参数传递给计时器函数?
谢谢,
答案 0 :(得分:8)
创建一个扩展Timer类的类,并为Player添加一个属性。
public class PlayerTimer extends Timer
{
public var thePlayer:Player;
public function PlayerTimer(delay:Number, repeatCount:int=0)
{
super(delay, repeatCount);
}
}
使用您的示例代码将是这样的:
bonusPlayer1Timer = new PlayerTimer(5000);
bonusPlayer1Timer.thePlayer = new Player();
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER, bonusChanges);
bonusPlayer1Timer.addEventListener(TimerEvent.TIMER_COMPLETE, bonusChangesRemove);
bonusPlayer1Timer.start();
function bonusChanges(event:TimerEvent):void {
var playerBonus:Player = PlayerTimer(event.target).thePlayer;
switch (playerBonus.bonus) {
case 0 :
playerBonus.multipleShooting = false;
playerBonus.bonus = -1;
break;
...}}
答案 1 :(得分:2)
您可以从不将多个参数传递给EventListener触发的函数。您需要找到传递信息的其他方式,例如Nathan Smith提供的解决方案。
答案 2 :(得分:0)
你也可以这样做:
var x = setTimeout(yourfunction(/*arguments you need*/),1000);
function yourfunction(/*var*/){
//your code
}
答案 3 :(得分:0)
只需键入 var bonusPlayer1Timer = new Timer(5000);