Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to optimize the performance of this code ?

Guest
Apr 13, 2015 Apr 13, 2015

I've two Movie clips on a flash project. One of them is fixed and the other can be moved by arrow buttons on the keyboard. The two Movie clips have irregular shapes, so HitTestObject and HitTestPoint doesn't work very well. I've a function that detects collision of two movie clips using bitmap. I wanted to update the position of the movable Movie clip so I put the collision detection function under the code of ENTER_FRAME event listener. It works very well, but when I add many fixed movie clips  ( about 10 fixed movie clips in one frame ), the game (.swf file) becomes slower and the performance of the PC becomes slower. I thought that my collision detection function has a negative effect on PC performance so I used the class on this page : https://forums.adobe.com/thread/873737
but the same thing happens.

Would you tell me how to speed up the execution of my codes ?

Here is part of my code :

stage.addEventListener(Event.ENTER_FRAME, myOnEnterFrame);

function myOnEnterFrame(event:Event):void

{

  if (doThisFn) // doThisFn is a variable to allow or prevent the movable movie clip form being moved with keyboard arrows

  {

  if ( left && !right ) {

  player.x -= speed;

  player.rotation = player.rotation - speed ;

  }

  if( right && !left ) {

  player.x += speed;

  player.rotation = player.rotation + speed ;

  }

if( up && !down ) {

  player.y -= speed;

  }

if( down && !up ) {

  player.y += speed;

  }

// The fixed movie clips are wall1 ,wall2 , wall3 , wall4 , ... and so on
// the following code checks how many walls exists on each frame and pushes them into the wallA  array

  for(var i:int=0;i<1000;i++) // We can put up to 1000 wall object into the wallA array

  {

  if(this['wall'+i]) // If the object wall exists, push it into the wallA array

  {

  wallA.push(this['wall'+i]);

  }

  }

  for(i=0;i<wallA.length;i++)

  {

  if( h.hitF (player , wallA ) || gameOverTest ) // This code checks if the player ( the movable movie clip ) hits the walls or not

  {

  trace ( "second try" ) ;

  gameOver.visible = true ;

  doThisFn = false ;

  }

  }

//I think the following codes are easy to excite and run. I think the performance issue is due to previous codes.


  if (player.hitTestObject(door))

  {

  win.visible = true ;

  doThisFn = false ;

  }

  if (key) // if there is a key on frame

  {

  if (player.hitTestObject(key))

  {

  key.visible = false ;

  switch( currentFrame )

  {

  case 4:

  wallA[0].visible = false ;

  wallA[0].x = 50000;

  break;

  case 5:

  wall14.play();

  wall8.x = 430 ;

  break;

 

  }

  }

  }

  }

}

TOPICS
ActionScript
443
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 14, 2015 Apr 14, 2015

it's a simple question that usually has no simple answer.

here's an excerpt from a book (Flash Game Development: In a Social, Mobile and 3D World)  i wrote.

Optimization Techniques

Unfortunately, I know of no completely satisfactory way to organize this information. In what follows, I discuss memory management first with sub-topics listed in alphabetical order. Then I discuss CPU/GPU management with sub-topics listed in alphabetical order.

That may seem logical but there are, at least, two problems

...
Translate
Community Expert ,
Apr 13, 2015 Apr 13, 2015

for movieclips that are only moving (ie, not scaling, rotating, fading), enable their cacheAsBitmap property.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 13, 2015 Apr 13, 2015

Thank you for your answer, "player" movie clip has a "blur" filter and "adjust color" filter. When it moves using arrows, it also rotates. Some of the fixed movie clips ( walls )  are scaled during the run time of my game. There are too many flash games on the internet that use collision detection with good performance. I would like to know how this can be done, please ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2015 Apr 14, 2015
LATEST

it's a simple question that usually has no simple answer.

here's an excerpt from a book (Flash Game Development: In a Social, Mobile and 3D World)  i wrote.

Optimization Techniques

Unfortunately, I know of no completely satisfactory way to organize this information. In what follows, I discuss memory management first with sub-topics listed in alphabetical order. Then I discuss CPU/GPU management with sub-topics listed in alphabetical order.

That may seem logical but there are, at least, two problems with that organization.

1. I do not believe it is the most helpful way to organize this information.

2. Memory management affects CPU/GPU usage, so everything in the Memory Management section could also be listed in the CPU/GPU section.

Anyway, I am going to also list the information two other ways, from easiest to hardest to implement and from greatest to least benefit.

Both of those later listings are subjective and are dependent on developer experience and capabilities, as well as, the test situation and test environment. I very much doubt there would be a consensus on ordering of these lists.  Nevertheless, I think they still are worthwhile.

Easiest to Hardest to Implement

1.  Do not use Filters.

2.  Always use reverse for-loops and avoid do-loops and avoid while-loops.

3.  Explicitly stop Timers to ready them for gc (garbage collection).

4.  Use weak event listeners and remove listeners.

5.  Strictly type variables whenever possible.

6.  Explicitly disable mouse interactivity when mouse interactivity not needed.

7.  Replace dispatchEvents with callback functions whenever possible.

8.  Stop Sounds to enable Sounds and SoundChannels to be gc'd.

9.  Use the most basic DisplayObject needed.

10. Always use cacheAsBitmap and cacheAsBitmapMatrix with air apps (i.e., mobile devices).

11. Reuse Objects whenever possible.

12. Event.ENTER_FRAME loops: Use different listeners and different listener functions applied to as few DisplayObjects as possible.

13. Pool Objects instead of creating and gc'ing Objects.

14. Use partial blitting.

15. Use stage blitting.

16. Use Stage3D

Greatest to Least Benefit

  1. Use stage blitting (if there is enough system memory).
  2. Use Stage3D.
  3. Use partial blitting.
  4. Use cacheAsBitmap and cacheAsBitmapMatrix with mobile devices.
  5. Explicitly disable mouse interactivity when mouse interactivity not needed.
  6. Do not use Filters.
  7. Use the most basic DisplayObject needed.
  8. Reuse Objects whenever possible.
  9. Event.ENTER_FRAME loops: Use different listeners and different listener functions applied to as few DisplayObjects as possible.
  10. Use reverse for-loops and avoid do-loops and while-loops.
  11. Pool Objects instead of creating and gc'ing Objects.
  12. Strictly type variables whenever possible.
  13. Use weak event listeners and remove listeners.
  14. Replace dispatchEvents with callback functions whenever possible.
  15. Explicitly stop Timers to ready for gc.

  16. Stop Sounds to enable Sounds and SoundChannels to be gc'd.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines