Forum ActionScript 3.0 Trouble with some code

Problems and discussions relating to ActionScript 3.0 here.

Trouble with some code

Postby default0 » February 13th, 2010, 7:30 pm

Hi guys

I'm having trouble with a small piece of code.
Code: Select all
shieldHealth += Math.round(Math.random()*50);
if (shieldHealth > 200000)
{
   shieldHealth = 200000;
}
shield.x = player.x;
shield.y = player.y;
inter_face.shieldText.text = shieldHealth + "/ 200000";
shield.alpha = (shieldHealth / 200000);
inter_face.shieldBar.width = shield.alpha * 150;


This one is it.
Its not throwing errors or anything, it just lags hard (actualization rate goes from 37 to 22...).

Hope anyone can tell me why, Im clueless...

Best regards
User avatar
default0
 
Posts: 115
Joined: February 2nd, 2010, 6:46 pm
Location: Germany

Re: Trouble with some code

Postby EgoAnt » February 14th, 2010, 12:06 am

How frequently are you calling this piece of code?
User avatar
EgoAnt
 
Posts: 23
Joined: February 2nd, 2010, 5:01 am
Location: Edmonton, AB

Re: Trouble with some code

Postby Benjaminsen » February 14th, 2010, 1:09 pm

As EgoAnt is saying, how often are you calling that code?
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Trouble with some code

Postby default0 » February 14th, 2010, 2:43 pm

Each frame (40x/sec).
If you mention the "alpha" in it.
Thats not it, I removed it to test if thats what it was, but it wasn't...

Best regards
User avatar
default0
 
Posts: 115
Joined: February 2nd, 2010, 6:46 pm
Location: Germany

Re: Trouble with some code

Postby EgoAnt » February 15th, 2010, 7:46 am

I would try pulling as much as possible out of the cycle that requires Flash computing something.

For example, rounding can be fairly intensive, and the random can, too.

A couple little tweaks you could do would be:
1. Add a fixed amount to the shield every turn, this way you don't have to use rounding and random.
2. Check the shieldHealth before you add to it, that way on any frames where the player isn't damaged you don't have to do anything.
3. You might want to also break out the last section that adjusts the alpha onto another cycle that runs less frequently, or add it in to the if() code block so it only runs when the shield is actually adjusting.

Basically any time you are running the Math functions frequently they can slow things doen, here's a good article about it: http://www.calypso88.com/?cat=7
User avatar
EgoAnt
 
Posts: 23
Joined: February 2nd, 2010, 5:01 am
Location: Edmonton, AB

Re: Trouble with some code

Postby default0 » February 15th, 2010, 11:18 am

1. Add a fixed amount to the shield every turn, this way you don't have to use rounding and random. -> Already tried, didnt help
2. Check the shieldHealth before you add to it, that way on any frames where the player isn't damaged you don't have to do anything. -> I do this, but the shield recovers each frame and should keep doing this
3. You might want to also break out the last section that adjusts the alpha onto another cycle that runs less frequently, or add it in to the if() code block so it only runs when the shield is actually adjusting. -> I already removed the whole alpha section, didnt help

Okay, replaced shieldHealth += ... with ++shieldHealth -> lagg
removed that line -> runs fine -> removed visualization (showing new shieldHealth) -> lagg -> removed ++shieldHealth -> no lagg

Any ideas?

Best regards
User avatar
default0
 
Posts: 115
Joined: February 2nd, 2010, 6:46 pm
Location: Germany


Return to ActionScript 3.0