X and y increase decrease wrong values[SOLVED]


(billy2000) #1

Hello. So i have my enemy’s speed declared as number. something like this:

private var speed:Number=1;

When the enemy sees player in his front, and he respect a certain condision, like this:

if (collide("player", x, y)&& (x - 40.5) % 17==0)&&(y - 31) % 17==0)

The enemy should have speed from 1 to 1.7.

The problem is that his x for example will not be increassed by 1.7 each frame but by a very weird number becose if i select him in concole and watch his x ,his x is like 122.10000000000011 or something like this.

After some more research i found that any speed that is not a integer number will the have same problem. Any ideas?


(Ultima2876) #2

I don’t understand what you’re trying to do with the modulo and “- 40.5” stuff? If you can explain a little more what you’re trying to do we can help :slight_smile:


(billy2000) #3

well the map begins with x=40.5; and y=31; so i just whant to be sure that enemy see player when he has his x-40.5(the point where map begins) to be divisible with 17 and so for y. The map is practicaly a grid and those 2 conditions are just to help me. But the conditions dont have anything to do with the weird number thos.


(Ultima2876) #4

Numbers in AS3, due to the way they are stored in memory, are slightly inaccurate as you describe. This is a regular issue that you can’t really do anything about - the best way to work around it is to make sure you’re not really relying on exact numbers in your code (or if you are, stick to ints).


(billy2000) #5

I tryed to make enemy see player almost when his x and y is divisible by 17 like this:

//speed before seeing player is 0.85;
if((x - 40.5 % 17 < 1 && y - 31 % 17 < 1)
speed=1.7; //just saying 0.85*20=17 and 1.7*10=17 (whanted enemy's x and y to be divisible with 17 every 17 pixels(every floor block is 17 pixels))

Ok so i made up a code to reput the enemy on the correct x/y when he collide with a wall so he will not go crazzy on the map. the code is like this:

                        if ((x - 40.5) % 17 != 0) {
						intTrans = (x - 40.5) / 17;
						intTrans *= 17;
						if ((x - 40.5) > intTrans) {
							x = intTrans + 40.5;
						}
						if ((x - 40.5) < intTrans) {
							x = intTrans + 40.5 + 17;
						}
					}
					if ((y - 31) % 17 != 0) {
						
						intTrans = (y - 31) / 17;
						intTrans *= 17;
						if ((y - 31) > intTrans) {
							y = intTrans + 31;
						}
						if ((y - 31) < intTrans) {
							y = intTrans + 31+17;
						}
					}

-edit- intTrans declared as int

but i dont know why i am missing something because sometimes when enemy hit a wall it will teleport him back 17 pixels.


(billy2000) #6

Ok so i tested it abit more. The glich appear just when hes moveing from left to right and from up to down. I shall mention that it dosent appear every time tho on those 2 direction that remains.


(billy2000) #7

ok so i added some lines of code to the existing code…this shall be just from up to down cuz the other is almost the same:

                        if ((x - 40.5) % 17 != 0) {
						intTrans = (x - 40.5) / 17;
						intTrans *= 17;
						if ((x - 40.5) > intTrans) {
							x = intTrans + 40.5;
						}
						if ((x - 40.5) < intTrans) {
							x = intTrans + 40.5;
						}
					}
					if ((y - 31) % 17 != 0) {
						
						intTrans = (y - 31) / 17;
						intTrans *= 17;
						if ((y - 31) > intTrans) {
							y = intTrans + 31;
						}
						if ((y - 31) < intTrans) {
							y = intTrans + 31;
						}
					}//added form here
					setHitbox(17, 34, 0, -3)
						if (collide("level", x, y)) {
							
						}
						else {
							y += 17;
						}
					setHitbox(17, 17, 0, -3);

In this case it will add 17 pixels just if it would teleport back 17 pixels…and that would solve the problem xD. Thx guys.