PDA

View Full Version : [AS2] My First Actionscript Project - Coinflip Game


jcbrady89
12-29-2008, 02:24 AM
I've been studying for months and finally decided to create a project. It's a simple coin flip game in which I'm 95% finished. Unfortunately, I ran into a glitch that I can't figure out, relating to the removeMovieClip() method. Here is my code for the project:


this.createEmptyMovieClip("canvas_mc", 1);
canvas_mc._x = 140;
canvas_mc._y = 90;
canvas_mc.attachMovie("Heads", "Heads_mc", canvas_mc.getNextHighestDepth(), {_x:canvas_mc._x, _y:canvas_mc._y});
var headsTails:Number;
var i:Number = 0;
function flip():Number {
headsTails = (Math.round(Math.random()));
return headsTails;
}
function removeTails() {
//removeMovieClip(canvas_mc.target.name);
removeMovieClip(canvas_mc.CoinSpin_tails_mc+[i]);
//canvas_mc.CoinSpin_tails_mc[+i].removeMovieClip;
//trace(canvas_mc.CoinSpin_tails_mc[+i].name);
//removeMovieClip(canvas_mc.CoinSpin_tails_mc1);
//removeMovieClip(canvas_mc.CoinSpin_tails_mc2);
//removeMovieClip(canvas_mc.CoinSpin_tails_mc3);
//removeMovieClip(canvas_mc.CoinSpin_tails_mc4);
//removeMovieClip(canvas_mc.CoinSpin_tails_mc5);
}
function removeHeads() {
//removeMovieClip(canvas_mc.target.name);
removeMovieClip(canvas_mc.CoinSpin_heads_mc+[i]);
//canvas_mc.CoinSpin_heads_mc[+i].removeMovieClip;
//removeMovieClip(canvas_mc.CoinSpin_heads_mc1);
//removeMovieClip(canvas_mc.CoinSpin_heads_mc2);
//removeMovieClip(canvas_mc.CoinSpin_heads_mc3);
//removeMovieClip(canvas_mc.CoinSpin_heads_mc4);
//removeMovieClip(canvas_mc.CoinSpin_heads_mc5);
}
flip_btn.onPress = function() {
flip();
removeHeads();
removeTails();
removeMovieClip(canvas_mc.Heads_mc);
//removeMovieClip(canvas_mc.target.name);
i++;
if (headsTails == 0) {
trace('tails');
canvas_mc.attachMovie("CoinSpin_tails", "CoinSpin_tails_mc"+i, canvas_mc.getNextHighestDepth(), {_x:canvas_mc._x, _y:canvas_mc._y});
} else {
trace('heads');
canvas_mc.attachMovie("CoinSpin_heads", "CoinSpin_heads_mc"+i, canvas_mc.getNextHighestDepth(), {_x:canvas_mc._x, _y:canvas_mc._y});
}
};



I'm actually trying to get this counter to work. I left the commented lines in the above code to show some of the things I've tried; //removeMovieClip(canvas_mc.CoinSpin_heads_mc1); do what I need to do but I can't get the counter to work properly. All my other attempts to remove the movie clips did not work except when I targeted them directly with the number at the end.

:eek:

atomic
12-29-2008, 02:31 AM
Attach your .fla to this forum...

jcbrady89
12-29-2008, 04:41 AM
Here is the fla file, (I haven't put the final graphics in it yet).

atomic
12-29-2008, 05:09 AM
A bit confused...

Are you saying that as it is, this is working as you would want it to, or not?

Or that on every press after the first press of the flip button, the result clip should be removed and I should only see the flipping movement as I first saw on the firts press?

Am I making sense?

atomic
12-29-2008, 06:22 AM
Think all you need is this...


stop();
this.createEmptyMovieClip("canvas_mc", 1);
canvas_mc._x = 140;
canvas_mc._y = 90;
canvas_mc.attachMovie("Heads", "Heads_mc", canvas_mc.getNextHighestDepth(), {_x:canvas_mc._x, _y:canvas_mc._y});
var headsTails:Number;
var i:Number = 0;
function flip():Number {
headsTails = (Math.round(Math.random()));
return headsTails;
}
flip_btn.onPress = function() {
canvas_mc.Heads_mc.removeMovieClip();
flip();
if (headsTails == 0) {
//trace('tails');
canvas_mc.attachMovie("CoinSpin_tails", "CoinSpin_tails_mc"+i, 10, {_x:canvas_mc._x, _y:canvas_mc._y});
} else {
//trace('heads');
canvas_mc.attachMovie("CoinSpin_heads", "CoinSpin_heads_mc"+i, 10, {_x:canvas_mc._x, _y:canvas_mc._y});
}
};


In fact attaching either clip to the same depth - I choose 10, rather than getNextHighestDepth() - automatically removes the previous clip that was attached to that depth...

jcbrady89
12-30-2008, 04:11 AM
It seems like I tried everything but that! It works fine now.

I guess that's a bit of a gotcha when using getNextHighestDepth()

atomic
12-30-2008, 04:15 AM
Grrrrreat! ;)