I *think *the answer to this is a case by case basis but sometimes i wonder if i should check if a property needs to be assigned, or should i just assign it regardless. Specifically in the main game loop, so its happening constantly.
take this example of flipping the players scaleX when traveling the opposite direction:
var vx:Number=1;//velocity x
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
function enterFrameHandler(e:Event)
{
if (vx < 0 && player.scaleX != -1)
{
player.scaleX = -1;
}
else if (vx>=0 && player.scaleX!=1)
{
player.scaleX = 1;
}
}
I believe booleans are among the fastest check right? so perhaps this would be slightly faster
var flipped:Boolean;
function enterFrameHandler(e:Event)
{
if (vx < 0 && !flipped)
{
flipped=true;
player.scaleX = -1;
}
else if (vx>=0 && flipped)
{
flipped=false;
player.scaleX = 1;
}
}
but sometimes i say **** it and just assign it every frame whether it needs it or not.
function enterFrameHandler(e:Event)
{
if (vx < 0)
{
player.scaleX = -1;
}
else
{
player.scaleX = 1;
}
}
with velocity of course player will slow down before actually changing directions, otherwise i would assign the scaleX once on a key press or event triggering the turn around.
Thanks for any thoughts on this. In this example none of them are going to create a noticeable slow down (i don’t think). Sometimes i find myself doing similar things and i don’t have a uniformed method on the matter. I guess the best answer is, what ever will keep my game loop function running fastest? (in an average state that is)