PDA

View Full Version : Improving code for speed



Roalr
July 30th, 2009, 05:46 PM
Hello, I'm working on a game as a sort of hobby. I just started today, so don't expect anything too advanced. There are mini Marios falling and you have to catch them with Yoshi... here's an example of the game:

http://rorobertalr.webs.com/index.htm

I forgot to mention, I'm using flash 8

Question 1

There is a delay that keeps track of how long it takes before the next Mario falls down, and the delay decreases as the game goes on, I've coded it this way:



onEnterFrame = function(){
delay = 60-Math.ceil(Math.pow(getTimer()/5000, 1.2));
}
But I feel this is a bad way to do it. The point of that script is to make the delay decrease faster as the game goes on. Is there a better way to achieve that effect?

Question 2

I have a "border" on the left and right sides of the screen which Yoshi can't go beyond. But again I feel there's a better way to code it:


onMouseMove = function () {
if (_xmouse>mcYoshi._width) {
if (_xmouse<Stage.width-mcYoshi._width) {
mcYoshi._x = _xmouse;
}
}
};The border is the half the size of Yoshi and Yoshi's registered in the middle.

Question 3

I want to optimize this part of my script which removes the Marios:

function removeMario(ob) {
with (ob) {
var id:Number = Number(_name.substr(2));
mArray.splice(id, 1);
removeMovieClip();
for (var j = id+1; j<mArray.length+1; j++) {
game.clip["m_"+j]._name = "m_"+(j-1);
}
}
}Basically, I determine which Mario hit the ground by its name, I remove him from mArray and then every other Mario that's listed after that Mario in mArray has his name changed to correspond with its new position in mArray.

i.e : There are 5 Marios on the stage, Mario_3 hits the ground, he's removed from mArray, his position in the array is 3, that index is removed from mArray, then Mario_4 and Mario_5 get their names switched to Mario_3 and Mario_4 respectively.

There has to be a better way to achieve this.

Thank you for taking the time to read my post :)
Robert

flyingmonkey456
July 30th, 2009, 11:21 PM
1. use two countdowns (variables that subtract one from themselves every frame), one counting down to change the rate that the marios appear and one counting down that is the rate that the marios appear. also a variable with the amount of speed change in it. you would put a big number in the first countdown, however many seconds you want it to wait before changing times your framerate. every time this number reaches 0, you would change the speed and reset the countdown back to the top. you can hardcode the top or have it as a variable, it doesn't matter. for the next part, you'll need to create another variable that decides the top of the second countdown. every time the first countdown reaches 0, subtract the amount of change you defined above from the top of this countdown. every time the second countdown reaches 0, make a mario fall and reset it to 0.

2. two simple if statements will fix this. if yoshi._x > (stagewidth) then yoshi._x = (stagewidth). same for the other side, but use 0 instead of the stage width.

3. run a loop that goes through every mario checking for hittests with the ground, then another checking for hittests with yoshi. then delete the ones that test true.

there may be better ways than these, but this is how i would do it.

SparK_BR
July 31st, 2009, 12:26 AM
hmm... if you base your fall rate on the score the game will get somehow fair, but too long...

overall there's no lag here, running in a toshiba dinobook
specs:
1gb RAM DDR SDRAM (2 of 512mb)
2ghz intel Celeron mobile
8mb video ram :(

therobot
July 31st, 2009, 12:50 AM
Whatever you do, don't base your falling logic on getTimer(). your logic will break down if you ever introduce the ability to pause the game.

Roalr
July 31st, 2009, 07:22 AM
Thanks for the comments :)

flyingmonkey I like your idea for question 1, I think I'll use that instead because, like therobot said, if I use getTimer(), I won't be able to pause the game.

As for q2, I guess your method and mine wouldn't make much of a difference, correct me if I'm wrong.

For q3, I'm already going trough a loop that does that, but in both cases whether they touch the ground or Yoshi they have to be removed from the stage, which is where my removeMario function comes in. The reason I'm using it is to keep my array as small as possible, I could instead not splice the array but after a while I would end up with a huge array, plus I would have to loop through the array on every frame, so the shorter I can make it the better. I guess that after all the method I'm using right now is not a bad approach.

And for the comments about the score based on time, it's just temporary, I will remove the getTimer() from the game to be able to pause it. This isn't just going to be a "catch them" game, it's only one part of it.

Btw do you like how the game looks? I know it's in gray but there's a reason behind that. The marios are kind of wierd too :s

Edit: I've updated the game, now the score does not depend on getTimer() and I've added a pause button, you can try it out here:

http://rorobertalr.webs.com/index.htm

I'm having a weird problem with the score: sometimes when the game ends the shown score does not correspond to the actual score.

flyingmonkey456
July 31st, 2009, 09:04 AM
by the way, i think you should give them all the same fall speed. it's too easy for two of them to hit the ground at the same time and it makes you lose really fast. and i think the time between spawns gets too small too fast. they're falling slowly and i'm catching them all with no problem, then a second later i'm having to catch 10 at a time.

Roalr
July 31st, 2009, 09:24 AM
Alright I've updated the game again thanks for the suggestions flyingmonkey :hugegrin:

flyingmonkey456
July 31st, 2009, 02:05 PM
i like it so far, there's still a tiny bit of lag every few seconds and it makes it keeps messing me up. does anybody else get that? its probably because my anti-virus is running scans right now.

other than that, it still gets a bit hard when the level changes and the first one with the new speed starts falling while the ones from the previous level are still falling. once you get into the higher levels, it can get very annoying. perhaps it could wait for the screen to empty before it starts the next level? you could even flash the words "level up!" on the screen like an old arcade game. by the way, i can color the sprites for you if you give me a link to them. i might even be able to make a nice mario background :)

http://imagebin.org/index.php?page=add is a good place to upload them

oh, and one more thing. it's kind of visually unappealing when something suddenly starts and stops moving. you should add some easing to the yoshi :P

Roalr
August 1st, 2009, 12:10 AM
Thanks again for the suggestions, ya I was think of adding some easing, the thing is at higher levels I think easing would be more annoying, but I'll experiment with it.

As for the level changing and different speeds I knew about it I just didn't have time to edit it.

For the colors, well I drew the sprites in flash, originally they were colored but I decided to remove them for the plot of my game. But I think I'll be switching back to colors. I'll send you the fla. The thing is the sprites are quite messy :s

http://rorobertalr.webs.com/spirtes.fla

Each mc (i.e : mcMario) contains a graph (graphMario). graph is where the sprites are animated.

i.e: mcMario contains graphMario. Glow effect is applied on graphMario, and inside graphMario are the different frames for the animation.

Thank you for your offer :)

flyingmonkey456
August 1st, 2009, 10:57 AM
here ya go ;)
i didn't know what parameters you used for the blur, so you've gotta do that

http://www.mediafire.com/download.php?03z5wikm2yz

Roalr
August 1st, 2009, 11:13 PM
Thanks :)

What version of flash did you save it in? Because I have flash 8 :s and I can't open the file.

flyingmonkey456
August 1st, 2009, 11:36 PM
cs4, try and find a converter or something :P

Roalr
August 2nd, 2009, 12:06 AM
Alright I'll try, but could you try to save it as an older version?

flyingmonkey456
August 2nd, 2009, 02:22 AM
Alright I'll try, but could you try to save it as an older version

i can only save back to cs3, you've either gotta upgrade or find a converter :P

Roalr
August 2nd, 2009, 09:31 AM
Could you send me the swf version? I'll try using a swf decompiler, I didn't find any converters.

flyingmonkey456
August 2nd, 2009, 10:32 AM
here ya go

http://www.mediafire.com/download.php?v4j23wut4jm

i don't think it will work, it will just decompile it to a cs4 fla :P

Roalr
August 2nd, 2009, 11:56 AM
here ya go

http://www.mediafire.com/download.php?v4j23wut4jm

i don't think it will work, it will just decompile it to a cs4 fla :P

Actually it worked :) it lets you select which version you want. I'm using Sothink trial version.

flyingmonkey456
August 2nd, 2009, 04:09 PM
so do you want a background? i could do the one with the giant buildings that look like silos :P

http://www.gamesandgames.net/wp-content/uploads/2007/02/super_mario_world_flash_game.jpg