dcsimg
A Flash Developer Resource Site

Page 21 of 21 FirstFirst ... 111718192021
Results 401 to 411 of 411

Thread: Optimization Tips

  1. #401
    hippie hater Cimmerian's Avatar
    Join Date
    Oct 2006
    Location
    over there
    Posts
    599
    Rigth, in the code you posted on that thread you showned that this circle to circle without sqrt you had the collisionRange pre calculated BEFORE any test,in this way
    Code:
    var collisionRange:Number = (heroRadius + enemyRadius) * (heroRadius + enemyRadius);

    now that makes sense and that is a lot diferent from this
    Code:
    function checkCollision(circle1, circle2) {
    circle1Radius = circle1._width>>1;
    circle2Radius = circle2._width>>1;
    xdist = circle2._x-circle1._x;
    ydist = circle2._y-circle1._y;
    return xdist*xdist+ydist*ydist<Math.pow(circle1Radius+circle2Radius, 2);
    }
    wich drops the sqrt but got to have a added step of find the pow of(circle1Radius + circle2Radius), so them becomes a matter of what is faster, pow vs sqrt
    And is also diferent to this,
    Code:
    var radius1 = (clip1._width * clip1._width)/2
    var radius2 = (clip2._width * clip2._width)/2
    var radius = radius1 + radius2
    wich seems a odd way to precalculate the collisionRange and gives an error, as i shown in the fla i have posted before
    So, again, for this to work you have to pre calculate all the combinations of summs of all radius, and even this will not be enough if you will have dynamic things with diferent sizes added at runtime...well...at first i was think it could be very dificult but now maybe you could just build a function that makes the calculation of that and put the result in a array and that is called whenever a new dynamic enemy is created, so it finds the new pow of the summ of the new radius plus any other radius and put on that array...
    Last edited by Cimmerian; 01-13-2007 at 06:04 AM.

  2. #402
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    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.

  3. #403
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    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?

  4. #404
    Senior Member webgeek's Avatar
    Join Date
    Sep 2000
    Posts
    1,356
    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.

  5. #405
    Humble Flash Newbie
    Join Date
    Feb 2007
    Posts
    107
    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...

  6. #406
    Junior Member
    Join Date
    Oct 2008
    Posts
    2

    Question 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

  7. #407
    Senior Member lapo73's Avatar
    Join Date
    Jun 2003
    Location
    italy
    Posts
    395
    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
    www.gotoandplay.it
    Flash game developers community
    www.smartfoxserver.com
    Multiplayer Flash Server

  8. #408
    Junior Member
    Join Date
    Oct 2008
    Posts
    2

    Thumbs up Ok!

    Hey Lapo!!!

    That was such a great article, and believe me, I learned a lot from it!!!

    I think I'll upgrade to AS3. I've done some research I've noticed that there are some new techniques that can be applied to game development, so I'll have to start all over again. Here's what I found, maybe it can become useful to someone:

    Asteroids game AS3 tut: http://www.8bitrocket.com/newsdispla...?newspage=8141

    Asteroids game AS3 optimization: http://www.8bitrocket.com/newsdispla...newspage=10248

    Thank you Lapo!!! I'll put my hands over AS3...

    GT

  9. #409
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501

    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. #410
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,367
    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.

  11. #411
    Senior Member
    Join Date
    Apr 2002
    Location
    UK
    Posts
    501
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center