[SOLVED] Stack Problem in Conditions


(Bora Kasap) #1

Debugger shows stack error in if statement CONDITIONS line: i can’t realize why it is happening.

nearestenemy = FP.world.nearestToPoint("enemy", Input.mouseX, Input.mouseY);
if (magnetpower > 0 && GLOBALS.enemycount > 0 && nearestenemy.onCamera && FP.distance(nearestenemy.x, nearestenemy.y, Input.mouseX, Input.mouseY) < magnetpower)
    			{
    				targetlock = nearestenemy;
    			}

(Jacob Albano) #2

I’m guessing that nearestenemy is null, which would be the case if there are no enemies in the world. You should check against that first:

nearestenemy = FP.world.nearestToPoint("enemy", Input.mouseX, Input.mouseY);
if (nearestenemy != null)
{
    //  your conditions here
}

I should point out that (in Flashdevelop) you can hover your mouse over any variable after an error or breakpoint and see its value.


(Bora Kasap) #3

It looks like worked. But let me ask a question, i’ve tried to use “if(nearestenemy)” before, so if it is not the same thing with “if(nearestenemy != null)” could you explain me what is difference between “if(nearestenemy)” and “if(nearestenemy != null)”


(Jacob Albano) #4

In flash, there’s no difference between the following:

if (object) {}
if (object != null) {}

I prefer to explicitly test against null because it’s obvious that the variable you’re testing is an instance of a class, not a boolean. In example code this is particularly helpful. In addition, Haxe, another language I use a lot, doesn’t allow you to test the first way – you must explicitly test for null or you get a compile error. Doing it this way keeps me from getting used to a way that doesn’t work in both environments. :wink: