Tic Tac Toe(AS3.0)

Started by
7 comments, last by Aezon 14 years, 7 months ago
I have a bit of a problem, I made a basic Tic Tac Toe engine, and I have a bit of a problem cleaning up after a game is over. Basically, Once the board is full, or if there is three in a row, it calls this method: function clearDisplayGrid(emptySpaces:int = 0):void{ //max is the number of actual pieces on the board. var max:int = 9-emptySpaces; for(var ind:int = 1;ind<=max;ind++){ this.removeChildAt(ind); } } I have run it through the Flash debugger, and still haven't figured out why it doesn't remove any of the display objects. Please help me. The rest of the program is available at request. [Edited by - Aezon on September 9, 2009 6:45:15 PM]
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
Advertisement
Some things to consider:

1) Are you sure the board doesn't have any "children" other than the pieces? (Possibly including things you didn't explicitly create yourself? What type is it?)

2) Why do you pass 'emptySpaces' as a parameter? Doesn't the board already "know" how many spaces are empty?

3) As in most languages, AS3 array indices start at 0, not 1. (Or is this because of your answer to (1)? ;) )

4) Suppose you remove a child successfully. Any children after that element will get re-numbered, right? Think about the effect that has on your logic.
hold all movieclips in array and while adding them to scene (addChild) like

mcArray.push(newmc);
this.addChild(newmc);

when game is over you can say

for(var i=0; i<mcArray.length; i++){
this.removeChild(mcArray);
}

after that set mcArray to undefined and hope that garbage collector will deal with it.


My another suggestion is, create an empty movie clip, first frame is empty, second has an O symbol and third has a X symbol. add 9 of these movieclip into the scenes (and call .stop() command after adding these) and make a 3x3 board

so if you want to change a square into O just call .gotoAndStop(1) or for X call .gotoAndStop(2) on that movie clip.

if you want to clear the board call .gotoAndStop(0) for all nine movieclips

I didn't use flash for a long time, sorry if I said something wrong
taytay
Quote:Original post by Zahlman
4) Suppose you remove a child successfully. Any children after that element will get re-numbered, right? Think about the effect that has on your logic.

That's what i remember too.
while (parent has movie clips){  this.removeChildAt(0);}

Although I agree with shultays that keeping them (movie clips, sprites) in an array might be preferable here.
If you do it with a for loop just remember to loop backward through the children so that the indices dont change after you remove.

[Edited by - dustin10 on September 10, 2009 2:00:56 PM]
Thank you, everyone, for your help. I will now reply to each point:

@zahlman:
1. In other parts of the code, I had it explicitly set the index of each item to be deleted, for simplicity.

2. The number of empty spaces are calculated, and sent to the function, so max only equals the number of display objects already in existance. That way, there are no out of bounds runtime errors, etc.

3. I forget my logic there. XD

4. I had completly forgotten that. That is probably part of my problem. XD Thanks for pointing this out.

@shultays: I did put everything into an array, but popped it afterwards. Your idea is much better though.

@Wanmaster: Another good way to do that... Thanks for pointing this out.

I think I got it now, Thanks everyone, I will report back if there are any problems.
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
Well, it works, a little bit.

I modified the code to this:

function clearDisplayGrid(emptySpaces:int = 0):void{
//removes all instances of x and o pieces.
var max:int = 9-emptySpaces;
for(var ind:int = 0;ind<=max;ind++){
removeChildAt(0);
}
}

However, it only removes the piece I added before the game is over.(Ie, I add the final 'x' that makes a win, and it removes the last 'o' piece I placed)
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!
as I said, keep movieclips in array and remove theme from the scene using this array.

there is no way that x and o movieclips will be always move to the front if there are other movie clips in the scene. you might remove them from the scene.

you can also use this but I am not sure this will work either.

var container = new MovieClip();

this.addChild(container);

after that try adding x-o movie clips to this container.

container.addChild( ... );

if you want to remove all x o movie clips just call

this.removeChild(container);

but again, I am not sure this will work either. Best way is the keeping the movie clips in array and removing them from the scene using removeChild function. or my other suggestion in the previous post
taytay
Thank you, I tried the array method, and it works.

And I just tried an experimental(for me, I just love inventing the wheel) way to clear an array. Just doing array = new Array();.



Thank you everyone for your help!
-----------------------Are you breaking into the world of game development, and are finding yourself lost?I am also a beginner, whose here to get lost-and find his way back-for you. Check out my blog You will(hopefully) learn bunches, alongside me.*Last update: 1/1/10*New Theme!

This topic is closed to new replies.

Advertisement