A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [FLASH 8] help needed with a board game

  1. #1
    Junior Member
    Join Date
    Aug 2008
    Posts
    14

    Question [FLASH 8] help needed with a board game

    Hi all,

    I'm making an hexagonal board game, using Tonypa's tutorial on Tile Based Games. the goal is getting four of your own pieces in a row on the board. The problem is you just have 6 pieces, so when you don't have any, you have to take another one off the board and lay it somewhere else.

    Now the problem with the coding: there's something wrong with counting the amount of pieces you have left.

    Here's the code:
    Actionscript Code:
    aantalblokjes1 = 6;
    aantalblokjes2 = 6;
    aantalblokjes3 = 6;
    aantalblokjes4 = 6;
    huidigespeler = 1;
    Mouse.hide();
    spelBord = [[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
                [0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0],
                [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                [0, 0, 1, 1, 1, 1, 1, 1, 1, 0, 0],
                [0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]];
    // defining tiles
    game = {tileH:52, tileS:30.02221399786};
    game.Tile0 = function() {};
    game.Tile0.prototype.clickable = false;
    game.Tile0.prototype.frame = 1;
    game.Tile1 = function() {};
    game.Tile1.prototype.clickable = false;
    game.Tile1.prototype.speler = 0;
    game.Tile1.prototype.frame = 2;
    game.Tile1.prototype.spelend = 1;
    buildMap(spelBord);
    this.onEnterFrame = function() {
        speler_txt=("Speler: "+_root.huidigespeler)
        getMouse();
    };
    var someListener:Object = new Object();
    someListener.onMouseUp = function() {
        getTarget();
    };
    Mouse.addListener(someListener);
    ///////////////////////////////////////////////
    ////////////////// FUNCTIONS //////////////////
    ///////////////////////////////////////////////
    function buildMap(map) {
        _root.attachMovie("empty", "tiles", 1);
        _root.attachMovie("muisaanwijzer", "muisaanwijzer", 2);
        game.clip = _root.tiles;
        game.clip._x = 270;
        game.clip._y = -15;
        var d = _root.getNextHighestDepth();
        var mapWidth = map[0].length;
        var mapHeight = map.length;
        for (var y = 0; y<mapHeight; ++y) {
            for (var x = 0; x<mapWidth; ++x) {
                var naam = "t_"+y+"_"+x;
                game[naam] = new game["Tile"+map[y][x]];
                if (naam == "t_5_5") {
                    game[naam].clickable = true;
                }
                game.clip.attachMovie("tile", naam, ++d, {_x:(x*_root.game.tileS*1.5), _y:(y*_root.game.tileH+(x%2)*_root.game.tileH/2)});
                game.clip[naam].gotoAndStop(game[naam].frame);
            }
        }
        game.clip.attachMovie("mouse", "mouse", _root.getNextHighestDepth());
    }
    function getMouse() {
        _root.muisaanwijzer.gotoAndStop(_root.huidigespeler);
        _root.muisaanwijzer._x = _root._xmouse;
        _root.muisaanwijzer._y = _root._ymouse;
        _root.muisaanwijzer.gotoAndStop(_root.huidigespeler+1);
        game.xmouse = Math.round((_root.muisaanwijzer._x-270)/game.tileS/1.5);
        game.ymouse = Math.round((_root.muisaanwijzer._y+15-((((game.xmouse+1)%2)*game.tileH/2)+(game.xmouse%2)*game.tileH))/game.tileH);
        var naam = "t_"+game.ymouse+"_"+game.xmouse;
        _root.tiles.mouse._x = game.xmouse*game.tileS*1.5;
        _root.tiles.mouse._y = game.ymouse*game.tileH+((game.xmouse%2)*game.tileH/2);
    }
    function getTarget() {
        naam = "t_"+game.ymouse+"_"+game.xmouse;
        if (game[naam].clickable and game[naam].speler == 0) {
            _root.tiles[naam].gotoAndStop(_root.huidigespeler+2);
            game[naam].speler = _root.huidigespeler;
            if(_root.huidigespeler==1) {
                _root.aantalblokjes1--;
            }else if(_root.huidigespeler==2){
                _root.aantalblokjes2--;
            }else if(_root.huidigespeler==3){
                _root.aantalblokjes3--;
            }else if(_root.huidigespeler==4){
                _root.aantalblokjes4--;
            }
            _root.wincheck(spelBord);
        } else if(game[naam].clickable) {
            _root.tiles[naam].gotoAndStop(2);
            game[naam].speler = 0;
            _root.wincheck(spelBord);
        }
    }
    function wincheck(map) {
        var mapWidth = map[0].length;
        var mapHeight = map.length;
        for (var y = 0; y<mapHeight; ++y) {
            for (var x = 0; x<mapWidth; ++x) {
                var naam = "t_"+y+"_"+x;
                if(game[naam].spelend) { //het is een tile van het spelbord
                    game[naam].clickable=false;
                    col = x%2;
                    col1 = (x+1)%2;
                    if(_root.huidigespeler==1) {
                        // set new clickable tiles
                        if (_root.aantalblokjes1 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes1==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    } else if(_root.huidigespeler==2) {
                    // set new clickable tiles
                        if (_root.aantalblokjes2 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes2==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    }else if(_root.huidigespeler==3) {
                    // set new clickable tiles
                        if (_root.aantalblokjes3 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes3==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    }else if(_root.huidigespeler==4) {
                    // set new clickable tiles
                        if (_root.aantalblokjes4 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes4==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    }
                    // check if the current player has won
                    if (game[naam].speler == _root.huidigespeler) {
                       
                    }
                }
            }
        }
        if(_root.verderspelen) {
            _root.huidigespeler++;
            if (_root.huidigespeler>_root.aantalspelers) {
                _root.huidigespeler = 1;
            }
        }
    }
    stop();
    Attached Files Attached Files
    Last edited by jerdb; 03-16-2010 at 12:36 PM. Reason: updated Flash file

  2. #2
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    Quote Originally Posted by jerdb View Post
    Now the problem with the coding: there's something wrong with counting the amount of pieces you have left.
    Welkom op het forum jerdb.

    Not much wrong with the counting. You set the clickable property of your tiles to false when they're created. Now this is your function to check if a piece should be placed on the tile:

    Actionscript Code:
    function getTarget() {
        naam = "t_"+game.ymouse+"_"+game.xmouse;
        if (game[naam].clickable and game[naam].speler == 0) {
            _root.tiles[naam].gotoAndStop(_root.huidigespeler+2);
            game[naam].speler = _root.huidigespeler;
            if(_root.huidigespeler==1) {
                _root.aantalblokjes1--;
            }else if(_root.huidigespeler==2){
                _root.aantalblokjes2--;
            }else if(_root.huidigespeler==3){
                _root.aantalblokjes3--;
            }else if(_root.huidigespeler==4){
                _root.aantalblokjes4--;
            }
            _root.wincheck(spelBord);
        } else if(game[naam].clickable) {
            _root.tiles[naam].gotoAndStop(2);
            game[naam].speler = 0;
            _root.wincheck(spelBord);
        }
    }

    Nothing will happen if (game[naam].clickable == false), which is the case with your code as it is. I changed the intial setting of the tiles.clickable to true and it seemed to work fine.

  3. #3
    Junior Member
    Join Date
    Aug 2008
    Posts
    14
    No, you are a little bit wrong: I set one tile to clickable in the middle of the board so you, as a player, would have to start there. The real problem is that if you have laid 6 tiles of the first color(blue), you have to delete as player 2 a blue tile instead of placing your last yellow tile.

  4. #4
    Funkalicious TOdorus's Avatar
    Join Date
    Nov 2006
    Location
    Nijmegen, Netherlands
    Posts
    697
    I see, but it's kinda weird that this would be related to the counting. I ran it through the debugger and all tiles seemed to have the clickable property set to true or it wasn't defined at all. Looking at your code I can't find where the surrounding cells are made clickable.

  5. #5
    Junior Member
    Join Date
    Aug 2008
    Posts
    14
    It's in the function wincheck:
    Actionscript Code:
    var mapWidth = map[0].length;
        var mapHeight = map.length;
        for (var y = 0; y<mapHeight; ++y) {
            for (var x = 0; x<mapWidth; ++x) {
                var naam = "t_"+y+"_"+x;
                if(game[naam].spelend) { //het is een tile van het spelbord
                    game[naam].clickable=false;
                    col = x%2;
                    col1 = (x+1)%2;
                    if(_root.huidigespeler==1) {
                        // set new clickable tiles
                        if (_root.aantalblokjes1 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes1==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    } else if(_root.huidigespeler==2) {
                    // set new clickable tiles
                        if (_root.aantalblokjes2 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes2==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    }else if(_root.huidigespeler==3) {
                    // set new clickable tiles
                        if (_root.aantalblokjes3 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes3==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    }else if(_root.huidigespeler==4) {
                    // set new clickable tiles
                        if (_root.aantalblokjes4 and game[naam].speler == 0 and (game["t_"+(y-1)+"_"+x].speler or game["t_"+(y+1)+"_"+x].speler or game["t_"+(y-col1)+"_"+(x-1)].speler or game["t_"+(y+col)+"_"+(x-1)].speler or game["t_"+(y-col1)+"_"+(x+1)].speler or game["t_"+(y+col)+"_"+(x+1)].speler)) {
                            game[naam].clickable=true;
                        } else if (_root.aantalblokjes4==0 and game[naam].speler == _root.huidigespeler) {
                            game[naam].clickable=true;
                        }
                    }

  6. #6
    Junior Member
    Join Date
    Aug 2008
    Posts
    14
    I now finally got the game working(see: http://jerreke-spelletjes.webs.com/HEXAGONS.html, but there is a minor bug in it: the yellow marker doesn't follow your mouse pointer that well: it sometimes shoots to another tile your pointer is still in the one next to it. Here is the code:
    Actionscript Code:
    function getMouse() {
        _root.muisaanwijzer.gotoAndStop(_root.currentplayer);
        _root.muisaanwijzer._x = _root._xmouse;
        _root.muisaanwijzer._y = _root._ymouse;
        _root.muisaanwijzer.gotoAndStop(_root.currentplayer+1);
        game.xmouse = Math.round((_root.muisaanwijzer._x-270)/game.tileS/1.5);
        game.ymouse = Math.round((_root.muisaanwijzer._y+15-((((game.xmouse+1)%2)*game.tileH/2)+(game.xmouse%2)*game.tileH))/game.tileH);
        var naam = "t_"+game.ymouse+"_"+game.xmouse;
        if (game[naam].inthegame) {
            _root.tiles.mouse._x = game.xmouse*game.tileS*1.5;
            _root.tiles.mouse._y = game.ymouse*game.tileH+((game.xmouse%2)*game.tileH/2);
        }
    }
    There is something wrong with the calculation of where the mouse should be or something like that. Could someone help me out please?
    Last edited by jerdb; 04-20-2010 at 02:53 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center