Demo Showing Scaling and Rotation
Click on the stage below to do some hotdog moves with my guitar. Use the arrow keys to rotate and move the guitar, or press plus and minus to enlarge and shrink the guitar.
The code for this demo is presented at the bottom of the page - switch the guitar for a spaceship and you almost have yourself an asteroids game. If you were creating a game or an application, you could prerender all the different rotations and scales ahead of time to speed things up. In this simple example, I am rendering everything in real time.
Source Code
package {
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
public class Guitar extends Sprite {
var loader : Loader = new Loader();
var guitar : Sprite = new Sprite();
var guitarB : Bitmap;
var guitarBD : BitmapData;
public function Guitar() : void {
// initialize everything once guitar is loaded
public function loaded(e : Event) {
// main loop
public function mainLoop(e : Event) {
}
}
import flash.display.Sprite;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.net.URLRequest;
import flash.events.Event;
import flash.events.MouseEvent;
public class Guitar extends Sprite {
var loader : Loader = new Loader();
var guitar : Sprite = new Sprite();
var guitarB : Bitmap;
var guitarBD : BitmapData;
public function Guitar() : void {
// load the guitar
loader.load(new URLRequest("GuitarRed.png"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
}loader.load(new URLRequest("GuitarRed.png"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
// initialize everything once guitar is loaded
public function loaded(e : Event) {
// create bitmap of guitar
guitarBD = Bitmap(loader.content).bitmapData;
guitarB = new Bitmap(guitarBD);
guitarB.smoothing = true;
// make guitar centered in the sprite
guitarB.x = -(guitarB.width / 2);
guitarB.y = -(guitarB.height / 2);
guitar.addChild(guitarB);
addChild(guitar);
// center the guitar on the stage
guitar.x = stage.stageWidth / 2;
guitar.y = stage.stageHeight / 2;
// add keyboard handler
key.initKey(stage);
// also make the guitar draggable by the mouse
guitar.addEventListener(MouseEvent.MOUSE_DOWN, function (e : MouseEvent) { guitar.startDrag() } );
guitar.addEventListener(MouseEvent.MOUSE_UP, function (e : MouseEvent) { guitar.stopDrag() } );
// add enter frame handler
stage.addEventListener(Event.ENTER_FRAME, mainLoop);
}guitarBD = Bitmap(loader.content).bitmapData;
guitarB = new Bitmap(guitarBD);
guitarB.smoothing = true;
// make guitar centered in the sprite
guitarB.x = -(guitarB.width / 2);
guitarB.y = -(guitarB.height / 2);
guitar.addChild(guitarB);
addChild(guitar);
// center the guitar on the stage
guitar.x = stage.stageWidth / 2;
guitar.y = stage.stageHeight / 2;
// add keyboard handler
key.initKey(stage);
// also make the guitar draggable by the mouse
guitar.addEventListener(MouseEvent.MOUSE_DOWN, function (e : MouseEvent) { guitar.startDrag() } );
guitar.addEventListener(MouseEvent.MOUSE_UP, function (e : MouseEvent) { guitar.stopDrag() } );
// add enter frame handler
stage.addEventListener(Event.ENTER_FRAME, mainLoop);
// main loop
public function mainLoop(e : Event) {
var radians : Number;
if (key.k[key.LEFT]) { guitar.rotation -= 5; }
else if (key.k[key.RIGHT]) { guitar.rotation += 5; }
if (key.k[key.UP]) {
else if (key.k[key.DOWN]) {
if ((key.k[key.PLUS] || key.k[key.EQUAL])&& guitar.scaleX <= 2.5) {
else if ((key.k[key.MINUS] || key.k[key.UNDERSCORE])&& guitar.scaleX > 0.5) {
if (guitar.x < 0) guitar.x = 0;
else if (guitar.x > stage.stageWidth) guitar.x = stage.stageWidth;
if (guitar.y < 0) guitar.y = 0;
else if (guitar.y > stage.stageHeight) guitar.y = stage.stageHeight;
}if (key.k[key.LEFT]) { guitar.rotation -= 5; }
else if (key.k[key.RIGHT]) { guitar.rotation += 5; }
if (key.k[key.UP]) {
radians = ((guitar.rotation + 180) * Math.PI) / 180;
guitar.x += 5 * Math.cos(radians);
guitar.y += 5 * Math.sin(radians);
}guitar.x += 5 * Math.cos(radians);
guitar.y += 5 * Math.sin(radians);
else if (key.k[key.DOWN]) {
radians = (guitar.rotation * Math.PI) / 180;
guitar.x += 5 * Math.cos(radians);
guitar.y += 5 * Math.sin(radians);
}guitar.x += 5 * Math.cos(radians);
guitar.y += 5 * Math.sin(radians);
if ((key.k[key.PLUS] || key.k[key.EQUAL])&& guitar.scaleX <= 2.5) {
guitar.scaleX += 0.1;
guitar.scaleY += 0.1;
}guitar.scaleY += 0.1;
else if ((key.k[key.MINUS] || key.k[key.UNDERSCORE])&& guitar.scaleX > 0.5) {
guitar.scaleX -= 0.1;
guitar.scaleY -= 0.1;
}guitar.scaleY -= 0.1;
if (guitar.x < 0) guitar.x = 0;
else if (guitar.x > stage.stageWidth) guitar.x = stage.stageWidth;
if (guitar.y < 0) guitar.y = 0;
else if (guitar.y > stage.stageHeight) guitar.y = stage.stageHeight;
}
}