让我们说,我们建立了一种游戏,我们有角色。 游戏中的这种特性将采取许多行动,如步行、跳跃、打猎、搜查目标等:
public class Player extends MovieClip
{
public function Player(){}
private function walk(){}
private function attack(){}
private function jumping(){}
...
..
.
private function update(){}
}
Now, for every state or action we want to execute a different code. Now, how to do this? I can think of 3 ways.
1.With events - as I understand, event system is costly to use, so I ll skip this one. 2. With simple checks in the update() - something like if(currentStateAction) {doSomethingHere} or with switch statement. But what if the actions are not mutually exclusive, e.g. it can jump and attack in the same time? Than we have to use if() for every action, e.g. to do checks for every frame. 3. I came up with this solution on my own and I need to know is this better than option number 2:
//declare Dictionary. Here we will add all the things that needs to be done
public var thingsToDo:Dictionary = new Dictionary(true);
//in the update function we check every element of the dictionary
private funciton update():void
{
for each(item:Function in thingsToDo)
{
item.call();
}
}
Now, every time we need to do something, we add a element, like thingsToDo[attack]=attack;
Or if we don t want to do something anymore: delete thingsToDo[attack];
在这一体系中,......你不需要检查每个目标应该做什么,而只是检查需要什么时候。 它像一个事件驱动系统一样。 例如,在新的敌人pa起之前,物体不必检查攻击。 因此,当一个新敌人停下来时,你将增加ToDo[attack]=攻击等物品。
So the question: Is this faster than option number 3? Any idea how to do this checking differently? Thanks.