|
-
01-13-2007, 09:50 AM
#401
Senior Member
var collisionRange:Number = (heroRadius + enemyRadius) * (heroRadius + enemyRadius);
This is not an expensive operation. Worse case scenario, you can simply run this code when it's needed. Or, as you said, you can also cache things in a data structure of some sort. Test to make sure that creating them on the fly is noticably slower then caching though as you want to be sue the cache is a good idea.
-
03-26-2007, 09:36 AM
#402
I have something like this in my onEnterFrame:
// inside a foor loop with x
if (Thing[x].P>random(100) {
// Do cool stuff with Thing[x]
}
Cool stuff will happen to Thing[x] occasionally, P % of the time. But maybe random() is CPU-consuming? (I will test this)
Is there another way? I considered making a small "almost-random" array of random numbers for each Thing[x] and picking out these numbers one at a time, instead of calling random()...
Or can I use random(), but only when the condition is true? Let's say a counter counts to a certain number that was randomized LAST time cool stuff happened to Thing[x]. Then, next time it happens, a new number is handed out. How would that look like, to make it happen P % of the time... (Can't think right now)
Let's say, P is 5. If I count to 100/5 = 20, it would happen 5 % of the time, but not randomly. Maybe if I counted to random(40)? Would that produce the same pattern as testing 5>random(100) each time?
-
03-26-2007, 09:50 AM
#403
Senior Member
Maybe if I counted to random(40)? Would that produce the same pattern as testing 5>random(100) each time?
No, the results will be different. random(100) could potentially produce >50 many times in a row. If Thing[x].P were equal to 50, then the if statement would be true often. If you use your random(40) and then a smaller Thing[x].P you will much less often see repeats.
With that said, what you describe is a common way to make random things less "twitchy". You simply establish a "duration" that determines how long before "thing" can be updated again. Generally this is done after the previous update. You will probably want to do this independently for each Thing though rather then for all of em. It will make it "feel" better when people play.
-
03-26-2007, 10:01 AM
#404
But, I would not always count to random(40), but to random(2*100/P)... So, if P=50, I would count to random(4).... And hey, isn't that a good example? The chance of flipping a coin and get "head" twice in a row is 1/4. And only 1/4 of the times, with the counting-method, I would get to count from 0 to 0, i.e. it would happen in a row. Right?
Or, maybe you're still right, that it's not the same pattern, somehow...
-
10-07-2008, 06:37 PM
#405
Game optimization
Hi! I’m a new member and this is my first post!
I've developed an Actraiser's remake (from the SNES) using sprites I've found on the web. This is a platform game and is 1 level long which includes boss. However, the game looks and feels a little “slow”. I spent yesterday’s afternoon reading about AS Optimization Tips (I also read all posts in this thread) and I noticed that I didn’t apply most of the optimization issues mentioned in this great article. For example, I’m using loops instead of ASBoradcaster, every baddie has his own OnEnterFrame where most of its actions are performed and I’m using complex if-else’s. Also, I use the movie’s main OnEnterFrame to perform Hero’s actions, collisions, background and platform movement, etc… using loops and arrays, of course. By the way, I used FlashMX 2004, I suppose it uses ActionScript 1.0 (sorry for not using a newer version, but I have a low budget… $$$).
I’m planning to restructure my game applying the optimization tips, but I have a question: Should I place all the actions on a single OnEnterFrame event? I mean, place collisions, scenery movement, hero actions, baddies actions, all to be executed in the movie’s main OnEnterFrame, just like in the old school programming where you placed all in a single main loop. I also read that I could use a MovieClip as a handler, but I want to choose the best option that will boost my game’s performance so I can continue with the next levels.
Here’s the game’s demo:
http://www.mediafire.com/?sharekey=1...db6fb9a8902bda
I hope I can read your recommendations.
Thanks a lot guys!!!
GT
-
10-08-2008, 02:52 AM
#406
Senior Member
A long time has passed since I wrote that article that you have mentioned I think those tips are still valid if you're using Actionscript 1, however the Flash Player (the "flash runtime") has evolved alot since then, it has gained better overall perforamance and it finally provides JIT compilation if used in conjunction with Actionscript 3.
If you're looking for the ultimate performance for browser based games I would definitely recommend to invest some time and learn AS 3, it will provide a huge performance increase over your AS1 code no matter how many performance tricks you apply 
Hope it helps
p.s. = btw, you don't need to buy any new Flash to start studying/working with AS3. Adobe nicely provides the Flex SDK for free here -> http://opensource.adobe.com/wiki/dis...exsdk/Flex+SDK
Lapo
-
10-08-2008, 11:58 AM
#407
-
10-06-2010, 03:32 AM
#408
Any optimisation improvements for this code appreciated
I am having a stab at making a game for the iPhone and its a bit sluggish on the iPhone 3G.
I have been trying to optimise the following code and would therefore appreciate any further suggestions to improve this code if anyone has any?
Code:
function moveBallObjects(e:Event):void {
var sqrt = Math.sqrt;
for each(var ballObject in objGame.ballObjects) {
if (ballObject.alpha < 1) {
var newScale:Number = ballObject.scaleX * 0.98;
ballObject.alpha = ballObject.alpha - 0.1;
ballObject.scaleX = ballObject.scaleY = newScale;
if (ballObject.alpha <= 0) {
ballObject.horizontalVelocity = ballObject.verticalVelocity = 0;
ballObject.visible = false;
continue;
}
} else if (returnIfBallOnTheBoard(ballObject) == false) {
ballObject.alpha = ballObject.alpha - 0.08;
}
var horizontalVelocity:Number = ballObject.horizontalVelocity;
var verticalVelocity:Number = ballObject.verticalVelocity;
var V:Number = (horizontalVelocity * horizontalVelocity) + (verticalVelocity * verticalVelocity);
// ----------------------------------------------------------
// Only do calculations on this ball if it is actually moving
// ----------------------------------------------------------
if (V == 0) {
continue;
}
V = sqrt(V);
var k:Number = (V - objGame.dV) / V;
if (k < 0) {
k = 0;
}
horizontalVelocity = horizontalVelocity * k;
verticalVelocity = verticalVelocity * k;
ballObject.tempX = ballObject.tempX + horizontalVelocity;
ballObject.tempY = ballObject.tempY + verticalVelocity;
ballObject.horizontalVelocity = horizontalVelocity;
ballObject.verticalVelocity = verticalVelocity;
// ---------------------------------
// Update Position of Ball On Screen
// ---------------------------------
ballObject.x = ballObject.tempX;
ballObject.y = ballObject.tempY;
}
}
function returnIfBallOnTheBoard(passedMC:MovieClip):Boolean {
if (passedMC.x < objGame.numBoardMinX) {
return false;
}
if (passedMC.x > objGame.numBoardMaxX) {
return false;
}
if (passedMC.y < objGame.numBoardMinY) {
return false;
}
if (passedMC.y > objGame.numBoardMaxY) {
return false;
}
return true;
}
Thanks
Paul
Paul Steven
http://www.mediakitchen.co.uk
-
10-06-2010, 12:10 PM
#409
Senior Member
Well you could check immediately if the ball was on the board and set visible to false.
But most importantly, if you did not use alpha it would run faster.
-
10-06-2010, 12:15 PM
#410
Thanks UnknownGuy.
I will try removing the alpha and see if it improves things though this is only used when a ball goes out of play so I am not sure it will make my game any more playable on the 3G. Certainly worth a try though
Thanks
Paul
Paul Steven
http://www.mediakitchen.co.uk
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|