Be a Supporter!

how do i get more FPS for my game?

  • 591 Views
  • 29 Replies
New Topic Respond to this Topic
Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
how do i get more FPS for my game? 2009-02-24 20:59:46 Reply

hi im making a highly dynamic game in as2 and my frame rate is really low even though it is at 30 fps right now

http://spamtheweb.com/ul/upload/240209/7 5468_weirdshootercs41.php

i heard u could make all your movie clips to be fake bitmaps to trick flash and make it run faster but i dont know

Ashed-Dreams
Ashed-Dreams
  • Member since: Jun. 27, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-24 21:27:18 Reply

I personally prefer to take things down the dumb way. Export your graphics as .pngs then re-import them into flash.

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to how do i get more FPS for my game? 2009-02-24 21:29:41 Reply

Optimize your code and/or use AS3.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
LeechmasterB
LeechmasterB
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to how do i get more FPS for my game? 2009-02-24 21:30:26 Reply

What you mean is
MovieClip.cacheAsBitmap = true;
But it only gives you better performance on vectors that do not get scaled or rotated and only are moved around.

Your game should not run so slow with so few things in it. May be you should program it faster/better to get more performance improvement. Are you doing a lot of collision detection / distance measurements ect. for AI and such?

Making a complex game in AS2 is a bad idea anyways. You should do that in AS3 to achieve desired performance results.

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-24 21:45:17 Reply

yea i probably have more than 40 hittests going on every frame maybe if hittest events make my game slow then i would have to change that

Renandchi2
Renandchi2
  • Member since: Jul. 24, 2008
  • Offline.
Forum Stats
Member
Level 13
Filmmaker
Response to how do i get more FPS for my game? 2009-02-24 22:03:29 Reply

At 2/24/09 09:27 PM, Ashed-Dreams wrote: I personally prefer to take things down the dumb way. Export your graphics as .pngs then re-import them into flash.

Agreed
Good game, btw. But W won't make me jump. And the controls on the keyboard are kinda streched apart (make the player click to attack instead of Y). But good game, and great code


/

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-24 22:15:16 Reply

i also heard that in as3 and cs3 you could use all the filters you want and it wouldnt lag too badly? in as2 it creates bad frame rate. i would just change to as3 if the filter system is better, the blur filter is so nice

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 03:25:05 Reply

I am sorry, but while as 3 is a lot faster than as 2, it can not help with filters, because the filters are not run in actionscript. On the other hand, multiple processor cores will help, since Flash Player now supports multithreading for the rendering.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

Redshift
Redshift
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 15
Programmer
Response to how do i get more FPS for my game? 2009-02-25 04:07:56 Reply

Tips:

Only use cacheAsBitmap if the MovieClip does not scale or rotate, or else flash will have to remake the bitmap if it scales or rotates (simply moving it around is okay though), hence this would make things run slower than if you didn't apply cacheAsBitmap.

Try to avoid filters when you can, and if you need to use them, use low quality.

The difference of low, medium and high quality filters, is that it applies the same low quality filter twice for medium quality, and three times for high quality. This makes medium quality twice as slow as low quality, and high quality is three times as slow as low quality.

That's for graphics.

When it comes to code, if you are using 100% processing while running your game, then you should consider making some serious revisions about how tasks need to be executed. The instant you go over 100% processing, the FPS begins to deteriorate.


#include <stdio.h>
char*p="#include <stdio.h>%cchar*p=%c%s%c;%cmain() {printf(p,10,34,p,34,10);}";
main() {printf(p,10,34,p,34,10);}

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 05:27:01 Reply

I suspect that the lag is as much to do with the code that's running, as the filters. The number of moving sprites with some kind of AI (move/attack), plus the variable terrain detection, it's all going to add up to a fair whack of processing power, especially with the individually animated body parts on top of that. The fact that you hit an infinite loop if you die by falling off the left hand end suggests that all is not as it should be in terms of the code. Check it through for unnecessary loops and if possible only run functions when they're needed.


- - Flash - Music - Images - -

BBS Signature
Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 07:44:47 Reply

ok im going to remove all filters and try to create the filter look in animation and i will remove bad loops and probably just going to turn all my as2 into as3 after i clean it up so it will run even better.

LeechmasterB
LeechmasterB
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 07:59:36 Reply

40 hittests sounds like a lot. There should be only 1 hittest necessary per baddie and player at a cycle and on attack once.

Like earlier suggested it will help if you export the vectors and reimport as png. The individual limbs for the characters will slow down renderperformance a lot as vectors. Additionally to that you could prerotate your graphics and cache each sprite rotated that will ease rendering performance as well instead of having to render the things rotated.

Also if you have a lot of objects that need hittesting there is one more step you can do. If you do spacial partitions you will need to register objects to areas and keep updated lists (which takes some performance). But that will reduce hittests to only surrounding objects. So it makes a big deal if you have many objects.

About filters, no their not really faster in AS 3.0. But think about usage of filters. If an object doesn't really change the graphics filter wise you could also precache a filtered graphic instead of reapplying the filter every frame.

Like suggested by the others think about what needs to happen how often. For example if the AI runs a check on the players position you only need to do that every 10 or 100 frames unless its in attacking range and attacking.

A lot of things can be precalculated and temporarily stored until they change, its a matter of design too. Look over all your functions and the variables they use. Are you doing unnecessary math? Can you extract constants or reformulate an equation simpler? Can you store values that are used in loops and use them once calculated for all objects using them in the loop? Logical optimizing can do a huge deal on performance in general. Huge chains of if statements are bad. Can you replace such through a state pattern may be or store a function that gets called in an array and skip evaluations and directly call the function depending on its case (by which its referenced in the array).

There are many ways to optimize...

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:19:39 Reply

well the reason theres so many hit tests are for example:
if the player isnt touching the ground- then gravity comes in
if the player isnt touching the ground and he presses down- then you cant crouch
if the play isnt touching the ground and wants to jump- you cant double jump
if the player is touching the ground and presses down- you can crouch
if the player is touching the ground and you press u- then you can jump
if the player is touching the ground and has sword out and presses down- he can roll
theres many more hit tests running without functions...they are just "if" events which probably screws up my performance

also the reason my enemies have more than 1 hit test is for
if he isnt touching the ground---use gravity
if he is touching the grond-- this._y-- so he doesnt walk through hills
if the bullet hits the enemy-- health points--
if the grenade hits the enemy-- die
if the enemy hits another enemy-- this._x+=random(5) so they dont overlap
if the enemy touches the character's blade- death
if the enemy's head hitbox touches a bullet- count as a headshot and die

then the grenade
if it isnt touching the ground- gravity
if it is touching the ground- bounce once

then the bullet
if it touches the ground- go to frame "dirt"
if it touches the enemy- go to frame "blood"
if it isnt touching the vcam- remove movie clip

this is just the half of it... i know some of this can be put into one but it is easier to just code based on what you are trying to do while you test it instead of messing with your old code that works

the player has 307 lines of code but at the same time it could be less and im still adding code

i didnt know hit test takes so much processing i was just taught to abuse the hit test function but i didnt make as complex games in highschool

LeechmasterB
LeechmasterB
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:23:43 Reply

I have a hunch that you just showed where a lot of performance gets lost in your code. Did you actually code the collision detection with the ground with so separate if statements and hittests in each? If you could post a code sniped of the player for example it could be verified.

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:29:04 Reply

At 2/25/09 08:23 AM, LeechmasterB wrote: I have a hunch that you just showed where a lot of performance gets lost in your code. Did you actually code the collision detection with the ground with so separate if statements and hittests in each? If you could post a code sniped of the player for example it could be verified.

if (_root.ground.hitTest(this._x, this._y-5, true)) {
gravity = 4;
jumping = 0;
jump = -20;
this._y -= 4;

}
this 1 lets you jump again because jumping is not true ( i use 0=false and 1 = true)
and it makes sure you move up and down hills when your ._y-5 is too low in the ground mc detection
if (!_root.ground.hitTest(this._x, this._y-1, true)) {
gravity++;
this._y += gravity;
}
this 1's hit detection is higher so that you dont bounce up and down when they contradict each other

LeechmasterB
LeechmasterB
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:39:44 Reply

That looks really bad like i thought.
There should be exactly 1 hittest for this and no more. You will have to recode some parts though.

Also i really disliked how you treated good old physics :P.

Gravity is a constant! (9.81 m/s^2) on earth. It does not change!
It is an acceleration constant that works on the velocity of your player.
So it should be more like:

velocityY += gravity;
_y += velocityY;

monksor
monksor
  • Member since: Nov. 8, 2008
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:43:16 Reply

At 2/25/09 08:39 AM, LeechmasterB wrote: Gravity is a constant! (9.81 m/s^2) on earth. It does not change!
It is an acceleration constant that works on the velocity of your player.
So it should be more like:

velocityY += gravity;
_y += velocityY;

I was just about to post the same thing :P

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:43:51 Reply

well if i walk up a hill how would i be able to go up it when theres only 1 hit test? 1 hit test is for when im no where near the ground and i fall and 1 hit test is for when im so deep in the ground that i have to pop up to match the surface

LeechmasterB
LeechmasterB
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:49:39 Reply

At 2/25/09 08:43 AM, monksor wrote: I was just about to post the same thing :P

:D

Yeah well in your specific case you could use a while loop to move the character out of the ground when it is colliding. But it still remains only 1 necessary hittest with the ground :).

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 08:53:09 Reply

well lets say i have 40 hit tests or more in my as2 which makes it slow and i remove all the filters.. then i switch to as3..do you think it would still run faster even if used

40 of these

if (ground.hitTestPoint(player.x , player.y-3,true)) {
player.y-=7;
gravity = 0;
}

opposed to 40

if(_root.ground.hitTest(this._x,this._y,
true)){
player._y-=7;
gravity = 0;}

LeechmasterB
LeechmasterB
  • Member since: Apr. 1, 2005
  • Offline.
Forum Stats
Member
Level 17
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 09:05:59 Reply

The idea is to reduce hittests as much as you can ALWAYS. So you should really consider recoding these parts.

And if you switch to as 3 and reduce filters it will help. But you should also use png's instead of vectors for the characters body parts.

If you just do these few things it should make a huge difference. But i really don't understand the difference in your mentioned example. 40 of the same thing will perform the same way ... where is the difference in that code sniped? :P

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 09:15:26 Reply

well i dont see the point of moving to AS3 performance wise.

when i was a senior in HS(last year) they created AS3 so i barley learned some things in as3 but the reason i stayed with as2 is because its so easy

i have this so far in as3 that i did in highschool but didnt work on it because even though as3 isnt much harder, it is like forgetting everything you memorized in as2 for very small differences like this._y to this.y and stuff. i guess if i code it in as3, then i would use more functions which would make it smoother even if i tried to code it poorly

Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 09:20:52 Reply

o forgot link and cant edit
http://spamtheweb.com/ul/upload/250209/3 3182_metalslugas3.php

Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 09:55:00 Reply

would also hugly help if you got rid of the Vcam...just put everything that you want to scroll in a mc, and change its x/y. As for hitTests things such as:
if this hitTests ground
jumping = true
else
jumping = false
could be replaced by
if key W is pressed && gravity is 0
gravity = 20


AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature
Jereminion
Jereminion
  • Member since: Nov. 6, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 10:02:10 Reply

At 2/25/09 09:55 AM, Yambanshee wrote: would also hugly help if you got rid of the Vcam...just put everything that you want to scroll in a mc, and change its x/y. As for hitTests things such as:
if this hitTests ground
jumping = true
else
jumping = false
could be replaced by
if key W is pressed && gravity is 0
gravity = 20

well the way i do jumping is i set jump to 20 and dont do anything with gravity
i do while jumping...
jump--
this._y+=jump
so 1st you are going up at a fast rate then when jump goes past 0 you go back to the ground and the jump value mimics gravity but i see what u mean

Deadclever23
Deadclever23
  • Member since: Nov. 27, 2006
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 11:19:36 Reply

At 2/25/09 07:59 AM, LeechmasterB wrote: There are many ways to optimize...

These are actually all very useful tips. I'll follow these.
Oh and dude why so many hitTest's just do the one hitTest and run other ifs insid it.

if(this.hitTest(that)){
          if(Key.isDown(Key.LEFT)){
                   //stuff
          }
          if(crouch){
                    //blah
          }
          hit=true
}
if (hit!=true){
          /more stuff
}

"To live is the rarest thing in the world. Most people exist, that is all."
- Oscar Wilde

BBS Signature
Deadclever23
Deadclever23
  • Member since: Nov. 27, 2006
  • Offline.
Forum Stats
Member
Level 12
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 11:20:52 Reply

At 2/25/09 10:02 AM, Jereminion wrote: Blah

Easy way to jump.

yvel=-10

"To live is the rarest thing in the world. Most people exist, that is all."
- Oscar Wilde

BBS Signature
Yambanshee
Yambanshee
  • Member since: Oct. 5, 2008
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 11:35:14 Reply

At 2/25/09 11:20 AM, Deadclever23 wrote:
At 2/25/09 10:02 AM, Jereminion wrote: Blah
Easy way to jump.

yvel=-10

Basicly what i said... Less variables, let gravity do the work for you, gets the same results, only looks more realistic, as your rising/falling at the same speed as a normal fall


AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature
Rekano
Rekano
  • Member since: Aug. 24, 2005
  • Offline.
Forum Stats
Member
Level 07
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 12:11:43 Reply

tested it in a fast way... it jumps with the u I think, I didn't watched.. add less hit tests and try again :D

henke37
henke37
  • Member since: Sep. 10, 2004
  • Offline.
Forum Stats
Member
Level 30
Blank Slate
Response to how do i get more FPS for my game? 2009-02-25 12:18:25 Reply

if you are going to check the result of an expensive operation more than once, cache it. Or in simpler terms, hittest once and save the result in a variable and check against that.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.