I have four coins: coin1, coin2, coin3, coin4, They were dragged and dropped to the stage.
All I want is if the user clicks a coin, it gets removed from the stage!
Thanks!
(I used an array and yes, it doesn’t work because it’s in a different function) … oh well…
(updated code: formatted)
package {//v0.1
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class CountPage extends MovieClip
{
private var _objectsRemaining:uint;
private var _objectsCounted:uint;
private var _gameStatus:String;
private var _gameWon:Boolean;
private var _startMessage:String;
//declare Array
private var _collectionJar:Array;
public function CountPage()
{
// constructor code
//_stage = stage;
this.addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
}
private function addedToStageHandler(event:Event):void
{
init();
trace("Added to Stage!");
this.removeEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);
}
public function init():void
{
//directives inside the constructor method are automatically run when the class is instantiated
//this is to reinitialize(RESET) everything easily from Anywhere in the program!
//Let's Initialize variables!
trace("Game started!");
_startMessage = "Let's count!";
_objectsRemaining = 10;
_objectsCounted = 0;
//Initialize text fields
messageTextBox.text = _startMessage;
output.text = "";
output.backgroundColor = 0xffffff;
output.restrict = "0-9";
//stage.focus = output;
//Instantiate array
_collectionJar = new Array ;
_collectionJar = [];
//A variable to store the reference
//to the stage from the application class
//private var _stage:Object;
//Initialize buttons
for (var i:int = 1; i <= 4; i++)
{
//Collision.playerAndPlatform(player, this["coin" + i], 0.2, 0);
this["coin" + i].enabled = true;
this["coin" + i].alpha = 1;
//trace("coin" + i);
//Add an event listener to the button
this["coin" + i].addEventListener(MouseEvent.CLICK, onClick);
// trace("coin EventListener " + i);
_collectionJar[0] = "0";
_collectionJar.push("coin" + i);
trace("Element" + i + _collectionJar*);
}
}
public function onClick(event:MouseEvent):void
{
trace("You clicked on the coin!");
_objectsCounted = _objectsCounted + 1;
output.text = "" + _objectsCounted;
if (coin1)
{
this[_collectionJar[1]].alpha = 0;
//removeChild(_collectionJar[1]);
_collectionJar[1] = null;
}
if (coin2)
{
this[_collectionJar[2]].alpha = 0;
//removeChild(_collectionJar[1]);
_collectionJar[2] = null;
}
}
}
}