View Full Version : Zuma Like Game and High CPU Usages
neeraj
06-19-2008, 05:13 AM
I am working on a Zuma like game in Flash 8 AS2. Game FPS is 30.
My problem is that game is taking too much CPU. Major CPU is used by graphical part, when Flash does repaint. Like moving the balls, rotating and spinning balls. Also I have some graphics in game that contiously runs.
In a P4 1.7 GHz system it takes 75 % CPU with only 30 balls on screen.
Also I am reading data from an array which is about 6000 elements in onEnterFrame. Is there any fast reading method from arrays ? There is several checking and loops are going on in onEnterFrame.
Anyone can tell why it is so slow and why Flash 8 repaint is so slow. Is there any way to make is fast, low on CPU usages ?
6000 elemnts in an array that you loop constantly with onEnterFrame can certainly slow down a lot your game. What's in this array? Why do you need all this?
neeraj
07-02-2008, 06:04 AM
I really need that array of 6000 elements, as its the bezier path on which I am moving the ball and I have agian tried and found that its not that array that is causing the slowdown.
Actually, its redraw/repaint that is using high CPU.
If I just turn off the rotation we can see a difference of 10% CPU usages.
Anyone else.
CobaltBlueDW
07-02-2008, 06:47 AM
I'd like to know how you are sure it isn't the array. Did you take the array check out of the onEnterFrame to see if it ran faster?
You can definitely solve the problem without an checking an array of 6000 points 30 times a second. If you explain more and show some code I could suggest an alternative solution.
Also, I can't tell you how you can optimize the graphics without knowing more about your graphics, so google yourself up some flash optimization tips, or give me more info. :)
One big one might be, if the balls aren't animated cache the images as bitmaps so flash doesn't have to draw each ball every time. Flash can render bitmaps quite a bit faster than Vector Graphics in many instances.
neeraj
07-02-2008, 07:11 AM
Thanks for you reply.
CacheAsBitmap is already turned on.
I removed the 6000 elements array references and checked it but there no major different in comparison to what we can see after turning the "_rotation".
Means that even If I change _rotation property with a fixed value like 10, then also no change in CPU usages and if I turn off that _rotation then CPU usages goes down 10%.
One more thing, I need rotation because If I don't use _rotation then a shacking comes up in balls movement. Rotation actually does some visual changes in positions, yet it does not changes the x/y properties but visually we can see some change in its position event if it is CIRCLE. Because of that visual change the shaking/zigzag in balls movement get minimized.
Neeraj
CobaltBlueDW
07-02-2008, 07:36 AM
You know I bet the _rotation issue is from CacheAsBitmap. Flash has to do more work when it has to translate all the pixels by some rotation on a redraw.
You don't have 6000 movieclips on your stage do you? You just have a hand full of movieclips actually moving around the screen?
I would assume you have maybe 30-40 movie clips on the stage at any one time? --And those movie clips don't have any events being listened to?
neeraj
07-02-2008, 08:11 AM
CobaltBlueDW, thanks for your reply.
I have tried by turning on rotation for a movie clip with cacheAsBitmap set to true and then cacheAsBitmap set to false. In both cases, cpu usage shown in task manager was nearly identical.
So far tests with various options turned on or off didn't showed any cut down way.
Only thing which I got is that if I avoid use of _rotation, then CPU use cut down by 10-20%. So it shows that _rotation require some extra CPU cycles (like wise you mentioned), and its for both bitmap or vector.
Let me explain things in more detail.
What I am doing is having a bezier path, converted to an array. Each array element is an object with 3 properties. Those are X, Y position and Angle of rotation ( angle was calculated and stored along with X,Y positions, to prevent the run time resources.
Now when game starts, I create a duplicate movieclip of ball from library and in root's onEnterFrame, ask ball movie clip (which I created) to move along with the X,Y values from array. X, Y position values are in fractions (not integer). Now if I turn off _rotation (which use to assign the angle property of the current bezier point to ball), I can definitely notice CPU use cut down. At the same time ball on path started shaking while moving.
I think this is because flash support only two digits after decimal in X,Y property an that is also in multiple of 5. like 0,5,10,15,20 and so on. If we give anything else, flash try to round that above range and this cause shaking. Because, our array have values like 20.2148002, 20.828080038.
If we turn on rotation, then in visual appearance object seems to be moving from their current position (though there is no change in X,Y property, but visually we can see some changes). If we turn on rotation, then it minimize the shaking caused due to X,Y position to very minimum level. So virtually we can't notice any shaking.
So now what I have to do is finding a way where I can use rotation or some other method while keeping CPU use low as much possible and sort out the shaking problem.
Any idea/help will be appreciated.
Neeraj.
CobaltBlueDW
07-02-2008, 08:34 AM
If having CacheAsBitmap off doesn't seem to affect performance then leaving it off is preferable.
I think you have to change the scale of your game. If you need to frequently utilize fractions of pixels in your positioning, it is definitely time to scale up. :)
You can always resize the finished product if that is an issue.
Try doubling your flash app size and throwing all your coordinates through this function before storing them.
public function resize(x:Number, y:Number, r:Number, scale:Number){
if(isNan(scale)) scale = 2;
r = Math.floor(Math.abs(r%360));
x = Math.floor(x*scale);
y = Math.floor(y*scale);
}
Getting rid of the need for rational pixel positioning might get rid of the shaking.
Good Luck!
|
vBulletin® v3.8.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.