as3 bubbling problem

7 posts

Flag Post

Hi,

I have following piece of code in a AS3 class (in constructor), which is added to another class as child.

timer1 = new Timer(3000,0);
timer1.start();

var tween1:Tween = new Tween(some code);
tween1.stop();

timer1.addEventListener(TimerEvent.TIMER, startTween);

function startTween(event:TimerEvent):void
{
trace(“tick”);
tween1.start();
}

The problem is, that on each timer event i get trace: tick, tick. So it basically triggers twice. I tried to print in which phase events are, it returns me number 2, which i believe is bubbling phase(in both cases). Also events are not cancelable.

I do understand idea of bubbling, but i don’t understand why events bubble, when default behavior should be no bubbling?

If someone can tell me which lines of code to include and where(parent, child, constructor, event listener…) to disable this unwanted behavour i would be really grateful :)

Cheers

 
Flag Post

Bubbling only applies in the display tree, not to timers. Getting the function called twice implies you’ve attached the handler twice, though it’s hard to see how in a constructor. Do you create two of these objects?

 
Flag Post

Hmm, i only create this object once.

There is chance that i call X.addChild(object with timer) more than once, but that shouldn’t be the problem right, since it doesn’t create new instance of object?

quick example of how i am doing this is:

Face constructor:
-this.addChild(new Eye)

Eye constructor:
-this.addChild(new Class C, new class D)
-here i add timer and event listeners, for animation of the eye

 
Flag Post

Post your original code, not some pseudo code.

 
Flag Post

It is a lot of code… but here it goes (all relevant lines from constructors)


dt(engine) class:

public function dt(){
var cc:CharCutomizationScreen = new CharCutomizationScreen();
stage.addChild(cc);

}
-
CharCutomizationScreen class:

public function CharCutomizationScreen(){

hero = new Hero();
this.addChild(hero);

-
hero class:

public function Hero(){

heroClip = new HeroClip();
this.addChild(heroClip);

}
-
heroclip class:
public function HeroClip(){

face = new Face();
this.addChild(face);

}
-
face class:
public function Face(){

oko = new HMEye();
this.addChild(oko);

}
-
HMEYE class:
public function HMEye(){

timer1 = new Timer(3000,0);
timer1.start();

var tween1:Tween = new Tween(…);
tween1.stop();

timer1.addEventListener(TimerEvent.TIMER, startTween);

function startTween(event:TimerEvent):void
{
trace(“tick”);
tween1.start();
}

this are all references of this class up to the stage. I hope it helps. (there are no other timers anywhere in my code!)

 
Flag Post

any reason you are attaching the event listener after .start()?

Probably not causing a problem, but not good practice.

Probably could remove the timer event listener in startTween too if it’s no longer going to be used, as you’re creating a new timer each time.. Could be something with that.

 
Flag Post

Wrap your code in <pre> tags so we can read it.

public function sample():Void
{
	trace("this");
	trace("is");
	if (true)
	{
		trace("a sample");
	}
|