Elevator

by mutantleg
a top down view elevator
♥0 | Line 135 | Modified 2012-05-14 17:17:37 | MIT License
play

ActionScript3 source code

/**
 * Copyright mutantleg ( http://wonderfl.net/user/mutantleg )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/6z1h
 */

package {
    import flash.ui.KeyLocation;
    import flash.ui.Keyboard;
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    import flash.display.Graphics;
    import flash.display.Sprite;
    public class FlashTest extends Sprite {
        
        public var ps:Sprite;
        public var px:Number = 250;
        public var py:Number = 250;
        public var pa:Number = 0;
        
        public var mover:aMove;
        
        public function FlashTest() {
            // write as3 code here..
           
           var g:Graphics;
            
            ps = new Sprite();
            g = ps.graphics;
            g.lineStyle(2,0);
            g.drawCircle(0,0,16);
            g.moveTo(0,0);
            g.lineTo(16,0);
            addChild(ps);
            
            mover = new aMove();
            addChild(mover);
            
            stage.addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, kdown);
            stage.addEventListener(KeyboardEvent.KEY_UP, kup);
        }//ctor
        
        public function kdown(e:KeyboardEvent):void
        { cKeyMan.setKey(e.keyCode, true); }//kdown
        
        public function kup(e:KeyboardEvent):void
        { cKeyMan.setKey(e.keyCode, false); }//kup
        
        public function update(e:Event):void
        {
            ps.x = px;
            ps.y = py;
            ps.rotationZ = pa * (180.0/3.1415);
            
             if (cKeyMan.isKeyDown(Keyboard.LEFT))
             { pa -= 0.1; }
             if (cKeyMan.isKeyDown(Keyboard.RIGHT))
             { pa += 0.1; }
            
            if (cKeyMan.isKeyDown(Keyboard.UP))
            { 
              px += Math.cos(pa)*8;
              py += Math.sin(pa)*8;
             }//endif
             
             if (cKeyMan.isKeyDown(Keyboard.DOWN))
            { 
              px += Math.cos(pa)*-8;
              py += Math.sin(pa)*-8;
             }//endif
            
            mover.update();
            
            if (mover.isInside(px,py) == false)
            {
            if (mover.isInsideOld(px, py))
            {
                px += mover.cx - mover.ox;
                py += mover.cy - mover.oy;
            }//endif
            }
            
            if (mover.isInside(px, py))
            {
                px += mover.vx;
                py += mover.vy;
            }//endif
            
            
        }//update
        
    }//classend
}
import flash.display.Sprite;//package


internal class aMove extends Sprite
{
    
    public var xrad:Number = 32;
    public var yrad:Number = 32;
    public var ang:Number = 0;
    
    public var cx:Number = 100;
    public var cy:Number = 100;
    
    public var ox:Number = 0;
    public var oy:Number = 0;
    
    public var vx:Number = 0;
    public var vy:Number = 0;
    
    public function aMove()
    {
        vy = 8;
        vx = 4;
        
       graphics.clear();
       graphics.lineStyle(2,0);
       graphics.drawRect(-xrad,-yrad,xrad*2,yrad*2);
    }//ctor
    
    public function isInside(kx:Number, ky:Number):Boolean
    {
        if (kx > cx+xrad) { return false; }
        if (kx < cx-xrad) { return false; }
        if (ky > cy+yrad) { return false; }
        if (ky < cy -yrad) { return false; }
        
        return true;
    }//isinide
    
    public function isInsideOld(kx:Number, ky:Number):Boolean
    {
        if (kx > ox+xrad) { return false; }
        if (kx < ox-xrad) { return false; }
        if (ky > oy+yrad) { return false; }
        if (ky < oy-yrad) { return false; }
        
        return true;
    }//inold
    
    public function update():void
    {
        ox = cx;
        oy = cy;
        
        cy += vy;
        cx += vx;
        
        
        if (cy >= 350 && vy > 0) { vy = -vy; }
        if (cy <= 50 && vy < 0) {vy = -vy; } 
        if (cx >= 350 && vx > 0) { vx = -vx; }
        if (cx <= 50 && vx < 0) {vx = -vx; } 
        
        x = cx;
        y = cy;
     
        
        
    }//update
    
}//classendamove




internal class cKeyMan
   {
       public function cKeyMan() {}//ctor (unused)
       
       public static var vecKey:Vector.<Boolean> = new Vector.<Boolean>(512,true);
       
       public static function setKey(k:int, b:Boolean):void
       {
           if (k < 0) return;
           if (k >= 512) return;
           vecKey[k] = b;
       }//setkey
       
       public static function isKeyDown(k:int):Boolean
       {
           if  (k < 0) return false;
           if (k >= 512) return false;
           return vecKey[k];
       }//iskey
       
   }//keyman