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

Checkers //my first game

my first game
Get Adobe Flash player
by D_one 03 Jul 2010
    Embed
//my first game

package {
    import flash.display.MovieClip;
    import flash.display.DisplayObject
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.*;
    import flash.geom.Point;
    import fl.transitions.Tween;
    import fl.transitions.*
    import fl.transitions.easing.*;
    import flash.filters.GlowFilter;
    import flash.display.SimpleButton;
    import flash.display.Shape;
    import flash.geom.ColorTransform;
    import flash.text.*;
    
    
    public class Checkers extends MovieClip {
        public function Checkers() {
        function drawCheckerGrid(xOrigin:Number, yOrigin:Number, sizeOfSquare:Number, numberOfSquaresWidth:Number, colourOutline:Number, lineThickness:Number, colourFill1:Number, colourFill2:Number) {
            
            var oddOrEvenRow:Number = 0;
            
            function drawSquare(size:Number, xPos:Number, yPos:Number, colourFill:Number) {
                var Square:Shape = new Shape();
                Square.graphics.lineStyle(lineThickness, colourOutline);
                Square.graphics.beginFill(colourFill);
                Square.graphics.drawRect(0, 0, size, size);
                Square.graphics.endFill();
                Square.x = xPos;
                Square.y = yPos;
                return(Square);
            }
            var i:Number;
            
            function squared(a:Number) {return(a*a);}
                
            var xPosition:Number, yPosition:Number;
            
            for(i=0; i<squared(numberOfSquaresWidth); i++) {
                if(numberOfSquaresWidth%2 - 1) {oddOrEvenRow = Math.floor(i/numberOfSquaresWidth)%2;}
                xPosition = xOrigin + (i%numberOfSquaresWidth)*sizeOfSquare;
                yPosition = yOrigin - Math.floor(i/numberOfSquaresWidth)*sizeOfSquare;
                var activeSquare:SimpleButton = new SimpleButton();
                activeSquare.upState   = drawSquare(size, xPosition, yPosition, (i+oddOrEvenRow)%2 ? colourFill1*3 : colourFill2);
                activeSquare.overState = drawSquare(size, xPosition, yPosition, (i+oddOrEvenRow)%2 ? colourFill1*3/2 : colourFill2*2/3);
                activeSquare.downState = drawSquare(size, xPosition, yPosition, (i+oddOrEvenRow)%2 ? colourFill1 : colourFill2*3);
                activeSquare.hitTestState = activeSquare.upState;
                stage.addChild(activeSquare);
                activeSquare.name = "square" + String(i%numberOfSquaresWidth+1) + String(Math.floor(i/numberOfSquaresWidth + 1));
            }
        }
        
        function createPlayer(xSq:Number, ySq:Number, radius:Number, colourOutline:Number, lineThickness:Number, colourFill:Number, playerName:String) {
            var playerCircle:MovieClip = new MovieClip();
            playerCircle.graphics.lineStyle(lineThickness, colourOutline);
            playerCircle.graphics.beginFill(colourFill);
            playerCircle.graphics.drawCircle(0,0, radius);
            playerCircle.graphics.endFill();
            playerCircle.alpha = 0.5
            playerCircle.x = squareToCoordinate(xSq,true);
            playerCircle.y = squareToCoordinate(ySq,false);
            playerCircle.name = playerName;
            playerCircle.king = new Boolean(false);
            gridArray[xSq - 1][ySq - 1] = playerName;
            stage.addChild(playerCircle);
        }
        
        function movePlayerToSquare(piece:DisplayObject , currentSquare:Point, desiredSquare:Point) {
            gridArray[currentSquare.x - 1][currentSquare.y - 1] = "empty";
            gridArray[desiredSquare.x - 1][desiredSquare.y - 1] = piece.name;
            motionComplete = false;
            var motionTweenX:Tween = new Tween(piece, "x", Regular.easeInOut, squareToCoordinate(currentSquare.x, true), squareToCoordinate(desiredSquare.x,true), 1, true),
                motionTweenY:Tween = new Tween(piece, "y", Regular.easeInOut, squareToCoordinate(currentSquare.y,false), squareToCoordinate(desiredSquare.y,false), 1, true);
            motionTweenX = new Tween(piece, "scaleX", Regular.easeInOut, 1,2,0.5,true);
            motionTweenY = new Tween(piece, "scaleY", Regular.easeInOut, 1,2,0.5,true);
            motionTweenX.addEventListener(TweenEvent.MOTION_FINISH, yoyo)
            
            function yoyo(e:Event):void {
                motionTweenX.yoyo();
                motionTweenY.yoyo();
                motionTweenX.removeEventListener(TweenEvent.MOTION_FINISH,yoyo);
                motionTweenX.addEventListener(TweenEvent.MOTION_FINISH,finishedMoving);
            }
            
            function finishedMoving(e:Event):void {
                kingIfAtBack(piece);
                removeJumpedPlayerIfNecessary(); //function changes motionComplete even if nothing to jump
            }
            
        }
        
        function squareToCoordinate(square:Number, xAxis:Boolean) {
            if(xAxis) {return((square - 0.5)*size);}
            else {return(8.5*size - square*size);}
        }
        
        function coordinateToSquare(coordinates:Point):Point {
            if(coordinates.x > 0 && coordinates.x < 8*size && coordinates.y > 0 && coordinates.y < 8*size) {    
                return( new Point( Math.ceil(coordinates.x/size),  8 - Math.floor(coordinates.y/size)) );
            } else {
                return(null);
            }
        }
        
        function drawTeam(colourFill:int, colourOutline, lineThickness:Number, numberOfPlayers:int, startingSquare:Point, gridSizeLinear:int, teamNumber:int) { //teamNumber = 0 for top, 1 for bottom
            
            var i:int,
                originalX:Number = startingSquare.x,
                nextSquare:Point = startingSquare,
                playerName:String;
                
            for(i = 1; i <= numberOfPlayers; i++) {
                playerName = "player" + teamNumber + String(Math.floor(i/10)) + String(i%10);
                createPlayer(nextSquare.x, nextSquare.y, size*2/5, colourOutline, lineThickness, colourFill,playerName);
                if(nextSquare.x < gridSizeLinear - 1) {
                    nextSquare.x += 2;
                } else {
                        nextSquare.x = originalX
                        if (teamNumber != 0) {
                            nextSquare.x += (nextSquare.y % 2 != 1) ? 0 : 1
                        } else {
                            nextSquare.x += (nextSquare.y % 2 != 1) ? -1 : 0;
                        }
                        
                        nextSquare.y += (teamNumber == 1) ? 1 : -1;
                    }
            }
        }
        
       /* function activateCustomMouse() {
        
            Mouse.hide();
            var mouseCursor:MovieClip = new mouseCursorClass();
            addChild(mouseCursor);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMouse);
            moveToTop(mouseCursor);
            
            function moveMouse(evt:MouseEvent) {
                moveToTop(mouseCursor);
                mouseCursor.x = evt.stageX;
                mouseCursor.y = evt.stageY;
            }
        
        } */
        
        function mouseClick(evt:MouseEvent) {
        
            var highlightColour:ColorTransform,
                desiredSquare:Point;
            if(motionComplete) {
                if(!(hasSelectedPlayer)) {
                    mouseSquare = coordinateToSquare( new Point(stage.mouseX, stage.mouseY) );
            
                    if(mouseSquare != null) {
                        if(gridArray[mouseSquare.x - 1][mouseSquare.y - 1] != "empty") { 
                            piece = parent.getChildByName(pieceAtSquare(mouseSquare));
                            highlightColour = new ColorTransform(1,1,1,1.5,0,0,0,0);
                            if (playerTurn(piece)) {
                                piece.transform.colorTransform = highlightColour;
                                hasSelectedPlayer = true;
                            } else {
                                trace("somehow remind them it isn't their turn");
                            }
                        }
                    }
                } else {
                    desiredSquare = coordinateToSquare(new Point(stage.mouseX, stage.mouseY));
                    if(desiredSquare != null) {
                        if( gridArray[desiredSquare.x - 1][desiredSquare.y - 1] == "empty") {
                            moveToTop(piece);
                            if(ableToMoveToSquare(piece,mouseSquare,desiredSquare)) {
                                movePlayerToSquare(piece, mouseSquare, desiredSquare);
                                turn *= -1;
                            } else {}
                        } else {}
                    }
                    highlightColour = new ColorTransform(1,1,1,2/3, -60, -60, -60,0);
                    piece.transform.colorTransform = highlightColour;
                    hasSelectedPlayer = false;
                    piece = null;
                    
                }
            }
        }
        
        function pieceAtSquare(square:Point) {
            return gridArray[square.x - 1][square.y - 1]
        }
        
        function moveToTop(clip:DisplayObject) {
            stage.addChild(clip);
        }
        
        function kingIfAtBack(pieceInput:DisplayObject) {
            var piece:MovieClip = pieceInput as MovieClip,
                pieceName:String = piece.name,
                pieceTeam:int = Number(pieceName.charAt(6)),
                pieceSquare:Point = coordinateToSquare( new Point(piece.x, piece.y));
                
            if( ((pieceTeam == 0 && pieceSquare.y == 1) || (pieceTeam == 1 && pieceSquare.y == 8) ) && piece.king == false ) {
                piece.king = true;
                //var crown:Sprite = new Crown();
                //piece.addChild(crown);
            }
        }
        
        function ableToMoveToSquare(pieceAsDispObj:DisplayObject,currentSquare:Point, desiredSquare:Point) /* also handles part of jumping */ {
            
            var piece:MovieClip = pieceAsDispObj as MovieClip,
                jumpedPiece:String;
            
            function properDirectionOrKing():Boolean {
                if(currentSquare.y - desiredSquare.y > 0 && piece.name.charAt(6) == "0") {
                        return(true);
                    }
                    if(currentSquare.y - desiredSquare.y < 0 && piece.name.charAt(6) == "1") {
                        return(true);
                    }
                    if(Math.abs(currentSquare.y - desiredSquare.y) != 0 && piece.king == true) {
                        return(true);
                    }
                return(false);
            }
                
            if(gridArray[desiredSquare.x - 1][desiredSquare.y - 1] == "empty") {
                if(Math.abs(currentSquare.x - desiredSquare.x) == 1) { //not jump
                    if (properDirectionOrKing() && Math.abs(currentSquare.y - desiredSquare.y) == 1) {
                        return(true);
                    }
                }
                if(Math.abs(currentSquare.x - desiredSquare.x) == 2 && Math.abs(currentSquare.y - desiredSquare.y) == 2) { // for possible jump
                    var jumpedSquare = new Point( (currentSquare.x + desiredSquare.x)/2 , (currentSquare.y + desiredSquare.y)/2);
                    jumpedPiece = pieceAtSquare(jumpedSquare);
                    if(jumpedPiece != "empty" && properDirectionOrKing()) {
                        if(jumpedPiece.charAt(6) != piece.name.charAt(6)) { //not same team
                            jumpedPlayerSquare = jumpedSquare;
                            jumpedPlayerToRemove = jumpedPiece;
                            return(true);
                        }
                    }
                }
            }
            return(false);
        }
        
        function playerTurn(player:DisplayObject):Boolean {
            if(player.name.charAt(6) == "0" && turn == -1) {
                return(true);
            } else if(player.name.charAt(6) == "1" && turn == 1) {
                return(true);
            } else {
                return(false);
            }
        }
        
        function removeJumpedPlayerIfNecessary() {
                
            if(jumpedPlayerToRemove != null) {
                var jumped:Tween = new Tween(parent.getChildByName(jumpedPlayerToRemove), "alpha", Regular.easeIn, 1, 0, 1.5, true),
                    colourTransform:ColorTransform = new ColorTransform(0,0,0,1,255,255,0,0);
                parent.getChildByName(jumpedPlayerToRemove).transform.colorTransform = colourTransform; //fuck sakes
                jumped.addEventListener(TweenEvent.MOTION_FINISH, removePiece);
                
            } else {motionComplete = true;}
            
            function removePiece(evt:TweenEvent) {
                stage.removeChild(parent.getChildByName(jumpedPlayerToRemove));
                gridArray[jumpedPlayerSquare.x - 1][jumpedPlayerSquare.y - 1] = "empty";
                jumpedPlayerToRemove = null;
                motionComplete = true;
            }        
        }
        
            
        
        //activateCustomMouse();
        
        var speed:Number,
            size:Number = 50,
            mouseSquare:Point,
            hasSelectedPlayer:Boolean = false,
            piece:DisplayObject,
            turn:int = -1,
            motionComplete:Boolean = true,
            jumpedPlayerToRemove:String,
            jumpedPlayerSquare:Point,
            gridArray:Array = [ ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"], ["empty","empty","empty","empty","empty","empty","empty","empty"] ];
        
        function startGame() {
        
            stage.addEventListener(MouseEvent.CLICK, mouseClick);
            
            drawCheckerGrid(0, 7*size, size, 8, 0xDDDD00, 1, 0x880000, 0x333333);
            
            drawTeam(0x66CCFF, 0x000033, 1.5, 12,  new Point(1,1) , 8, 1);
            drawTeam(0x66CC99, 0x000033, 1.5, 12,  new Point(2,8) , 8, 0);
        }
        
        function mainMenu() {
            
            function drawButton(xSize:Number, ySize:Number, xPos:Number, yPos:Number, colourFill:Number, lineThickness:Number, colourOutline:Number, rectangleText:String, desiredTextSize:Number) {
            
                function drawRectangle(xSize:Number, ySize:Number, xPos:Number, yPos:Number, colourFill:Number, lineThickness:Number, colourOutline:Number, rectangleText:String, desiredTextSize:Number ) {
                    var drawnRectangle:Sprite = new Sprite();
                    drawnRectangle.graphics.lineStyle(lineThickness, colourOutline);
                    drawnRectangle.graphics.beginFill(colourFill);
                    drawnRectangle.graphics.drawRect(0, 0, xSize, ySize);
                    drawnRectangle.graphics.endFill();
                    drawnRectangle.x = xPos;
                    drawnRectangle.y = yPos;
                    
                    var rectangleTextBox:TextField = new TextField(),
                        rectangleTextBoxFormat:TextFormat = new TextFormat();
                        
                    //rectangleTextBoxFormat.align = TextFormatAlign.CENTER
                    rectangleTextBoxFormat.size = desiredTextSize;
                    rectangleTextBox.defaultTextFormat = rectangleTextBoxFormat;
                    rectangleTextBox.text = rectangleText;
                    rectangleTextBox.autoSize = TextFieldAutoSize.CENTER;
                    rectangleTextBox.wordWrap = false;
                    drawnRectangle.addChild(rectangleTextBox);
                    rectangleTextBox.x = xSize/2 - rectangleTextBox.textWidth/2;
                    rectangleTextBox.y = ySize/2 - rectangleTextBox.textHeight/2;
                    return(drawnRectangle);
                }
            
            
            var button:SimpleButton = new SimpleButton();
                button.upState   = drawRectangle(xSize, ySize, xPos, yPos, colourFill, lineThickness, colourOutline, rectangleText, desiredTextSize);
                button.overState = drawRectangle(xSize, ySize, xPos, yPos, colourFill + 0x110000, lineThickness + 1, colourOutline, rectangleText, desiredTextSize + 1);
                button.downState = drawRectangle(xSize, ySize, xPos, yPos, colourFill + 0x330000, lineThickness + 2, colourOutline, rectangleText, desiredTextSize + 2);
                button.hitTestState = button.upState;
                
                return(button);
            }
            
            var newGame:SimpleButton = drawButton(100,45,-50,150,0xCCCCCC, 1, 0x222200, "2 Players", 18);
            newGame.addEventListener(MouseEvent.CLICK, startNewGame);
            stage.addChild(newGame);
            
            var titleText:TextField = new TextField(),
                titleTextFormat:TextFormat = new TextFormat();
            titleTextFormat.size = 48;
            titleText.defaultTextFormat = titleTextFormat;
            titleText.text = "prgmGAYCHECKERS";
            titleText.x = 200;
            titleText.y = 15;
            titleText.autoSize = TextFieldAutoSize.CENTER;
            stage.addChild(titleText);
            
            function startNewGame(evt:MouseEvent) {
                var exitTween:Tween = new Tween(newGame, "x", Elastic.easeIn, 550/2, -120, 1.5, true);
                    exitTween = new Tween(titleText, "y", Elastic.easeIn, 15, -120, 1.5, true);
                
                exitTween.addEventListener(TweenEvent.MOTION_FINISH, exitTweenOver);
                
                function exitTweenOver(evt:TweenEvent) {
                    exitTween = null;
                    stage.removeChild(newGame);
                    stage.removeChild(titleText);
                    startGame();
                }
            }
            
            var introTween:Tween = new Tween(newGame,"x",Elastic.easeOut,0,550/2,1.5,true);
                introTween = new Tween(titleText, "y", Elastic.easeOut, -50, 15, 1.5, true); //this cant be good    
            
        }
            
        
        startGame();
        //mainMenu();

        }
    }
}

/* to do:
    write AI
    write menu <-- needs maybe a 'little' improvement
    sound <-- half complete
    ALLOW FOR MULTIPLE JUMPS
    CHECK IF JUMPS ARE POSSIBLE, and perhaps notify player
    *good idea* text box other than trace()  ... ;/ ... altering people of what in the fuck is going on half the time
*/