A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Number returning different lengths

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    25

    Number returning different lengths

    Hello,

    I'm trying to track one of my characters as it moves across my stage, however it seems that samuraiChar's (my character) x position has a much higher decimal rounding system in place rounding to maybe 8 decimals where as the over object that I'm trying to track with it has only a 2 d.p rounding system. Can someone explain to me why this is and how I can fix it, also I may be wrong I'm not 100% sure if this is the problem.

    Thanks, heres the code.

    This method is sending the variable to the samuraiChar
    Code:
    	public function moveSamurai():void{
    			if(samuraiXDirection == 1){
    				if(samuraiVelocity < Util.maxSpeed){
    					samuraiVelocity += Util.acceleration;
    				}
    			}
    			else if(samuraiXDirection == 2){
    				if(samuraiVelocity > -(Util.maxSpeed)){
    					samuraiVelocity -= Util.acceleration;
    				}
    			}
    			if(samuraiVelocity >= 0.5 || samuraiVelocity <= -0.5)
    				samuraiVelocity = samuraiVelocity * Util.speedDecay;
    			else samuraiVelocity = 0;
    			samuraiXPos += samuraiVelocity;
    			SamuraiChar.move(samuraiVelocity, samuraiYMove, samuraiXDirection);
    		}
    Code:
    public function move(x:Number, y:Number, moving:int):void{
    			if(x > 0 && moving > 0){
    				currentlyAttacking = false;
    				facingLeft = false;	
    				currentlyMoving = true;				
    				if(samuraiXPos < currentLevelWidth + (Util.PLAYABLE_WIDTH/2)){
    					if(moveToEdge){
    						samuraiChar.x += x;
    						collisionBox.x += x;
    					}
    					samuraiXPos += x;
    				}
    			}
    			if(x < 0 && moving > 0){
    				currentlyAttacking = false;
    				currentlyMoving = true;
    				facingLeft = true;
    				if(samuraiXPos > 0){
    					if(moveToEdge){
    						samuraiChar.x += x;
    						collisionBox.x += x;
    					}
    					samuraiXPos += x;
    				}
    			}
    			if(moving == 2)
    				samuraiChar.gotoAndStop(4);
    			else if(moving == 1)
    				samuraiChar.gotoAndStop(3);
    			if((x == 0 || moving == 0) && ((samuraiXPos + x) >= 0 && (samuraiXPos + x) < (currentLevelWidth + (Util.PLAYABLE_WIDTH/2)))){
    				if(moveToEdge){
    					samuraiChar.x += x;	
    					collisionBox.x += x;
    				}
    				samuraiXPos += x;
    				currentlyMoving = false;
    			}
    			trace(samuraiChar.x);
    			trace(collisionBox.x);

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Are the traces in question those at the bottom? Number is a Number is a Number, regardless of how it's formatted for display. Number is a double-precision floating point value, which means that it is not perfectly exact, and you cannot perfectly represent any Number to arbitrary precision. It's definitely good enough for this, though. You may see values like 3.000000001 when you expect 3, but it's going to display in the same place on the screen anyway. However, if you know you are dealing only with integer values, I'd suggest using int instead of Number because that will always be a true integer value and there are many performance benefits to pixel-aligning your display objects.

    Why are you passing y to move, and doing nothing with it?

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    I didn't include y because it has nothing to do with it. I understand that Number can be any length however for some reason it is returning 2 different lengths for each variable, as if the variables have a set length that they are rounded to. samuraiChar.x is always an unrounded Number when traced, whereas the collisionBox.x is always rounded to a whole number or 2 d.p, which is slightly offsetting it from the samuraiChar each time it changes position.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    Posts
    25
    Its incredibly weird and i have no idea whats going on. I also tried rounding x to 2d.p to no effect.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center