Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

question about my slide algorithm

Status
Not open for further replies.

iostream71

Programmer
Mar 13, 2002
229
US
this is how i've been doing slides:

make a MC with whatever inside it. and have this for the actionscript

onClipEvent(load){
_x=-100;
_y=0;
targetX=-50;
targetY=0;
speed=3;
}

onClipEvent(enterFrame){
_x += (targetX - _x)/speed;
}

my question is, sometimes the sliding is choppy, like my computer is lagging. was wondering if maybe the constant enterframe part was taxing my computer somehow? i dunno
 
If you're using the same script on several clips at once it could be hitting your CPU pretty hard. This is due to the equation never reaching zero: the distance between the clip and target just get smaller and smaller even though we can't see the differences as they're tiny fractions of a pixel.

It might be useful to have a cut-off on the script so that it stops executing once the difference between the clip's position and the target position are within one pixel.

if(Math.abs(targetX-_x)>=1){
_x += (targetX - _x)/speed;
}

... which saves a few processor cycles, although when there's only one line of code executing that saving is minimal because the condition being evaluated hits the processor too.

In MX you can clear an enterFrame event after it's completed which is a real saving:

this.onEnterFrame = function() {
if (Math.abs(targetX-_x)>=1) {
_x += (targetX-_x)/speed;
} else {
this.onEnterFrame = null;
}
};



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top