Is there an event probelm in actionscript?

Hi community

I am currently trying to set up a small piece of software in flash mx. Having experience in OOP / javascript, I stumble over some basic problems, but I think it’s not because of actionscript but because of my ignorance. Thus, any help is highly appreciated.

Here’s the simple problem.

Imagine some simple code like this:

// start code

this.createEmptyMovieClip(“firstClip”,1);
with (this.firstClip) {
beginFill(0x000000,100);
moveTo(10,10);
lineTo(10,50);
lineTo(50,50);
lineTo(50,10);
endFill();
}

this.firstClip.createEmptyMovieClip(“secondClip”,2);
with (this.firstClip.secondClip) {
beginFill(0x333333,100);
moveTo(20,20);
lineTo(20,170);
lineTo(170,170);
lineTo(170,20);
endFill();
}

this.firstClip.onPress = function() {
trace(“clicked first”);
}

this.firstClip.secondClip.onPress = function() {
trace(“clicked second”);
}

//end code

I would expect that If I click on the secondClip (in gray), the eventhandler on this clip would react and trace “clicked second”. However, I only get “clicked first”, even though I clicked on the second one.

How can I change this? I want the “clicked first” message to appear only when I clicked on the first clip, and the “clicked second” message only when I click on the second.

Attaching both of the clips on the root is not an option. Later, I want to make the the whole package of attached clips draggable, that’s why I attach one to the other.

Thanks a lot for your help!

sala

it’s the nesting that’s causing the problem. since secondClip is part of firstClip, the press event never makes it down the hierarchy.

if you want to avoid putting them in _root, perhaps putting them both in a third clip, and not nested in each other, would solve some problems.


this.createEmptyMovieClip("zeroClip",0);
this.zeroClip.createEmptyMovieClip("firstClip",0);
with (this.zeroClip.firstClip) {
   beginFill(0x000000,100);
   moveTo(10,10);
   lineTo(10,50);
   lineTo(50,50);
   lineTo(50,10);
   endFill();
}

this.zeroClip.createEmptyMovieClip("secondClip",1);
with (this.zeroClip.secondClip) {
   beginFill(0x333333,100);
   moveTo(20,20);
   lineTo(20,170);
   lineTo(170,170);
   lineTo(170,20); 
   endFill();
}

this.zeroClip.firstClip.onPress = function() {
trace("clicked first");
}

this.zeroClip.secondClip.onPress = function() {
trace("clicked second");
}

you’re right, that is a way to solve my problem. Thanks for the hint!

However, I am still a bit confused about this whole thing. It basically means that actionscript is not able to tell on which instance of a moveClip an event was fired (if the movieClips are nested).

If that is the case, then I think this is rather a huge flaw in actionscript. Any GUI is highly depending on such a mechanism. Hope this gets fixed in a new version…:-\

not all events behave like onPress.

onPress effectively makes the movie act as a button, which masks press events for nested buttons. unless you use the ‘track as a menu’ switch to modify its behaviour.

an onMouseUp event will fire for all clips regardless of their nesting, but also regardless of the mouse position. combined with a hitTest you can emulate the onPress action but without the nesting problems.

or you can structure your movie such that onPress clips are not nested in other onPress clips.