Reputation: 116
I would like simulate road in my game, and for this i use road's lanes and traffic, lanes and traffic dynamic created like this
if(roadlineTimer-- == 0){
roadlineTimer = 30;
newRoadline = new Roadline();
newRoadline.x = 0;
newRoadline.y = 0;
newRoadline.speed = 3;
roadlines.push(newRoadline);
addChildAt(newRoadline,numChildren-1);
}
But when complile and run this code. I have visible delays. My lane likes as rectangle and have own class. How I can optimize this code
Upvotes: 0
Views: 99
Reputation: 1094
It's hard to tell by just this snippet alone, but a big part of the delay could be object instantiation (new Roadline()), because that is a very expensive operation (CPU-wise). You can eliminate it if you create the max number of Roadlines before the start, and then just re-use them as the need arises.
Google 'object pool' for some in-depth discussion, and you may even stumble upon some ready-made flash solutions (personally, I use object pool from http://lab.polygonal.de/2008/06/18/using-object-pools/)
Upvotes: 0
Reputation: 9572
it looks like the problem is elsewhere , there's not enough here to go by...
on the other hand , if you're going to do this
addChildAt(newRoadline,numChildren-1);
you may as well do this
addChild(newRoadline);
although i doubt this will solve your delay problem
Upvotes: 1