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

Platformer Test

Import needed packages.
Get Adobe Flash player
by Johannes 25 Oct 2009
package {
    // Import needed packages.
    import flash.display.*;
    import flash.events.*;
    
    public class Game extends Sprite {
        // Variables
        public var dude:Sprite;
        public var ground:Sprite;
        public var keyboard:Array = new Array(255);
        public var gravity:Number = 0;
        public var speed:Number = 0;
        public var jumping:Boolean = false;
        public var r:Number;
        
        // Constant variables (They can't be changed, that's the point of constants.)
        public const jumpheight:Number = 15;
        public const maxspeed:Number = 6;
        public const maxgravity:Number = 15;
        
        public function Game() {
            // Quality
            stage.quality = StageQuality.HIGH;
            
            // Draw the ground. Ground as in level.
            drawGround([
                {type:"rect", color:0x555555, x:-50, y:50, w:600, h:200, rotation:0},
                {type:"rect", color:0x900000, x:100, y:-50, w:30, h:100, rotation:0},
                {type:"circle", color:0x00CC00, x:200, y:-25, radius:20}
            ]);
            
            // Draw the player.
            dude = new Sprite();
            
            dude.graphics.lineStyle(1, 0x000000);
            dude.graphics.beginFill(0x00CC00);
            dude.graphics.drawCircle(0, 0, 15);
            dude.graphics.lineTo(0, 0);
            dude.graphics.endFill();
            dude.y = -100;
            
            //Adding him to the scene.
            this.addChild(dude);
            
            // Calculate player radius. Used for ground collision,
            r = dude.height / 2;
            
            // Add Event Listeners.
            stage.addEventListener(Event.ENTER_FRAME, main);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);
            stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
        }
        
        // Draws a level from an array of objects containing level data
        public function drawGround(content:Array):void {
            var data:Object, gfx:Sprite;
            ground = new Sprite();
            
            for(var i:Number = 0; i < content.length; i++){
                data = content[i];
                
                switch(data.type) {
                    case "rect":
                        gfx = new Sprite();
                        
                        gfx.graphics.lineStyle(1, data.color);
                        gfx.graphics.beginFill(data.color);
                        gfx.graphics.drawRect(0, 0, data.w, data.h);
                        gfx.graphics.endFill();
                        gfx.x = data.x;
                        gfx.y = data.y;
                        gfx.rotation = data.rotation;
                        
                        ground.addChild(gfx);
                        break;
                    case "circle":
                        gfx = new Sprite();
                        
                        gfx.graphics.lineStyle(1, data.color);
                        gfx.graphics.beginFill(data.color);
                        gfx.graphics.drawCircle(data.x, data.y, data.radius);
                        gfx.graphics.endFill();
                        
                        ground.addChild(gfx);
                }
            }
            
            this.addChild(ground);
        }
        
        // Recieves key input, and stores if a key is down into the keyboard array.
        public function key_down(e:KeyboardEvent):void {
            keyboard[e.keyCode] = true;
        }
        
        // Recieves when a key has been released, then stores that the key is released.
        public function key_up(e:KeyboardEvent):void {
            keyboard[e.keyCode] = false;
        }
        
        // The main function. Runs every frame.
        public function main(e:Event):void {
            // Ground Collision & Gravity
            if(ground.hitTestPoint(dude.x, dude.y + r, true)) {
                gravity = 0;
                jumping = false;
            } else {
                gravity++;
                dude.y += gravity;
            }
            
            // Push the player up if he's "inside" the ground. Pretty useful technique.
            while(ground.hitTestPoint(dude.x, dude.y + r - 1, true)) {
                dude.y--;
                gravity = 0;
                jumping = false;
            }
            
            // Jumping
            if(keyboard[38] &&!jumping) {
                gravity =-jumpheight;
                dude.y += gravity;
                jumping = true;
            }
            
            // Accelerate to the left, if the left arrow key is pushed.
            if(keyboard[37]) {
                speed--;
            }
            
            // Accelerate to the right, if the right arrow key is pushed.
            if(keyboard[39]) {
                speed++;
            }
            
            // If none of the right or left arrow keys are pushed, "slowly" deaccelerate (Is that even a word?) the speed back to 0.
            if(!keyboard[37] &&!keyboard[39] && speed != 0) {
                speed -= speed < 0 ? -1 : 1;
            }
            
            // Going too fast? Slow down.
            if(speed > maxspeed || speed < -maxspeed) speed = speed > 0 ? maxspeed : -maxspeed; 
            if(gravity > maxgravity || gravity < -maxgravity) gravity = gravity > 0 ? maxgravity : -maxgravity;
            
            // Move and rotate the player after speed.
            dude.x += speed;
            dude.rotation += speed * 3;
            
            // Walls
            while(ground.hitTestPoint(dude.x-r+1, dude.y, true)) {
                dude.x++;
                speed = Math.abs(speed);
            }
            
            while(ground.hitTestPoint(dude.x+r-1, dude.y, true)) {
                dude.x--;
                speed = -Math.abs(speed);
            }
            
            while(ground.hitTestPoint(dude.x, dude.y-r, true)) {
                dude.y++;
                gravity = 0;
            }
            
            // Camera. This has to be in the last part of the code!
            this.x -= Math.round((this.x - ((stage.stageWidth / 2) - dude.x)) / 5);
            this.y -= Math.round((this.y - ((stage.stageHeight / 2) - dude.y)) / 5);
        }
    }
}