Help, button in an Mc (solve)

12 posts

Flag Post

I tried for a long time to have it work, but I always get errors. I know its an easy question, but can someone help me. I have an upgrade menu for my game. Inside my upgrade menu, I have a button ( Upgrade). I try to add an eventListener ( when mouse click) to the button inside my menu, but I don’t know how to add it to the button, I always get errors.

Thanks

 
Flag Post

upgradeButton.addEventListener(MouseEvent.CLICK, upgrade);

 
Flag Post

Thanks, But I still have the problem. Because what I want to know is how to add the eventListener to my upgradeButton who is inside my Menu. Do I have to create a var for it? Or do I have to refer to it like Menu.upgradeButton.addEventListener …..

*With the last line, I get error 1119 and 1120.

 
Flag Post

Let’s assume that where you put the eventListener, you have the menu as a variable ‘menu’. Also, the upgradeButton should be a public variable of the menu class, so it can be accessed from outside.

Then you can just write:
var button:WhateverClassThisIs = menu.upgradeButton;
button.addEventListener(MouseEvent.CLICK, upgrade);

There are tons of ways to do it, but this is the way I’d use.

 
Flag Post

…. In your menu class make sure to reference the instance variable names not the name of the movieclip but the name of its instantation inside of your menu MovieClip.

Go to your menu MC and select your upgrade button inside of the menu MC and go to properties pane and give it an instance name of (whatever you want, lets say Cheese).

so back in your menu class it should be Cheese.addEventListener(MouseEvent.CLICK, upgrade);

 
Flag Post

I still do something wrong. Sorry this is my first game. Here is my code:

static var menuInGame:MovieClip;
var upgradeButton:MenuInGame = menuInGame.UpgradeButton;

……

menuInGame = new MenuInGame();
menuInGame.x = 0;
menuInGame.y = 700;
addChild(menuInGame);

….

menuInGame.upgradeButton.addEventListener(MouseEvent.CLICK, clickUpgradeButton);

edit* And trying the last line like this also give me error 1120 : upgradeButton.addEventListener(MouseEvent.CLICK, clickUpgradeButton);

 
Flag Post

Where does the last line come from. frupgrade?

Also, could you copy/paste the exact errors you’re getting? Makes it alot easier.

 
Flag Post

ye sory I edit it, error copiing

And yes, Inside my MenuINGame, my upgradeButton have an instance name UpgradeButton. And its a button

 
Flag Post

Are you sure that your MenuInGame class contains the upgradeButton? If it does, are you sure it’s set as a ‘public’?

 
Flag Post

Thinks thats the problem, how do I set it at public

And yes it contain the upgradeButton

 
Flag Post

public var upgradeButton:Whatever

Although personally I’d go with internal for something like this.

 
Flag Post

Its good, Ive found a solution, thanks