Groups | Blog | Home
all groups > flash actionscript > january 2004 >

flash actionscript : Dynamic movieclip event handling


Chris Brandt
1/27/2004 11:19:29 PM
Hi all,

I am trying to do something that I Is there a way to assign a onPress handler to a movieclip which is created dynamically (ie on a buttonpress)?

I have a small mc symbol (let's call it HR - for Heart Rate). When the user clicks on the mc, it creates a new instance (either with duplicateMovieClip or attachMovieClip), and is draggable. The user moves over to the area that they want it to go and click the mouse again. Now it's set.

They repeat the process.

Now, they realize that they really wanted the first one to be somewhere else, so they click on it, it becomes draggable again, they move to where they want it, then click to place it there.

Now... the problem... I can't seem to make this happen. :(

I can dynamically create the new mc's just fine, but can't seem to attach any kind of handler to them. I can't 'pick them up again' to allow modification of their position.

I think that part of the problem is that I can't seem to create a handler for a mc instance that isn't created IMMEDATELY at runtime...

for example:

_root.attachMovie("LinkResp", "newclip_mc", 10);

newclip_mc.onPress = function () {
trace('clicked the newclip');
}


Works just fine...

But i want to be able to control movieclips that I add DURING the movie...


myButton.onPress = function() {
_root.attachMovie("LinkResp", "newclip_mc", 10);
}

newclip_mc.onPress = function () {
trace('clicked the newclip');
}


Doesn't do squat. Any ideas?

jerome NO[at]SPAM STC
1/28/2004 4:05:22 AM
Hello.

Here's where the issue is: You define an event for a clip that does not yet exist. Only when you press the button does newclip_mc exist. So you onPress assignment should come after the attachMovie.

So your code should be

myButton.onPress = function() {
_root.attachMovie("LinkResp", "newclip_mc", 10);
newclip_mc.onMouseDown = function () {
trace('clicked the newclip');
}
}

You can use onPress or onMouseDown...

Cheers

check out the STC fontBrowser

STC Associates
AddThis Social Bookmark Button