Optimizing transition/movement smoothness for a 2D flash game

后端 未结 8 1121
星月不相逢
星月不相逢 2020-12-10 22:42

Update 6:

Fenomenas suggested me to re-create everything as simple as possible. I had my doubts that this would make any difference as the algorithm

8条回答
  •  萌比男神i
    2020-12-10 23:11

    This is a pretty good question. I've scanned the code and I have a few suggestions, although my advice might not be that good.

    I'm thinking you can do a lot to optimize the code. Obviously, not at this early stage. But you can have a test with that code and see fast it runs with the optimized code, then you'll know if it's worth continuing.

    Here are my 'objections':

    • You use a lot of divisions. Division is more expensive than multiplication.

      var distance:Number = (newTimeStamp - movementTimeStamp) / 1000 * movementSpeed;

    can be easily written as

    var distance:Number = (newTimeStamp - movementTimeStamp) * .001 * movementSpeed;
    
    • You have a LOT of references to a lot of functions.

      Stuff like fixAngle() and so on can be inside the same function, without having calls running back and forth that often. This applies to references to external classes and Math.PI or Math.sin and so on, as fenomas and Allan pointed out.

    I've tested this method for sine and cosine and it's bloody fast. Sure it makes the code dirty, but that's why you don't optimize this soon, until you got most of it working the way it needs to work, optimizing will only make you go nuts as the code will get harder to read. From my experience sin and cos are pretty expensive operations.

    As the others already mentioned, you might be worring too much at this step. Keep in mind there are a lot of things you can gain speed on, until you got all your logic working properly, don't even think about optimizing.

提交回复
热议问题