Rotating a spritemap properly? [SOLVED]


(John Andersson) #1

Hi. I have a fireball class that is added whenever a mage is firing.

package
{ import net.flashpunk.Entity; import net.flashpunk.Graphic; import net.flashpunk.graphics.Image; import net.flashpunk.Mask; import flash.geom.Point; import Math; import net.flashpunk.graphics.Spritemap;

public class Fireball extends Entity 
{
	[Embed(source = "assets/Fireball.png")] private const FIREBALL:Class;
	
	public var sprFireball:Spritemap = new Spritemap(FIREBALL, 96, 56);
	
	private var graphicfx:Image;

	private var fireBallSpeed:Number = 14;
	
	public function Fireball(x:Number=0, y:Number=0) 
	{
		super(x, y);

		layer = 1;
		
		graphicfx = new Spritemap(FIREBALL);
		graphic = sprFireball;
		
		sprFireball.add("flying", [9, 10, 11, 12, 13, 14, 15, 16, 17], 20, true);
		
		graphicfx.angle = Math.atan2((Hero.yPos - y), (Hero.xPos - x)) * 180/ Math.PI + 90;
	}
	
	override public function update():void
	{
		sprFireball.play("flying")
		x -= Math.sin(graphicfx.angle / -180 * Math.PI) * (fireBallSpeed);
		y -= Math.cos(graphicfx.angle /-180*Math.PI)*(fireBallSpeed);
	}
	
}

}

The thing is, the graphic itself always looks non-rotated, even if the direction it’s travelling at is correct. Can anyone point me in the right direction?


(Per_K) #2

To make an image rotate you simply add to the angle value in the update, i.e.

wheel.angle += 5;

I like to keep the value for the angle between 0 and 360, so I also would add

if (wheel.angle <360)
wheel.angle -= 360;

You will probably also want to set the point that the image should rotate around to the middle of the image (i.e. if the image is 52x52)

wheel.originX = 26;    
wheel.originY = 26;

or it will rotate around the image’s x/y values.


(Ultima2876) #3

I’d like to add that it’s always worth multiplying by FP.elapsed for rotations/motion. Even if you use a fixed timestep, if you feel like switching to a variable timestep later it means less work.


(John Andersson) #4

Hmmm. I must say I am a bit confused. The code currenty does this:

It fires off a fireball in the direction of the hero. It then goes towards him properly. If the hero moves, it doesn’t change direction, as I put it in the constructor. All is fine so far. The only thing that doesn’t work is the sprite itself rotating visually. It is never rotating. Only the direction it’s going at is working, not the visual rotation. I even tried adding “grahicfx.angle += 5”, which made it go around in a circle path, but the sprite itself didn’t rotate, only the movement did.

I’m thinking “angle” doesn’t affect spritemaps visual rotation…?


(John Andersson) #5

Ok, I made it work.

sprFireball.angle += 5; was what I forgot to add. Thank you all for your input :slight_smile: