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