Copy link to clipboard
Copied
Ok I'm not sure if it's actually crazy or not but I seem to be having a REALLY hard time finding anyone else who has a similar question to mine.
I'll explain what I'm trying to do so you can all understand my question.
So:
I have 10 buttons on the stage at once. (We'll call them TestButtons for reference sake)
I want each button to do something different to the player's avatar based on whichever button is clicked. (IE: button 1 moves player to the left, button 2 moves player right, button 3 spins player around... just as an example)
I could easily do this by creating 10 different event listeners, and then 10 unique event handlers, but that would be tedious.
I'm trying to make my code as small and organized as possible, so what I want to do instead is include the event listener and the event handler inside the TestButton class file.
This worked no problem. Using only code, I created 10 unique instances of my TestButton on the screen, each with its own built-in listener and handler.
But now I want to change the x value of the player's avatar on the screen using these buttons...
Back in my main file, I have the code:
var playerOne:Player = new Player();
addChild(playerOne);
and since Player is defined elsewhere as an extension of MovieClip, playerOne inherently possesses an x value.
HOWEVER, when I include this code in the TestButton class event handler:
public function clickHandler(event:MouseEvent):void {
playerOne.x = 3;
}
It just tells me "access of undefined property playerOne"....
playerOne is definitely defined. It's defined inside the game's main.as file. But my TestButton.as file can't seem to see it...
I figured this could be solved by passing an argument from my one of my testButton's into the main file... but I also can't do that.
It seems that my testButtons cannot communicate in any way with any other variables outside of their own.
Which makes no sense to me... why is it that in my main file I can reference testButton.x, but in my TestButton file I can't access playerOne.x?
Copy link to clipboard
Copied
Your TestButton class needs to have a reference passed into it for the playerOne object. otherwise it has no way of knowing what it is dealing with from the outside world. The main file either created testButton, or it is the document class file wherein it can reference anything that you placed on the stage.
Copy link to clipboard
Copied
Thanks, that makes sense. I understand the why now but I guess I don't understand the how.
I'll explain exactly what I'm trying to do so there's no confusion on either end of this conversation:
Lets say the document class file dynamically creates a new instance of TestButton and names it testButton. It then pushes that instance into an array that we'll call testArray. Now essentially, the reference ID for this instance of TestButton has just become testArray[0]. testArray[0] has an event listener inside of it that triggers when the player clicks on it, and when clicked on, it calls the internal function clickHandler. clickHandler tries to change playerOne.x to something else... but it can't because it doesn't know what playerOne is...
I get that this is because testButton can't see playerOne, and so it would have to be passed a reference.... but how would you do that in this situation?
I don't really see "var testButton:TestButton = new TestButton(playerOne)" as being the answer... and since they are all being dynamically created and slotted into an array, we also can't say "Oh well just put the event listener into the document class file!" Because in this situation an undefined number of testButtons are going to be created, so creating a predetermined number of event listeners wouldn't work. You'd have to do something like testArray[buttonNumber].addEventListener(MouseEvent.CLICK, clickHandler) but that also wouldn't work for obvious reasons. (The value of buttonNumber in this situation can only be determined by using the eventlistener it's attached to, in order to see which button was clicked on.)
I know there's some function or operator or something out there that AS3 has built into it that would do exactly what I'm looking for, but without being extremely specific in what exactly I'm trying to do, I don't think anyone will ever be able to tell me what I'm looking for. So sorry for the overly thorough explanation, I just wanna make sure everyone gets EXACTLY what I'm trying to do so there's no confusion or incorrect answers.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now