• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Make the code find on wich movieclip I clicked

Contributor ,
Jan 28, 2014 Jan 28, 2014

Copy link to clipboard

Copied

Hi all,

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 differents 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 exemple, 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 {

.. .

I've asked around, and we told me to put :

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

}

But I don't really see where to put it. if someone could help me, it'll be a huge help.

So, here's my coden and my organisation :

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;

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.displayName == "HOUSE")

house = Engine.usableItems;

if (Engine.usableItems.displayName == "EMPTYSPACE1")

emptyspace1= Engine.usableItems;

}

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 much classes ?

Thank you again for the help.


TOPICS
ActionScript

Views

578

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jan 28, 2014 Jan 28, 2014

this is correct:

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

if (clickedSpace == emptyspace2) {
someBuilingInEmptySpace2.visible = true ;

} else if (clickedSpace == emptyspace

...

Votes

Translate

Translate
Community Expert ,
Jan 28, 2014 Jan 28, 2014

Copy link to clipboard

Copied

this is correct:

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

if (clickedSpace == emptyspace2) {
someBuilingInEmptySpace2.visible = true ;

} else if (clickedSpace == emptyspace3 {
someBuilingInEmptySpace3.visible = true ;

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 28, 2014 Jan 28, 2014

Copy link to clipboard

Copied

Thanks ! And, in witch class do you think I should put this code ? In Puzzle.as ?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jan 28, 2014 Jan 28, 2014

Copy link to clipboard

Copied

if your emptyspace objects have a class, you wouldn't even need an if-statement and that would be a logical place to add your listener.

otherwise, add it to the class where you create your emptyspace objects.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Jan 28, 2014 Jan 28, 2014

Copy link to clipboard

Copied

LATEST

Nope, that's not it. I must have badly explain the situation.

I'm gonna search on my own and come back with more info. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines