package { import flash.display.*; import flash.events.*; import flash.net.*; import flash.ui.*; import mx.core.BitmapAsset; import flash.text.TextField; public class imageinput extends Sprite { //we are going to load 2 images at runtime private var Image1:Loader; private var Image2:Loader; //we are going to embed the 3rd image into the swf! [Embed(source="iconsuperattack.png")] private var Image3Embeded:Class; private var Image3:BitmapAsset; //how many images are loading? used to know when images are done loading (it will be 0!) private var ImagesLoading:int; //booleans to keep track of which keys are pressed private var ButtonLeftPressed:Boolean; private var ButtonRightPressed:Boolean; private var ButtonUpPressed:Boolean; private var ButtonDownPressed:Boolean; //constructor - called when the program starts public function imageinput() { //add some instructions to the screen var greeting_txt:TextField = new TextField(); greeting_txt.text = "Use the arrow keys to move the sword image"; greeting_txt.width = 600; greeting_txt.x = 0; greeting_txt.y = 300; addChild(greeting_txt); //create the image loaders - not sure if it's better to use one loader per each image //or if it's better to have just one loader that loads each image one after the other. Image1 = new Loader(); Image2 = new Loader(); //set the callbacks for when the images are done loading Image1.contentLoaderInfo.addEventListener(Event.INIT,Image1Loaded); Image2.contentLoaderInfo.addEventListener(Event.INIT,Image2Loaded); //Note that we are waiting for 2 images (just used by input code to make sure our images are loaded //before we try to move them) ImagesLoading = 2; //start the loaders loading the images Image1.load(new URLRequest("iconattack.png")); Image2.load(new URLRequest("iconblock.png")); //grab the embeded image - when you embed images i dont think you can do a loading screen //like you can with the image loaders but not sure Image3 = new Image3Embeded(); Image3.x = 100; Image3.y = 100; addChild(Image3); //register our key down and key up events stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDownListener); stage.addEventListener(KeyboardEvent.KEY_UP, KeyUpListener); //register our frame enter event (ie our tick function that happens once per frame) stage.addEventListener(Event.ENTER_FRAME,Tick); } //called when the first image is done loading private function Image1Loaded (e:Event):void { Image1.content.x = 200; Image1.content.y = 200; addChild(Image1.content); ImagesLoading = ImagesLoading - 1; //i hope action script makes this thread safe! } //called when the second image is done loading private function Image2Loaded (e:Event):void { Image2.content.x = 0; Image2.content.y = 0; addChild(Image2.content); ImagesLoading = ImagesLoading - 1; //i hope action script makes this thread safe! } //called when a key is pressed private function KeyDownListener(e:KeyboardEvent):void { if(e.keyCode == Keyboard.LEFT) ButtonLeftPressed = true; else if(e.keyCode == Keyboard.RIGHT) ButtonRightPressed = true; else if(e.keyCode == Keyboard.UP) ButtonUpPressed = true; else if(e.keyCode == Keyboard.DOWN) ButtonDownPressed = true; } //called when a key is released private function KeyUpListener(e:KeyboardEvent):void { if(e.keyCode == Keyboard.LEFT) ButtonLeftPressed = false; else if(e.keyCode == Keyboard.RIGHT) ButtonRightPressed = false; else if(e.keyCode == Keyboard.UP) ButtonUpPressed = false; else if(e.keyCode == Keyboard.DOWN) ButtonDownPressed = false; } //called once every frame private function Tick(e:Event):void { //only do stuff if our images are loaded if(ImagesLoading == 0) { //if we should move left if(ButtonLeftPressed == true) { Image1.content.x = Image1.content.x - 3; } //if we should move right if(ButtonRightPressed == true) { Image1.content.x = Image1.content.x + 3; } //if we should move up if(ButtonUpPressed == true) { Image1.content.y = Image1.content.y - 3; } //if we should move down if(ButtonDownPressed == true) { Image1.content.y = Image1.content.y + 3; } } } } }