Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with eventHandler for object instantiated inside another objec

Status
Not open for further replies.

CorrieLee

Programmer
Oct 13, 2004
18
CA
Hi there,

In my application I have a container object (ContainerSquares) that has a number of smaller objects (Square). I create an instance of the container class on the main timeline, and create the instances of the squares inside of the container class. The Square object is a movieClip in the library that is linked to the Square class.

I can create these movieclips fine, but when I try to code an onRelease for them, I can't seem to reference any functions outside of the onRelease function. If I put a trace directly inside of the onRelease I can see it, but if I put a function inside the container class with a trace, and call that from the onRelease function, I can't see that (I've tried referencing it with every syntax I could think of (_root.container.function, this.cParent.function [cParent is a var in Square, it references the parent container]), I've also tried putting the function to call in the Square class, and the main timeline, and I can't seem to call any other function belonging to any of them (main movie, Container, or Square) inside of this onRelease.

The other weird thing is that I can perform the actions directly inside of the onRelease handler. I can check what frame that instance of a mcSquare is on, and tell it to gotoAndPlay("something")... but I have a few of them, and it seems ridiculous to code the same thing directly into each onRelease.
This works:
Code:
this.mcSquare1.onRelease = function(){
	trace("in onRelease");
	if(this._currentframe > 19){
		this.gotoAndPlay("next");
	}else if (this._currentframe > 9){
		this.gotoAndPlay("big");
	}	else if(this._currentframe > 0){
			this.gotoAndPlay("next");
	}
}

This doesn't work:
Code:
this.mcSquare3.onPress = function()
{this.otherFunction(mcSquare3);}

Regardless of where I put 'otherFunction' or how I reference it. It's maddening.

I suppose I could use a for loop and go through an array of these objects and still write the code just once, but I'd REALLY like to know how to call another function from inside the onRelease function, and why it's not working!

Sorry for the long rambling post! Thanks!

c.
 
you might find it easier to add the on release when the clips are added

if i understand your question then something along the lines of

Code:
for (i=0; i<somenumber; i++) {
  _root.container.attachMovie("square", "square"+i, i);
  _root.container["square"+i].onRelease = function (){
//some code
}
}
 
I just tried this, and it still didn't work... it's still not seeing any functions outside of the onRelease function.

Any other suggestions?

c.
 
Thanks, this does work really well when it's on the main timeline, but it still doesn't work when I have the onRelease being written inside of a class. I'll try to be more clear about what is happenning, maybe I'll post the code tomorrow (it's at work).

The Square class is linked to a movie clip, the ContainerSquares class isn't, it's just... a means of controlling the movie clips. So, on the main timeline, I create an instance of a container:ContainerSquares. Inside of the ContainerSquares class I have a function that is just fMakeBoards that passes in the main movie as the target, and attachs instances of this mcSquare (that's just in the library) to the target.

For some reason, because I'm doing this inside of a function inside of this other class, it won't ever call any other functions. I've tried calling functions written in the ContainerSquares class, in the Square class (in case it was just seeing itself), on the main timeline...

I hope this makes more sense... it's late and I'm frustrated and rambling. ;)

c.



 
Ok, here's some of the code.

This is (some of) the Square class:
Code:
class Square extends MovieClip
{
	var cParent:ContainerSquares;
	
	function Square()
	{}
	
	function setParent(tParent:ContainerSquares):Void
	{
		cParent = tParent;
	}
}

This is (some of) the ContainerSquares class:
Code:
class ContainerSquares {
	
	//array of small squares.
	var smallSquares_array:Array = new Array(); 
	var numOfSquares:Number;
	var mcTarget:MovieClip
	var mcSquare1:MovieClip;
	var mcSquare2:MovieClip;
	var mcSquare3:MovieClip;
	var pos1x:Number;
	var pos2x:Number;
	var pos3x:Number;
	var posY:Number;
	var boardDepth:Number;

function ContainerSquares (target:MovieClip, boardDepth:Number) {
    this.mcTarget = target;
    this.boardDepth = boardDepth;
    makeBoards();
}

//create the boards
private function makeBoards():Void {
	//hardcoded for testing.
      numOfBoards = 3;
      this.pos1x = 125;
	this.pos2x = 245;
	this.pos3x = 365;
	this.posY = 214; 
//create the squares - mcSquare is linked to Square class	
	for (var i = 1; i < numOfBoards+1; i++){
   		this["mcSquare" + i] = mcTarget.attachMovie("mcSquare","mcSquare", boardDepth +i);
		this["mcSquare" + i]._x = this["pos"+i+"x"];
		this["mcSquare" + i]._y = this.posY;
		this["mcSquare" + i].onRelease = function(){this.fng(["mcSquare"+i]);}
		//fill the array of squares
		smallBoards_array[i-1] = this["mcSquare"+i];
	}		
//set this container as the parent of all squares in array
	this.setSquares();
}

function pressHandler():Void{
	trace (" in pressHandler ");
		if(this._currentframe > 19){
			this.gotoAndPlay("next");
		}else if (this._currentframe > 9){
			this.gotoAndPlay("big");
		}	else if(this._currentframe > 0){
				this.gotoAndPlay("next");
		}
	
}

function setSquares(){
	for (var i =0; i < smallBoards_array.length; i++){
		smallSquares_array[i].setParent(this);
}

main timeline:
Code:
var container:ContainerSquares ;
//in initialization function
container = new ContainerSquares (this, 0);

Ok, so... the code for the onRelease in the makeBoards function, if I put a trace directly in there, I can see it, so I know that event is firing. But, I tried putting the pressHandler function inside of the Square class, the ContainerSquares class, the main timeline. I've tried calling it in different ways (from nothing in front of it to calling the object at _root). I can't get it to work.

Thanks for looking at this! :)

Corrie.
 
I think I figured it partly out...

If I code the onRelease directly in the square object and don't try to set it at all in the container class, it'll fire and catch, and I can call a method in the container class from there...

Still doesn't explain why I can't see anything outside of the function when it's being set in the container class, but... oh well. It works and I've got deadlines. ;)

Thanks again.

c
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top