RAO
RAO

Reputation: 71

Body and Sprite Positions

When I compile my hero doesn't touch the floor but stops anyways a few pixels above. I figured if I traced both bodies and their respective sprites I'd know which ones aren't coincinding.

trace("Hero: ", hero.position.y, "/", heroSprite.y);
trace("Floor: ", floor.position.y, "/", floorSprite.y);

I get the following,

Hero: 470.2(...) / 470.2

Floor: 0 / 0

Also, how is the floor position 0 in its y property when:

createWall(stage.stageWidth/2, 500, 100, 30); //(y = 500)

I read that while the nape body 'registration point' is in the middle, the sprite one is in the upper-left corner so when giving the sprite the same x and y of the body it won't match. Below the sprite will be out of position.

public function createWall(x:Number, y:Number, width:Number, height:Number):void
        {
            wall.shapes.add(new Polygon(Polygon.rect(x, y, width, height)));
            wall.space = space;

            wallSprite.graphics.beginFill(0x000000);
            wallSprite.graphics.drawRect(x, y, width, height);
            wallSprite.graphics.endFill;
            addChild(wallSprite);   

            wall.userData.sprite = (wallSprite);
            addChild(wall.userData.sprite);
        }

I tried wallSprite.graphics.drawRect(-width/2, -height/2, width, height); but didn't work. Althought I believe the problem is there, placing the sprite properly.

Upvotes: 1

Views: 116

Answers (1)

BotMaster
BotMaster

Reputation: 2223

Drawing does not affect the position of an object. In your case the wall is at 0,0 and you draw at x:stage.stageWidth/2 , y: 500 but that's not going to become the wall coordinates, those are still 0,0 anyway.

Upvotes: 1

Related Questions