Hello everybody! I’m currently making a TicTacToe game to help build my Flash experience and I’ve come across an issue. I’ll try and explain what everything is doing in my code thus far. I know it’s a lot of code for a forum post, but I don’t want to leave anything out as someone may have a really nice way to work with this.
//this will be switched to a user defined value (1 or 2), depending on which person the
//user decides should go first. currently it is set up for the 2nd player to go first
new _global.whosTurn();
_global.whosTurn = 2;
//this sets all cells DISABLED so, at the beginning of the game, any cell is selectable
cell1_isUsed = false;
cell2_isUsed = false;
cell3_isUsed = false;
cell4_isUsed = false;
cell5_isUsed = false;
cell6_isUsed = false;
cell7_isUsed = false;
cell8_isUsed = false;
cell9_isUsed = false;
theDepth = 100;
//cell_status : 1==X 2==O nil==0(zero). This keeps track of which cell belongs to X and to
//O, so that the checkForWinner function can keep track of when someone has won
var cell1_status = 0;
var cell2_status = 0;
var cell3_status = 0;
var cell4_status = 0;
var cell5_status = 0;
var cell6_status = 0;
var cell7_status = 0;
var cell8_status = 0;
var cell9_status = 0;
//this function is incomplete, it will check to see if any user has a tic-tac-toe and declare the winner
function checkForWinner() {
if ((cell1_status && cell2_status && cell3_status) == 1) {
whosTurn_txt.text = "Player 1 wins.";
}
}
//this function is responsible for alternating turns between players, not much else to say here
function switchTurns() {
if (_global.whosTurn == 1) {
_global.whosTurn = 2;
} else if (_global.whosTurn == 2) {
_global.whosTurn = 1;
}
}
//this minor function simply gets an x and y value for the placement of the X or the O, if
//you look further down at the markItUp function these values play the part of positioning
//the X or the O in the center of whichever cell the user selected
function placement(which_cell) {
placementX = which_cell._x;
placementY = which_cell._y;
}
//this is the function that is screwed up. the first X and the first O that are put onto the
// stage are great and do what it is supposed to do, but after that what I'm doing (due to
// my lack of coding knowledge ) is putting multiple movie clips on the stage with the SAME
// instance name. this is where the problem lies and where I need help figuring out how to
// give each movie clip its own unique name
function markItUp(which_cellStatus) {
if (whosTurn == 1) {
attachMovie("marker_X","xMarker",theDepth);//need to somehow declare unique movieclip instance names
_root.xMarker._x = placementX+8;
_root.xMarker._y = placementY+12;
which_cellStatus = 1;
switchTurns();
} else {
attachMovie("marker_O","yMarker",theDepth);//need to somehow declare unique movieclip instance names
_root.yMarker._x = placementX+8;
_root.yMarker._y = placementY+12;
switchTurns();
}
}
//this function handles all the above functions when a user clicks on a cell
function clickedCell(which_cellIsUsed, whichCell, cell_status) {
whichCell.onRelease = function() {
if (which_cellIsUsed == false) {
placement(whichCell);
markItUp(cell_status);
which_cellIsUsed = true;
theDepth++;
} else {
whosTurn_txt.text = "Pick a different cell";
}
//checkForWinner();
};
}
//sets up the clickedCell function
cell1.onPress = clickedCell(cell1_isUsed, cell1, cell1_status);
cell2.onPress = clickedCell(cell2_isUsed, cell2, cell2_status);
cell3.onPress = clickedCell(cell3_isUsed, cell3, cell3_status);
cell4.onPress = clickedCell(cell4_isUsed, cell4, cell4_status);
cell5.onPress = clickedCell(cell5_isUsed, cell5, cell5_status);
cell6.onPress = clickedCell(cell6_isUsed, cell6, cell6_status);
cell7.onPress = clickedCell(cell7_isUsed, cell7, cell7_status);
cell8.onPress = clickedCell(cell8_isUsed, cell8, cell8_status);
cell9.onPress = clickedCell(cell9_isUsed, cell9, cell9_status);
Sorry, I know it’s a lot of code for someone to look at without being paid, but I would appreciate any help anyone could provide.
~Jason