PDA

View Full Version : [AS3] when the player clicks on the red blocks, I want them to be removed from the st


alyskiss
04-27-2011, 06:31 PM
Hi,
I'm creating a game in Flash CS5 and it is a grid of blocks, different colors.
What I want to do is when the player clicks on the red blocks, I want them to be removed from the stage. But if the player clicks on any other color (green, blue, yellow), points will be deducted and nothing will happen (it will remain on the stage). I am calling upon a movie clip in the library which has four different colors on it on the timeline. I gave each of them a name in the label section.

My question is how do I call upon each separate color from the movie clip?
I'm going to need an IF ELSE statement. If the block is red > remove from stage. Else leave it there and deduct points....

this is what I have so far

var newBox101:Box = new Box();
newBox101.type = Math.ceil(Math.random()*4);
newBox101.gotoAndStop(newBox101.type);
newBox101.x=10;
newBox101.y=10;
addChild(newBox101);
newBox101.addEventListener(MouseEvent.CLICK,boxLea ve101);
function boxLeave101(event:MouseEvent){
trace("it works 101");
newBox101.x=-100;
}

The square in the library is named 'Box' if that helps..

Any one know how to do this?
Thanks so much!!

abeall
04-27-2011, 06:47 PM
Sounds like you have the general idea figured out already. You can check .type, .currentFrame, or .currentLabel to determine if the box is red or not when it's clicked.

Inside your click handler you can use event.currentTarget to get a reference to the box being clicked, since I assume you are looping to create a bunch of boxes somewhere.

Lawnguy
04-27-2011, 06:52 PM
yourbutton.addEventListener(MouseEvent.CLICK, opensSomething);

function opensSomething (event:MouseEvent) : void {
addChild (something);
}

var something:MovieClip = new SomethingsClassName();

something.x = 0; //position of "something" when it appears
something.y = 0;

//

closebutton.addEventListener(MouseEvent.CLICK, closeSomething);

function closeSomething (event:MouseEvent) : void {
removeChild (something);

alyskiss
04-27-2011, 07:43 PM
abeall: i feel like I kind of understand what you are talking about, but I'm not actually doing loops (yes I know its crazy) but I was having issues with it, and I'm fairly new at this, so I just wrote it all out.
Anyway, how would I go about using the currentLabel or currentFrame? would that go in my If statement?? You mentioned using the event:currentTarget but since im not using a loop, what other way could I do that?

alyskiss
04-27-2011, 08:21 PM
abeal: thanks! i figured it out. i used the .currrentFrameLabel code
im so excited! thanks again!