-
Notifications
You must be signed in to change notification settings - Fork 20
Rotate and scale
Alarms and Tweens is only a way to change your entity behaviour: you can change position (and many other properties), but MarteEngine help you in manipulating angle of your Entity and Scale.
Rotate
Rotate an entity is just a matter of change in angle so MarteEngine can rotate Image or Animation associated to Entity. We do this defining an alarm on entity, to change angle of Entity:
public class RotateEntity extends Entity { private static final String ROTATE_ME = "rotate_me"; public RotateEntity(float x, float y, Image image) { super(x, y, image); // we set that image or animation must be rotate with Entity setCentered(true); // we define and alarm so we can rotate entity every 2 seconds setAlarm(ROTATE_ME, 2, false, true); } @Override public void alarmTriggered(String name) { if (name.equalsIgnoreCase(ROTATE_ME)) { // let's rotate a bit int angle = this.getAngle(); angle += 2; if (angle >= 360) angle -= 360; this.setAngle(angle); } } }
We can mark on setCentered(true) in entity constructor: you must let know to MarteEngine that image or animation associated to entity rotate with it.. in some cases you simply don't want this behaviour.
Scale
Scale entity image or animation is not so different: just change scale properties on Entity:
public class ScaleEntity extends Entity { private static final String SCALE_ME = "scale_me"; private float scaleDir = -0.1f; public ScaleEntity(float x, float y, Image image) { super(x, y, image); // we define and alarm so we can scale entity every 2 seconds setAlarm(SCALE_ME, 2, false, true); } @Override public void alarmTriggered(String name) { if (name.equalsIgnoreCase(SCALE_ME)) { // let's scale entity this.scale += scaleDir; if (this.scale <= 0.1f || this.scale >= 2.0f){ scaleDir *= -1; } } } }
In this example we are scaling entity image from a minimum of 0.1 to 2.0 of his original width and height!
MarteEngine version 0.2