PreRotation help


(Ricesteam) #1

I’m probably not understanding the purpose of this class because my image asset is not rotating in run time.

For example, my usage in a constructor of an entity:

var rotatedImage:PreRotation = new PreRotation(Assets.SPR_IMG);
rotatedImage.angle = 45;
graphic = rotatedImage;

When I run the game, the entity’s graphic is not rotating.

Did I miss something or am I way off?


(Alex Larioza) #2

How are you actually rotating the image? The code you provided only shows how you created the prerotation object.


(Ricesteam) #3

I edited my post.

I was under the impression that I could use the PreRotation to animate the rotation of an entity during render.


(Wayne Makoto Sturdy) #4

FP has the ability to rotate the graphics of any Entity so you don’t necessarily need to use the Prerotation class if all you want to do is rotate an Entity’s graphic property.

In most instances you would probably extend Entity and have a variable hold the graphic resource that is your Entity's graphic and provide a getter and setter for it’s angle like so:

package
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	
	public class Enemy extends Entity
	{
		protected var _gfk:Image;
		
		public function Enemy()
		{
			super();
			
			graphic = _gfk = new Image(Assets.SPR_IMG);
			_gfk.centerOO();
		}
		
		public function set angle(angle:Number):void
		{
			_gfk.angle = angle;
		}
		public function get angle():Number
		{
			return _gfk.angle;
		}
	}
}

Then somewhere (like in your world class) you can adjust the angle like so:

_enemyInstance.angle = 45;

This would just set the rotation of the graphic to 45 degrees so you wouldn’t see any animation. To see it rotate as an animation you would need to adjust the angle over n frames, most likely you would do this in your World's update override like so:

override public function update():void
{
    super.update();
    _enemyInstance.angle++;
}

With all that said, the Prerotation class will take in your graphic and generate a sort of runtime Spritesheet of your graphic rotated at n angles depending on the frame count. The default is 36 so it will create a 36 frame Spritesheet where each frame is your graphic rotated 10 degrees (360 / 36 = 10). You can use this Protation type in place of the Image type in the same code above.

Setting the angle of the Entity's graphic will still be done in the same way except that the graphic will show only those prerendered rotations. So lets say that on any given frame the angle property of the Entity's graphic is 12.265 degrees, it will display the frame that is closest to that value, so in this case it would be the frame rendered at 10 degrees (assuming were using the default frame count).

The Prerotation class is usefull for reducing calculation in the game loop. Blitting a prerendered image is a lot faster than having to calculate the transformation of an image before blitting, but know that you trade memory for speed with this technique.


(Ultima2876) #5

PreRotations also don’t look as good. Honestly these days it’s better just to use the regular rotation, because PreRotations don’t provide so much of an advantage that they’re worthwhile.

There is of course an exception to every rule; if you’re doing stuff with tons of rotating sprites (100+ entities all rotating, like a particle effect) you would be better using a PreRotation for those.