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

Lesson 1. Cars

Get Adobe Flash player
by Rzer 01 Dec 2011
    Embed
/**
 * Copyright Rzer ( http://wonderfl.net/user/Rzer )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/hzE0
 */

package {
    import flash.events.Event;
    
    import flash.text.TextField;
    import flash.display.Sprite;
    import flash.ui.Keyboard;
    
    public class FlashTest extends Sprite {
        public function FlashTest() {
            // write as3 code here..
            
            var tf:TextField = new TextField();
            this.addChild(tf);
            tf.width = 500;
            tf.text = "Cars(Arrow keys & WASD)";
            
            var car:Car = new Car(Keyboard.LEFT, Keyboard.RIGHT, Keyboard.UP, Keyboard.DOWN);
            addChild(car);
            
            var car2:Car = new Car("A".charCodeAt(0),"D".charCodeAt(0), "W".charCodeAt(0), "S".charCodeAt(0));
            addChild(car2);
            
        }
    }
    

}

import flash.display.AVM1Movie;
import flash.ui.Keyboard;


import flash.events.*;
import flash.display.Sprite;

    

    internal class Car extends Sprite { 
    
        private var color:uint = 0x000000;
        private var speed:Number = 0;
        
        private var speedUp:Boolean = false;
        private var slowDown:Boolean = false;
        private var turnRight:Boolean = false;
        private var turnLeft:Boolean = false;
        
        private var left:Number = 0;
        private var right:Number = 0;
        private var up:Number = 0;
        private var down:Number = 0;
        
        public function Car(left:Number, right:Number, up:Number, down:Number):void{
            
            this.left = left;
            this.right = right;
            this.up = up;
            this.down = down;
            
            color = Math.random()*0xffffff;
            
            this.addEventListener(Event.ADDED_TO_STAGE, onAdded);
            
        }
        
        public function onAdded(e:Event):void{
            
            x = Math.random()*stage.stageWidth;
            y = Math.random()*stage.stageHeight;
            
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            
            
            rotation = 360*Math.random();
            
            drawCar();
            
        }
        
        public function onEnterFrame(e:Event):void{
            
            if (speedUp) {
               speed+=0.1;
            }
            
            if (slowDown){
                speed-=0.1;
            }
            
            if (turnLeft){
                rotation-=2;
            }
            
             if (turnRight){
                rotation+=2;
            }
            
            speed *= 0.97;
            


            
             x += speed*Math.cos(rotation*Math.PI/180);
             y += speed*Math.sin(rotation*Math.PI/180);

        }

        
        
        public function onKeyDown(e:KeyboardEvent):void{
            
            if (e.keyCode == up) {
                speedUp = true;
            }
            
            if (e.keyCode == down){
                slowDown = true;
            }
            
            if (e.keyCode == left){
                turnLeft = true;
            }
            
            if (e.keyCode == right){
                turnRight = true;
            }

        }
        
        public function onKeyUp(e:KeyboardEvent):void{
            
            if (e.keyCode == up) {
                speedUp = false;
            }
            
            if (e.keyCode == down){
                slowDown = false;
            }
            
            if (e.keyCode == left){
                turnLeft = false;
            }
            
            if (e.keyCode == right){
                turnRight = false;
            }

        }

        
        public function drawCar():void{
            graphics.beginFill(color);
            graphics.drawRect(-10,-5,20,10);
            graphics.endFill();
        }



        
    }