English 中文(简体)
优化速度——更好?
原标题:Speed optimize - which is better?

让我们说,我们建立了一种游戏,我们有角色。 游戏中的这种特性将采取许多行动,如步行、跳跃、打猎、搜查目标等:

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.

最佳回答

也许,你应当集中精力,使其工作达到你所希望的方式(灵活、易于维持和修改),而不是使之尽可能快。 这不可能成为真正的瓶颈。

Premature optimization is the root of all evil, and all that.

问题回答

暂无回答




相关问题
Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签