Copy link to clipboard
Copied
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;
}
}
}
}
}
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
...Copy link to clipboard
Copied
for movieclips that are only moving (ie, not scaling, rotating, fading), enable their cacheAsBitmap property.
Copy link to clipboard
Copied
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 ?
Copy link to clipboard
Copied
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
16. Stop Sounds to enable Sounds and SoundChannels to be gc'd.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now