动作脚本 3 中的计时器

发布于 2024-11-02 12:51:18 字数 885 浏览 0 评论 0原文

我有一个问题,我需要创建计时器,但我想向它传递一个变量,该怎么做? 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.

并且这个错误在粗体行中。

我可以这样使用它吗?或者我必须为每个玩家创建两个相同的函数,因为我不允许将任何不同的参数传递给计时器函数?

谢谢你,

I have problem that I need to create timer but I want to pass on a variable to it, how to do it? Is it possible in AS3?

I tried like this:

            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;
...}}

But I have error:

1067: Implicit coercion of a value of type Player to an unrelated type flash.events:TimerEvent.
1136: Incorrect number of arguments.  Expected 2.

And this error is in the bold line.

Can I use it in this way? Or I have to create two the same function for every of my players because I am not allow to pass on any different arguments to the timer function?

Thank you,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美的太阳 2024-11-09 12:51:18

创建一个扩展 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;
...}}

Create a class that extends the Timer class and add a property for the Player.

public class PlayerTimer extends Timer
{
    public var thePlayer:Player;

    public function PlayerTimer(delay:Number, repeatCount:int=0)
    {
        super(delay, repeatCount);
    }       
}

Using your example the code would be something like this:

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;
...}}
誰ツ都不明白 2024-11-09 12:51:18

永远不能将多个参数传递到由EventListener 触发的函数中。您需要找到其他方式来传递信息,例如 Nathan Smith 提供的解决方案。

You can never pass more than one argument into the function triggered by an EventListener. You need to find other ways of passing your information around, such as the solution provided by Nathan Smith.

冷默言语 2024-11-09 12:51:18

你也可以这样做:

var x = setTimeout(yourfunction(/*arguments you need*/),1000);
function yourfunction(/*var*/){
    //your code
}

you can also do it this way:

var x = setTimeout(yourfunction(/*arguments you need*/),1000);
function yourfunction(/*var*/){
    //your code
}
初熏 2024-11-09 12:51:18

只需输入
var BonusPlayer1Timer = new Timer(5000);

Just Type
var bonusPlayer1Timer = new Timer(5000);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文