maxbadger
February 3rd, 2010, 08:02 PM
Howdy Everyone-
I'm new to actionscript. A great deal of what I've learned has come from browsing the forums here.
I have a problem that I'm hoping someone can help me with. I should say in advance that I'm not at all sure about the syntax I'm using, especially for referencing the variable kept in the main document class. It seems cumbersome.
I have four instances of the same class called Tile, that is bound to a movieclip with four frames. They're imbedded in a movieclip that I'm using to hold all of the visuals.
In the main class of my .fla I declare a variable called menu1 that is bound to another movieclip with 2 buttons on it.
I'm trying to make three things happen here: on clicking the Tiles the menu will pop up over the tile that was clicked; that on clicking another tile when the menu is already up it will close that one before adding a new one; and lastly, that on clicking the second button in the menu, it will change the frame of the tile clicked and close the menu.
I am having a great deal of difficulty implementing all of this.
The tile.as has an addeventlistener in it's construction method to add the function onTileClick to each Tile.
public class Tile extends MovieClip{
public function Tile():void{
addEventListener(MouseEvent.CLICK, onTileClick);
}
}
The onTileClick function checks for an existing menu variable and removes it if it's found.
function onTileClick(event:MouseEvent):void{
if(parent.contains(MovieClip(parent.parent).menu1) {
parent.removeChild(MovieClip(parent.parent).menu1) ;
}
Then it adds the menu1, and adds the event listeners to the menu's buttons.
parent.addChild(MovieClip(parent.parent).menu1);
MovieClip(this.parent.parent).menu1.closeBTN.addEv entListener(MouseEvent.CLICK, onCloser);
MovieClip(this.parent.parent).menu1.advanceBTN.add EventListener(MouseEvent.CLICK, onAdvance);
The onCloser works like this:
function onCloser(event:MouseEvent):void{
parent.removeChild(MovieClip(parent.parent).menu1) ;
}
And the onAdvance function works like this:
function onAdvance(event:MouseEvent):void{
this.gotoAndStop(2);
parent.removeChild(MovieClip(parent.parent).menu1) ;
}
This works fine for one tile. I can click on it, the menu pops up, I can close and reopen the menu as many times as I want, and I can use the other button with no problems.
Also, clicking around on different tiles moves the menu around without repeatedly adding it, so that seems to be working fine as well.
The problem is this: If I click on a tile, close the menu, then click on another tile and close it, I get an error 1009.
It still closes, that error just comes up. And it will come up for any more tiles I open and close it on.
Then, after clicking on however many tiles, if I do click the advance button, it affects all of the tiles, and I get an ArgumentError: Error #2025 for each tile clicked on.
I tried adding a variable that held the event.target value in the onAdvance function, and using that for the gotoAndStop, but the same thing happened. When I tried tracing the name of the variable, I noticed that it was tracing the names of each tile clicked with each subsequent trace.
Am I building this all wrong?
I thought that making a class for my tile object, and adding eventlisteners to the class, and adding the functionality of the tiles inside of the class would be good practice for Object Oriented Programming, but it seems to be producing nothing but errors. When I defined these tiles inside the main timeline, with separate event listeners, I didn't have this problem.
If anybody could tell me where I've gone wrong, I would much appreciate it!
I'm new to actionscript. A great deal of what I've learned has come from browsing the forums here.
I have a problem that I'm hoping someone can help me with. I should say in advance that I'm not at all sure about the syntax I'm using, especially for referencing the variable kept in the main document class. It seems cumbersome.
I have four instances of the same class called Tile, that is bound to a movieclip with four frames. They're imbedded in a movieclip that I'm using to hold all of the visuals.
In the main class of my .fla I declare a variable called menu1 that is bound to another movieclip with 2 buttons on it.
I'm trying to make three things happen here: on clicking the Tiles the menu will pop up over the tile that was clicked; that on clicking another tile when the menu is already up it will close that one before adding a new one; and lastly, that on clicking the second button in the menu, it will change the frame of the tile clicked and close the menu.
I am having a great deal of difficulty implementing all of this.
The tile.as has an addeventlistener in it's construction method to add the function onTileClick to each Tile.
public class Tile extends MovieClip{
public function Tile():void{
addEventListener(MouseEvent.CLICK, onTileClick);
}
}
The onTileClick function checks for an existing menu variable and removes it if it's found.
function onTileClick(event:MouseEvent):void{
if(parent.contains(MovieClip(parent.parent).menu1) {
parent.removeChild(MovieClip(parent.parent).menu1) ;
}
Then it adds the menu1, and adds the event listeners to the menu's buttons.
parent.addChild(MovieClip(parent.parent).menu1);
MovieClip(this.parent.parent).menu1.closeBTN.addEv entListener(MouseEvent.CLICK, onCloser);
MovieClip(this.parent.parent).menu1.advanceBTN.add EventListener(MouseEvent.CLICK, onAdvance);
The onCloser works like this:
function onCloser(event:MouseEvent):void{
parent.removeChild(MovieClip(parent.parent).menu1) ;
}
And the onAdvance function works like this:
function onAdvance(event:MouseEvent):void{
this.gotoAndStop(2);
parent.removeChild(MovieClip(parent.parent).menu1) ;
}
This works fine for one tile. I can click on it, the menu pops up, I can close and reopen the menu as many times as I want, and I can use the other button with no problems.
Also, clicking around on different tiles moves the menu around without repeatedly adding it, so that seems to be working fine as well.
The problem is this: If I click on a tile, close the menu, then click on another tile and close it, I get an error 1009.
It still closes, that error just comes up. And it will come up for any more tiles I open and close it on.
Then, after clicking on however many tiles, if I do click the advance button, it affects all of the tiles, and I get an ArgumentError: Error #2025 for each tile clicked on.
I tried adding a variable that held the event.target value in the onAdvance function, and using that for the gotoAndStop, but the same thing happened. When I tried tracing the name of the variable, I noticed that it was tracing the names of each tile clicked with each subsequent trace.
Am I building this all wrong?
I thought that making a class for my tile object, and adding eventlisteners to the class, and adding the functionality of the tiles inside of the class would be good practice for Object Oriented Programming, but it seems to be producing nothing but errors. When I defined these tiles inside the main timeline, with separate event listeners, I didn't have this problem.
If anybody could tell me where I've gone wrong, I would much appreciate it!