PDA

View Full Version : Instances of a class sharing 'this' value?



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!

IQAndreas
February 4th, 2010, 10:02 AM
I'm having a hard time hanging along with what you are trying to do.

Could you upload a sample FLA along with your classes?

_kp
February 4th, 2010, 10:47 AM
I'm also not really sure what you are doing there.
Sounds like you have a few tiles and one menu, that you want to appear on a tile you click and also is closable?

I think most of your errors are related to your parent.parent constructs. I wouldn't even recommend using a single parent in most cases ;)
If you only have one menu instance, there is no need to use removeChild before using addChild, it will be removed from the old list anyway. I would go this way: create your menu somewhere and pass a reference of it to the tile class in it's constructor. When you want to add it you just use this.addChild(menu), if you want to close it use this.removeChild(menu).

edit: just noticed you are adding it to the tiles parent.. maybe you should restructure this, as I said above it's generally not a good idea to let a child control it's parents. As it looks now, you could move the whole eventlisteners to the parent class if you need the menu there?

maxbadger
February 4th, 2010, 12:58 PM
edit: just noticed you are adding it to the tiles parent.. maybe you should restructure this, as I said above it's generally not a good idea to let a child control it's parents. As it looks now, you could move the whole eventlisteners to the parent class if you need the menu there?

Thanks very much, _kp! I rethought the whole thing, and ended up reorganizing my classes. Instead of going through the whole backwards mess I was in, I declared a static variable in the tile class and am using that instead.