Tryzx
104 posts
|
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
|
|
|
Maqrkk
45 posts
|
upgradeButton.addEventListener(MouseEvent.CLICK, upgrade);
|
|
|
Tryzx
104 posts
|
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.
|
|
|
Maqrkk
45 posts
|
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.
|
|
|
NicheGamer
472 posts
|
…. 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);
|
|
|
Tryzx
104 posts
|
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);
|
|
|
Maqrkk
45 posts
|
Where does the last line come from. frupgrade?
Also, could you copy/paste the exact errors you’re getting? Makes it alot easier.
|
|
|
Tryzx
104 posts
|
ye sory I edit it, error copiing
And yes, Inside my MenuINGame, my upgradeButton have an instance name UpgradeButton. And its a button
|
|
|
Maqrkk
45 posts
|
Are you sure that your MenuInGame class contains the upgradeButton? If it does, are you sure it’s set as a ‘public’?
|
|
|
Tryzx
104 posts
|
Thinks thats the problem, how do I set it at public
And yes it contain the upgradeButton
|
|
|
BobJanova
853 posts
|
public var upgradeButton:Whatever
Although personally I’d go with internal for something like this.
|
|
|
Tryzx
104 posts
|
Its good, Ive found a solution, thanks
|