Setting an x value of an entity CRASHES the game if it's too low/high [SOLVED]


(John Andersson) #1

Hi.

I am currently using a very primitive way of holding weapons in the game, I will change it later.

Atm, it’s like this:

override public function update():void 
	{
		var e:Hero = collide("hero", x, y) as Hero;
		
		
		if (Hero.isFacingLeft)
		{
			sprSword.flipped = true;
		}else
		{
			sprSword.flipped = false;
		}
		//Make hero pick it up
		if (!pickedUp)
		{
			if (e)
			{
				pickedUp = true;
			}
		}
		
		if (pickedUp)
		{
			if (Hero.isFacingLeft)
			{
				x = e.x - 99;
			}else {
				x = e.x +99;
			}
			y = e.y + 10;
		}
blablabal
}

if x = e.x - 99;, then the game crashes! and if I change

if (Hero.isFacingLeft)
			{
				x = e.x - 99;
			}else {
				x = e.x +99;
			}

to

if (Hero.isFacingLeft)
			{
				x = e.x - 99;
			}else {
				x = e.x +100;      //look here
			}

then it also crashes. It’s so weird!


(Jacob Albano) #2

I’m absolutely certain that it has nothing to do with the magnitude of the number. The Number class in as3 is an IEEE double-precision numeric value, which can hold up to 10^308, or

100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

There’s no possible way you’re exceeding that. :wink:

What’s more likely is that you’re doing a null check on some parts of your code, but not others. I see that isPickedUp is a member variable, and its value persists across updates. You’re probably running into situations where there’s no collision but isPickedUp is still true, and you access a member of e, which is null at that time.


(John Andersson) #3

But it works as long as it is less than 99 and higher than -99!


(John Andersson) #4

Which might be because of isPickedUp, now that I think of it… Lemme try something!

Yep. That was it. Thank you once again :smiley:


(Ultima2876) #5

Be careful of making assumptions when debugging. In programming you’ll often find that the thing that seems most obvious (ie changing the number above 99 breaks my game! It must be a number problem!) is not actually the root cause of the issue. An unfortunate side effect of that is you’ll often ‘fix’ a bug by changing something that isn’t actually the problem at all but just happens to look like it has fixed it… for the bug to come back later and really cause you trouble.


(John Andersson) #6

Good advice! :frowning: