AS3使用多个计时器显示/隐藏的敌人多个、计时器、敌人

2023-09-08 15:20:57 作者:绘川绮

我有一个射击游戏,带有某种轮的工作,比如,这一轮是要呈现4个敌人在屏幕上,用户必须拍摄。我表现出4个敌人以半秒的时间间隔每一个因此它不都出现在同一时间。使用这样的:

i have a shooting game that works with some sort of rounds, for instance, this round is gonna show 4 enemies on the screen that the user has to shoot. I show the 4 enemies at a half seconds interval of each one so it doesnt all appear on the same time. Using something like:

enemiesShowTimer = new Timer(0.5 * 1000, 1);
enemiesShowTimer.addEventListener(TimerEvent.TIMER, showEnemyAtTime);
enemiesShowTimer.start();

玩家有2秒杀掉每个敌人出现后,所以我也用这个:

The player has 2 seconds to kill each enemy after they appear, so i also use this:

enemiesCleanTimer = new Timer(roundConfig.getSecondsPerEnemy() * 1000, 1);
enemiesCleanTimer.addEventListener(TimerEvent.TIMER, cleanEnemies);
enemiesCleanTimer.start();

现在的问题是,在玩家死亡后,我改变场景,如果我仍然有像3活着,敌人,线程将尝试运行的方法,它会崩溃。我使用的总是相同的变量来启动计时器,每当我需要它。我怎样才能解决这个问题?我将不得不存储每个线(定时器)名单上,然后分别停止每一个?因为事情是这样的,其他的线程都处于某种冷宫,而我不能停止的话,仅仅是最后一个。

The problem is, after the player dies i change scene and if i still have like 3 alive, enemies, the threads will try to run the methods and it will crash. I'm using always the same variable to the start the timer whenever i need it. How can i solve this? Will i have to store each "thread" (timer) on a list and then stop each one separately? Because the way it is, the other threads are in some sort of "limbo" and i cant stop then, just the last one.

任何帮助?

感谢您!

推荐答案

通常在一场比赛的情况下,为了避免多个计时器(和运行它们的开销/性能损失),就不会有持续运行一个计时器(如只要游戏激活),被称为像GameLoop和对象将注册/根据需要与注销。

Typically in a game situation, to avoid multiple timers (and the overhead/performance hit of running them), there would be a single timer that runs continuously (as long as the game is active), called something like GameLoop and objects will register/deregister with that as needed.

顺便说一下,这还提供了一种简单的方法来暂停游戏。

Incidentally, this also provides a simple way to pause a game.

我敢肯定有一个很好的理由,但如果你覆盖AS3计时器例如,previous定时器将继续没有办法无限期运行,以阻止它(为它的指针会被覆盖) 。所以,你要停止任何计时器您覆盖之前,因为泰勒提到的。

I'm sure there's a good reason for this, but if you overwrite a Timer instance in AS3, the previous timer will continue to run indefinitely with no way to stop it (as its pointer will have been overwritten). So you'll want to stop any timers before you overwrite them, as Tyler mentioned.