Tuesday, March 23, 2010

High CPU usage and program slowing down

Hi. I have helped write a program in AS3 that uses classes. We have 2 frames in our movieclip that consists of an ant moving. We have a class of ants and can create 15 instances of them and the program runs fine with the ants walking around looking for food and going back to a corner that is their nest and going back out again. The ants also respond to mouse movement if the mouse pointer is near an ant.

If we create more than 15 ants, then the program starts to slow down. If we have 30 ants on the screen, then there is noticable stuttering of the ant movement. The CPU usage on a P4 2.5Ghz Pentium is around 50%.

Is this normal? or is something wrong?

I would have thought we would be able to scale up the number of ants easily.

Thanks

Trev.

High CPU usage and program slowing down

it's normal to have performance issues if there are many (hundreds) of displayobjects but not 30 unless there are coding issues.聽 my guess is you could enable your ant's cacheAsBitmap property and see improvement in performance.聽 and my guess is each ant is using a loop that is unnecessary:聽 you should have a controller class with one loop that updates all the ant instances.

High CPU usage and program slowing down

Hi kglad.

Thanks for the reply.

I assume enabling cacheAsBitmap is set in the ant class Constructor, as we have tried that and it has helped, see below. We can now create 30 ants with very slight stuttering.

We have an聽 ant class as below.

public class TAnt extends MovieClip { // lots of code here }

ant Constructor as below.

public function TAnt(newX: Number, newY: Number, sW: Number, sH: Number, rCyc: Number) {
聽聽聽聽聽聽聽聽聽聽聽 this.cacheAsBitmap = true;

聽聽聽聽聽聽聽聽聽聽 // more code here }

// We have implemented a Finite State Machine for the ant behaviour.

regarding the ants using a loop and using a controller class, I am not sure what you mean.

We are creating instances of the ants in the Ants.fla file which is in the Action file and add each new ant to an array. See below.

for (var a = 0; a %26lt; nAnts; a++) {
聽聽聽 var newAntX = Math.random() * screenWidth;聽聽聽聽聽聽聽 // randomly generate ant location
聽聽聽 var newAntY = Math.random() * screenHeight;
聽聽聽 var rCyc = Math.random() * 800;
聽聽聽 var ant = new TAnt(newAntX, newAntY, screenWidth, screenHeight, rCyc);聽聽聽聽聽聽聽聽聽聽聽 // create ant
聽聽聽 addChild(ant);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 // add ant so visible on screen
聽聽聽 ants.push(ant);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 // add ant to array
}

So we use an array for looping through the ants in the Ants.fla Action file.

Is this the way you mean?

Thanks

Trev.


what causes an ant to update their position and behavior?


After we add the ants to an array in the Actions file, we have this code next. It has a Transitions and Action methods that it calls on for each ant in the loop.

stage.addEventListener(Event.ENTER_FRAME, main);

function main(e: Event) {聽聽聽 // Main Method
聽聽聽 for (var i = 0; i %26lt; nAnts; i++) {聽聽聽聽聽聽聽聽聽聽聽 // loop through ants processing transition and actions
聽聽聽聽聽聽聽 var pX = person.PersonX;
聽聽聽聽聽聽聽 var pY = person.PersonY;
聽聽聽聽聽聽聽
聽聽聽聽聽聽 // ants[i].Transition(ants, food, pX, pY);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //detecting person
聽聽聽聽聽聽 // ants[i].Action(pX, pY);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 // in place of mouse
聽聽聽聽聽聽聽
聽聽聽聽聽聽 ants[i].Transition(ants, food, mouseX, mouseY);聽聽聽聽聽聽聽 //detecting mouse
聽聽聽聽聽聽 ants[i].Action(mouseX, mouseY);聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //in place of person
聽聽聽 }

I have included a text file version of the Ant.as file. This may explain better.

Thanks

Trev.


that looks good.聽 i thought you might have an enterframe loop in the ant class.


I don't know squat about this but I'm still curious. If you use Timer instead of EnterFrame could that help?


Going back to a previous question, I assume enabling cacheAsBitmap is set in the ant class Constructor?

Thanks

Trev


yes, that should work.


doghouseJim. Quote ''If you use Timer instead of EnterFrame could that help?''

I have no idea if it would or how to implement it. Can you elaborate any further please?

Thanks

Trev


I've only played around with them in a tutorial once but I know theyre sometimes used in animations.聽 I'm uploading a file thats a photocopy of a page from Essential ActionScript 3.0 you might find useful.


a timer loop won't run more efficiently than enterframe loop.


We appear to have solved the problem of the stuttering images. It turned out the images we used were created in Photoshop and they are made up of layers. All these layered parts were included in the final images used ( hope that makes sense). We swapped these out for a final image without the included layers and instantly we could have 100 or more ants running around on the screen no problems. And the CPU usage has dropped right down.

Thank you all for your input.

Trev.


you're welcome.

No comments:

Post a Comment