Hi folks.
The game i am working on is acting like a naughty little boy and using way too much processing power. I have decided that it is these functions shown below. Basically what is happening is a ball is moving around (“wheel1”) using the APE engine and a target is fixed in position. I need an event to trigger if the ball becomes stationary, or comes within a set distance of the target.
Here’s the code i have now:
// Wheel speed check
private function checkWheelSpeed():void
{
var vel:Vector = wheel1.velocity; // returns a Point, basically with vx and vy
var absVelX:Number = Math.abs (vel.x);
var absVelY:Number = Math.abs (vel.y);
if (absVelX < 0.4) // checks if they are below a threshold
if (absVelY < 0.4)
{
stoppedCounter++;
// stoppedCounter stops it registering a "stop" at the apex of a vertical bounce
// where the velocity is in fact zero but i don't want it to think it's a stop.
if (stoppedCounter >= 20)
{
// do stuff that says it's stopped
}
}
}
// Check distance to target function.
// I think this is more likely to be the cpu hog.
private function checkTargetDistance():void
{
var dx:Number = Math.abs(targetPoint.x - wheel1.px);
var dy:Number = Math.abs(targetPoint.y - wheel1.py);
if (dy < 35)
if (dx < 35)
{
// do stuff to say it's at the target.
}
}
These functions are both running every frame (or actually every time a 20 ms timer ticks) whilst the engine is running. I didn’t think they were that cpu intensive, but there is nothing else that can be causing the slowdown really.
If anyone can suggest ways to optimise them then that would be awesome. I suspect the Math.abs has something to do with it, but casting to an int won’t work because the values are often quite small, i.e. decimals.