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

forked from: forked from: Pinapple 22 Controls

Just playing around with bitmapData :)

########### Controls #############
#  Arrow keys   -   Movement     #
#  Control Key  -   Slow/Shrink  #
#  Shift Key    -   Drag lasso   #
##################################
Get Adobe Flash player
by hiphi 17 Feb 2009
    Embed
// forked from GirlFlash's forked from: Pinapple 22 Controls
// forked from GirlFlash's Pinapple 22 Controls
// forked from GirlFlash's Pinapple 22 Cell Movement
/*
  Just playing around with bitmapData :)

  ########### Controls #############
  #  Arrow keys   -   Movement     #
  #  Control Key  -   Slow/Shrink  #
  #  Shift Key    -   Drag lasso   #
  ##################################  */


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

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


    public class GirlFlash_GameOne extends Sprite {
        private var bmd:BitmapData = new BitmapData(100,100,true);
        private var bmd2:BitmapData = new BitmapData(800,800,true);
        private var bmd3:BitmapData = new BitmapData(100,100,true,0xFFFFFFFF);
        private var bmp:Bitmap = new Bitmap(bmd);
        private var mySprite:Sprite = new 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;

        //lasso vars
        private var stringArray:Array = new Array();
        private var stringsArray:Array = new Array();

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

        //misc variables
        private var tempX:Number;
        private var tempY:Number;
        private var tempNode:Array;
        private var tempPoint:Point;
        private var i:Number;
        private var k:Number;
        private var a:Number;
        private var b:Number;
        private var c:Number;
        private var destroyString:Boolean;

        public function GirlFlash_GameOne() {

            addChild(bmp);
            bmp.width=800
            bmp.height=800
            bmp.alpha=1
            addChild(mySprite)
            bmd.fillRect(new Rectangle(0,0,400,400),0xffffff00);

            // 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;
            }

            if (ctrlKey){
                cellRadius = 30;
                theSpeed = 6;
            }else{
                cellRadius = 50;
                theSpeed = 9;
            }
            
            //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
                }
            }


            //lasso stuff
            if (shiftKey){
                //spawn lasso points if they are more then 20 pixels apart
                if (stringArray.length>0){
                        a = stringArray[stringArray.length-1].x - theX
			b = stringArray[stringArray.length-1].y - theY
			c = Math.sqrt((a*a)+(b*b))
                }else{
                    c=51
                }
                if (c>20){
                    stringArray.push(new Point(theX,theY))
                    if (stringArray.length>50){
                        //limit the length of the lasso
                        stringArray.splice(0,1)
                    }
                }
            }else{
                if (stringArray.length>0){
                    //if we have been spawning a lasso, copy it to a temp array
                    tempNode = new Array();
                    for (i=0;i<stringArray.length;i+=2){
                        tempNode.push(stringArray[i])
                    }

                    //find the center point of the lasso
                    tempPoint = new Point(0,0)
                    for (i=0;i<tempNode.length;i++){
                        tempPoint.x += tempNode[i].x
                        tempPoint.y += tempNode[i].y
                    }
                    tempPoint.x = tempPoint.x/tempNode.length
                    tempPoint.y = tempPoint.y/tempNode.length

                    //add the lasso and center point to an array of released lassos
                    stringsArray.push([tempNode,tempPoint]);

                    //reset the spawning lasso array
                    stringArray = new Array();
                }
            }

            for (i=0;i<stringsArray.length;i++){
                destroyString = false;
                for (k=0;k<stringsArray[i][0].length;k++){
                    stringsArray[i][0][k].x -= (stringsArray[i][0][k].x - stringsArray[i][1].x)/5
                    stringsArray[i][0][k].y -= (stringsArray[i][0][k].y - stringsArray[i][1].y)/5

                    //a = stringsArray[i][0][k].x - stringsArray[i][1].x
                    //b = stringsArray[i][0][k].y - stringsArray[i][1].y
                    //c = Math.sqrt((a*a)+(b*b));

                    //if (c<2){
                        //stringsArray[i][0].splice(k,1);
                        //if (stringsArray[i][0].length<3){
                            //destroyString = true;
                        //}
                    //}
                }
                if (destroyString){
                    stringsArray.splice(i,1);
                }
            }

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

            //render membrane
            mySprite.graphics.moveTo(membraneArray[0][0].x,membraneArray[0][0].y);
            for (i=0; i<membraneArray.length; i++){
                mySprite.graphics.lineTo(membraneArray[i][0].x,membraneArray[i][0].y);
            }
            mySprite.graphics.lineTo(membraneArray[0][0].x,membraneArray[0][0].y);

            //render lasso
            if (stringArray.length>0){
                mySprite.graphics.moveTo(stringArray[0].x,stringArray[0].y)
            }
            for (i=0;i<stringArray.length;i++){
                mySprite.graphics.lineTo(stringArray[i].x,stringArray[i].y);
            }
            if (stringArray.length>0){
                mySprite.graphics.lineTo(theX,theY)
                mySprite.graphics.lineTo(theX+xMove,theY+yMove)
            }

            //render released lassos
            for (i=0;i<stringsArray.length;i++){
                mySprite.graphics.moveTo(stringsArray[i][0][0].x,stringsArray[i][0][0].y);
                for (k=0;k<stringsArray[i][0].length;k++){
                    mySprite.graphics.lineTo(stringsArray[i][0][k].x,stringsArray[i][0][k].y)
                }
            }

            bmd2.draw(mySprite,null,null,null,new Rectangle(0,0,800,800))
            bmd.draw(bmd3,null,null,null,new Rectangle(0,0,100,100))
            for (i=0;i<bmd.width;i++){
                for (k=0;k<bmd.height;k++){
                    bmd.copyPixels(bmd2,new Rectangle(i*8,k*8,2,2),new Point(i,k))
                }
            }
            
        }
        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;
                case 17:
                    ctrlKey = true;
                break;
                case 16:
                    shiftKey = 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;
                case 17:
                    ctrlKey = false;
                break;
                case 16:
                    shiftKey = false;
                break;
            }
        }
        private function jitter():Number{
            return (Math.random()*10)-5
        }
    }
}