I'm making a game in AS3.
I've got a scene with empty space. There are empty space 1, empty space 2...etc
When the player click on an empty space, a usebox appear with 2 buttons. It the same usebox for all the empty spaces. When the player click on the buttons "buy" a windows appears where he can choose between different buildings (same windows for all the empty spaces).
When he clicks on the building that he wants, the function "build" is called ( someBuilding.visible = true
) and the building appears on the scene.
But, I don't how to tell the code to make the building corresponding to the empty space visible..
If the player has clicked on the empty space 2 for example, how can I tell the code to make someBuilingInEmptySpace2.visible = true
? If he has clicked on the empty space 3 --> someBuilingInEmptySpace3.visible = true
?
something like:
function build():void{
if clicked on emptyspace2 {
someBuilingInEmptySpace2.visible = true ; }
if clicked on emptyspace3 {
..
.
EDIT
Thank you for your answers, but I don't really see where to put it. Here's my code:
I've got Engine.as where the elements of the game are:
private function createBackground(thisBack:String):void{
for (i in usableItems){
var thisClip = usableItems[i];
thisClip.buttonMode = true;
thisClip.addEventListener(MouseEvent.CLICK, examine, false, 0, true);
thisClip.gotoAndStop(1);
}
private function createUI():void{
toolbar = new Toolbar(stage);
addChild(toolbar);
toolbar.x = 0;
toolbar.y = 400;
buildings = new Buildings(stage);
addChild(buildings);
buildings.visible = false;
puzzle = new Puzzle(stage);
stage.addEventListener("endGame", endGame, false, 0, true);
}
private function examine(e:MouseEvent):void{
stage.dispatchEvent(new Event("itemClicked"));
useBox = new UseBox(stage, e.currentTarget);
useBox.x = mouseX;
useBox.y = mouseY;
stage.addChild(useBox);
}
So when the player clicks on an empty space (emptyspace2 for exemple), the function examine is called and the useBox appears.
In my UseBox.as :
public function UseBox(stageRef:Stage, thisThing:Object){
this.stageRef = stageRef;
this.thisThing = thisThing;
this.visible = true;
useButton.buttonMode = true;
lookButton.buttonMode = true;
lookButton.addEventListener(MouseEvent.CLICK, lookAt, false, 0, true);
useButton.addEventListener(MouseEvent.CLICK, useThing, false, 0, true);
private function useThing(e:MouseEvent):void{
Engine.buildings.visible = true;
useButton.removeEventListener(MouseEvent.CLICK, useThing);
}
In my Buildings.as
public function Batiments(stageRef:Stage){
houseBtn.addEventListener(MouseEvent.CLICK, houseConstruction, false, 0, true);
}
private function houseConstruction(e:MouseEvent):void{
var house;
var emptyspace1;
trace("choix");
stage.dispatchEvent(new Event("itemClicked"));
Engine.buildings.visible = false;
Engine.puzzle.houseBuilt();
toolbar.money.text = String( Number(toolbar.money.text ) - 100 );
}
And, finally, in my Puzzle.as
public function Puzzle(stageRef:Stage){
allPuzzles = new Object;
allPuzzles.tundra.maisonBought = false;
}
public function maisonBuilt():void{
var house;
var emptyspace1;
for (var i in Engine.usableItems){
if (Engine.usableItems[i].displayName == "HOUSE")
house = Engine.usableItems[i];
if (Engine.usableItems[i].displayName == "EMPTYSPACE1")
emptyspace1= Engine.usableItems[i];
}
allPuzzles.tundra.houseBought = true;
house.visible = true;
house.gotoAndStop("contruction");
house.lookTag = "1";
timeConstruction.addEventListener(TimerEvent.TIMER_COMPLETE, constructionFini);
timeConstruction.start();
emptyspace1.visible = false;
}
Where should I put the emptyspace listeners? Have I made too many classes?
This can be accomplished by checking either the target
or currentTarget
property of the Event
:
// you should probably do this part in a loop of some sort.
emptySpace1.addEventListener(MouseEvent.CLICK, onSpaceClicked);
emptySpace2.addEventListener(MouseEvent.CLICK, onSpaceClicked);
emptySpace3.addEventListener(MouseEvent.CLICK, onSpaceClicked);
function onSpaceClicked(evt:MouseEvent):void
{
var clickedSpace:MovieClip = evt.currentTarget as MovieClip;
//... perform the actions you need to perform on the clickedSpace
}
addEventListener(MouseEvent.CLICK, onSpaceClicked);
function onSpaceClicked(event:MouseEvent):void
{
if ( event.target === emptySpace1 ) {
// case 1...
} else if ( event.target === emptySpace2 ) {
// case 2...
} else if ( event.target === emptySpace3 ) {
// case 3...
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.