How to set the position of a sprite


(Brendyn Todd) #1

I’ve tried to find out how to do this but I can’t figure it out.

Basically I want to set a sprite with animations to a specific x/y coordinate. How can this be done?

Main.as

package  
{
	import net.flashpunk.World;
	/**
	 * ...
	 * @author Brendyn Todd
	 */
	public class TestingWorld extends World
	{
		
		public function TestingWorld() 
		{
			add(new torch());
		}
		
	}

}

torch.as

package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.graphics.Spritemap;
	/**
	 * ...
	 * @author Brendyn Todd
	 */
	public class torch extends Entity
	{
		[Embed(source = 'assets/Torch.png')] private const TORCH:Class;
		
		public var sprTorch:Spritemap = new Spritemap(TORCH, 16, 16)
		
		public function torch() 
		{
			sprTorch.add("lit", [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], 20, true);
			graphic = sprTorch;
			sprTorch.play("lit");
			
		}
	}
}

(JasperYong) #2

sprTorch.originX = 10; sprTorch.originY = -10;

sprTorch.centerOrigin(); //automatically center the origin (half width, half height)


(Wayne Makoto Sturdy) #3

There are a number of ways you could do it.

You could add x and y variables in your Torch constructor and place it that way:

package  
{
    import net.flashpunk.Entity;
    import net.flashpunk.graphics.Image;
    import net.flashpunk.graphics.Spritemap;
    /**
     * ...
     * @author Brendyn Todd
     */
    public class torch extends Entity
    {
        [Embed(source = 'assets/Torch.png')] private const TORCH:Class;

        public var sprTorch:Spritemap = new Spritemap(TORCH, 16, 16)

        public function torch(x:Number = 0, y:Number = 0) 
        {
            sprTorch.add("lit", [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], 20, true);
            graphic = sprTorch;
            sprTorch.play("lit");
            
            this.x = x;
            this.y = y;
        }
    }
}

And then in your world you pass the x/y vals to the torch constructor:

add(new torch(64, 128));

Another way would be to create a temporary variable linked to the instance of the torch:

package  
{
    import net.flashpunk.World;
    /**
     * ...
     * @author Brendyn Todd
     */
    public class TestingWorld extends World
    {

        public function TestingWorld() 
        {
            // no need for parentheses if no parameters are being passed 
            var t:torch = new torch;
            t.x = 64;
            t.y = 128;
            add(t);
        }
    }
}

These should give you something to digest, let us know how it goes.


(Brendyn Todd) #4

YES! It worked perfectly! :smiley: Thanks for the help!