In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

flash on 2012-3-3

Get Adobe Flash player
by Highly 02 Mar 2012
    Embed
/**
 * Copyright Highly ( http://wonderfl.net/user/Highly )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fqzM
 */

package {
    import flash.ui.Keyboard;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.display.Sprite;
    import flash.text.TextField;
    public class FlashTest extends Sprite {
        public function FlashTest() {
            stage.frameRate=30;//set framerate
            //declare constants
            const SW:int = stage.stageWidth;//SW and SH are short for stageWidth and stageHeight, just makes coding more convenient
            const SH:int = stage.stageHeight;
            const txtOff:int = 10;
            const SP:Number = 1;
            const F:Number = 0.97;//friction
            //We're going to set up a textfield for output
            //I always set a constant to handle the offset so it looks nice, you'll see what I mean
            
            var out1:TextField = new TextField();
            out1.selectable = false;//this makes it so you can't select the text
            out1.multiline = true;//this allows for showing multiple lines
            out1.wordWrap = true;//This wraps text around
            out1.x = txtOff;
            out1.y = txtOff;
            out1.width = SW-(txtOff*2);// this makes it so the txt fills the screen
            out1.height = SH-(txtOff*2);
            addChild(out1);
            //we're going to create our sprite now, it's simple and funaljffsfare
            //we're gonna declare some constants for our sprite
            const BW:int = 80;
            const BH:int = 40;
            const BC:int = 0x83BD6B;
            var box:Sprite = new Sprite();
            box.graphics.lineStyle(1,0x000000);
            box.graphics.beginFill(BC);
            box.graphics.drawRect(-1*(BW/2),-1*(BH/2),BW,BH);
            //this draws the box so if the box were to rotate, it would rotate around it's center
            box.x = SW/2;//these two lines put the box in the middle of the screen
            box.y = SH/2;
            var vx:Number = 0;
            var vy:Number = 0;
            addChild(box);
            
            //this is where out ENTER_FRAME evet thing will be
            function step(e:Event):void{
                keys();//in order for the function code to run, yu need to call the function in the enterframe
                col();
            }

            function keys():void{
                //this function isn't necesary, I just like
                //to keep ENTER_FRAME code clean,
                //so I split up my code into functions.
                //This also makes editing really easy
                var outSt:String;
                outSt = "UP: "+kw+"\nDOWN: "+ks+"\nLEFT: "+ka+"\nRIGHT: "+kd;
                
                if(kw){
                    vy-=SP;
                }else if(ks){
                    vy+=SP;
                }else{
                    vy*=F;
                }

                if(kd){
                    vx+=SP;
                }else if(ka){
                    vx-=SP;
                }else{
                    vx*=F;
                }
                outSt+="\n\nxVelocity:  "+decRound(vx,3)+"\nyVelocity: "+decRound(vy,3);
                out1.text = outSt;
            }
            function col():void{
                if((box.x-(box.width/2))>SW){
                    box.x=-1*(box.width/2);
                }else if((box.x+(box.width/2))<0){
                    box.x=SW+(box.width/2);
                }
                if(box.y-(box.height/2)>SH){
                    box.y=-1*(box.height/2);
                }else if(box.y+(box.height/2)<0){
                    box.y=SH+(box.height/2);
                }


                box.x+=vx;
                box.y+=vy;
                if(Math.abs(vx)<0.001){
                    vx=0;
                }
                if(Math.abs(vy)<0.001){
                    vy=0;
                }


                //

            }

            function decRound(x:Number,y:int):Number{
                var pwr:Number = Math.pow(10,y);
                return Math.round(x*pwr)/pwr;
            }

            //we're gonna declare a bunch of boolean variables for detection
            var kw:Boolean = false;//kw stands for "key W", etc etc
            var ks:Boolean = false;
            var kd:Boolean = false;
            var ka:Boolean = false;
            function keyd(e:KeyboardEvent):void {
                //out1.text = e.keyCode+"";
                //w=87 s=83 d=68 a=65
                //nvm lol
                switch(e.keyCode){
                    //the value inside the () is the value it's checking in 'case'
                    case 87:
                        //out1.text = "W was pressed";
                        kw = true;
                        break;
                    case 83:
                        //out1.text = "S was pressed";
                        ks = true;
                        break;
                    case 68:
                        //out1.text = "D was pressed";
                        kd = true;
                        break;
                    case 65:
                        //out1.text = "A was pressed";
                        ka = true;
                        break;
                }

            } 
            function keyu(e:KeyboardEvent):void{
                switch(e.keyCode){
                    //the value inside the () is the value it's checking in 'case'
                    case 87:
                        kw = false;
                        break;
                    case 83:
                        ks = false;
                        break;
                    case 68:
                        kd = false;
                        break;
                    case 65:
                        ka = false;
                        break;
                }
            }

            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyd);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyu);
            addEventListener(Event.ENTER_FRAME,step);
        }
    }
}