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

Pinapple 22 Cell Movement

=== Movement engine ported from my 'Pinapple 22' game===
-- Original game can be played at:
http://girlflash.deviantart.com/art/Pinapple-22-Game-112250075

####### Controls ########
# Arrow keys - Movement #
#########################
Get Adobe Flash player
by GirlFlash 13 Feb 2009
/* === Movement engine ported from my 'Pinapple 22' game===
  -- Original game can be played at:
  http://girlflash.deviantart.com/art/Pinapple-22-Game-112250075

  ####### Controls ########
  # Arrow keys - Movement #
  #########################  */

package {
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.geom.*;

    [SWF(width=800, height=800, backgroundColor=0xFFFFFF)]


    public class GirlFlash_GameOne extends Sprite {
        //cell properties
        private var theX:Number = 220;
        private var theY:Number = 220;
        private var theSpeed:Number = 9;
        private var cellRadius:Number = 50;
        private var wallBounce:Number = 2;
        private var xMove:Number = 0;
        private var yMove:Number = 0;

        //membrane and properties
        private var membraneArray:Array = new Array();
        private var membraneDetail:Number = 20;

        //key states
        private var leftKey:Boolean = false;
        private var upKey:Boolean = false;
        private var rightKey:Boolean = false;
        private var downKey:Boolean = false;

        //misc variables
        private var tempX:Number;
        private var tempY:Number;
        private var i:Number;
        private var a:Number;
        private var b:Number;
        private var c:Number;

        public function GirlFlash_GameOne() {

            // create all the points the membrane is made up of
            for (i=0; i<membraneDetail; i++){
                //node structure is [ point, xMovement, yMovement]
                membraneArray.push([new Point(0,0),0,0])
                membraneArray[i][0].x = theX + (cellRadius * Math.cos((((i/membraneDetail)*360)/180)*Math.PI))
                membraneArray[i][0].y = theY + (cellRadius * Math.sin((((i/membraneDetail)*360)/180)*Math.PI))
            }

            //make event listeners for game loop as well as input
            addEventListener(Event.ENTER_FRAME,onTick);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
            stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUnPress);

            trace("poo");
        }

        private function onTick(evt:Event):void{

            //Movement
            if (leftKey){
                if (rightKey){
                    //do jack
                }else if (upKey){
                    yMove -= theSpeed * 0.7;
                    xMove -= theSpeed * 0.7;
                }else if (downKey){
                    yMove += theSpeed * 0.7;
                    xMove -= theSpeed * 0.7;
                }else{
                    xMove -= theSpeed;
                }
            }else if (rightKey){
                if (upKey){
                    yMove -= theSpeed * 0.7;
                    xMove += theSpeed * 0.7;
                }else if (downKey){
                    yMove += theSpeed * 0.7;
                    xMove += theSpeed * 0.7;
                }else{
                    xMove += theSpeed;
                }
            }else if (upKey){
                if (downKey){
                    //do jack
                }else{
                    yMove -= theSpeed;
                }
            }else if (downKey){
                yMove += theSpeed;
            }
            
            //decelerate
            xMove*=0.8
            yMove*=0.8

            //position
            theX+=xMove;
            theY+=yMove;

            //collision with screen edges
            if (theX<10){
                xMove=0;
                theX=10;
            }
            if (theX>790){
                xMove=0;
                theX=790;
            }
            if (theY<10){
                yMove=0;
                theY=10;
            }
            if (theY>790){
                yMove=0;
                theY=790;
            }

            if (theX<cellRadius*0.7){
                xMove += wallBounce;
            }
            if (theX>800-(cellRadius*0.7)){
                xMove -= wallBounce;
            }
            if (theY<cellRadius*0.6){
                yMove += wallBounce;
            }
            if (theY>800-(cellRadius*0.7)){
                yMove -= wallBounce;
            }

            //membrane stuff
            for (i=0;i<membraneArray.length;i++){
                //calculate the target position of the membrane point
                tempX = theX + (cellRadius * Math.cos((((i/membraneDetail)*360)/180)*Math.PI))
                tempY = theY + (cellRadius * Math.sin((((i/membraneDetail)*360)/180)*Math.PI))

                //calculate the points distance from the nucleus
                a = membraneArray[i][0].x - theX
                b = membraneArray[i][0].y - theY
                c = Math.sqrt((a*a)+(b*b));

                //increment movement value by distance from target, relative to distance from nucleus
                //working in the nucleus distance is what allows deformation in the membrane
                membraneArray[i][1] -= (membraneArray[i][0].x - tempX)/(c/10)
                membraneArray[i][2] -= (membraneArray[i][0].y - tempY)/(c/10)

                //decelerate membrane point's movement
                membraneArray[i][1] *= 0.999
                membraneArray[i][2] *= 0.999

                //position point relative to target position, but offset by its movement values
                membraneArray[i][0].x -= ((membraneArray[i][0].x-tempX)/3)-membraneArray[i][1]
                membraneArray[i][0].y -= ((membraneArray[i][0].y-tempY)/3)-membraneArray[i][2]

                //membrane collision with screen edges
                if (membraneArray[i][0].x<5){
                    membraneArray[i][0].x=5
                    membraneArray[i][1]=0
                }
                if (membraneArray[i][0].x>795){
                    membraneArray[i][0].x=795
                    membraneArray[i][1]=0
                }
                if (membraneArray[i][0].y<5){
                    membraneArray[i][0].y=5
                    membraneArray[i][2]=0
                }
                if (membraneArray[i][0].y>795){
                    membraneArray[i][0].y=795
                    membraneArray[i][2]=0
                }
            }

            //render
            graphics.clear();
            graphics.lineStyle(1,0x000000);
            graphics.moveTo(theX+xMove+jitter(),theY+yMove+jitter());
            graphics.lineTo(theX+xMove+jitter(),theY+yMove+jitter());

            graphics.moveTo(membraneArray[0][0].x,membraneArray[0][0].y);
            for (i=0; i<membraneArray.length; i++){
                graphics.lineTo(membraneArray[i][0].x,membraneArray[i][0].y);
            }
            graphics.lineTo(membraneArray[0][0].x,membraneArray[0][0].y);
        }
        private function onKeyPress(evt:KeyboardEvent):void{
            switch(evt.keyCode){
                case 37:
                    leftKey = true;
                break;
                case 38:
                    upKey = true;
                break;
                case 39:
                    rightKey = true;
                break;
                case 40:
                    downKey = true;
                break;
            }
        }
        private function onKeyUnPress(evt:KeyboardEvent):void{
            switch(evt.keyCode){
                case 37:
                    leftKey = false;
                break;
                case 38:
                    upKey = false;
                break;
                case 39:
                    rightKey = false;
                break;
                case 40:
                    downKey = false;
                break;
            }
        }
        private function jitter():Number{
            return (Math.random()*10)-5
        }
    }
}