PDA

View Full Version : can't assign onPress event to dynamically created mc (?)



j0se
September 15th, 2003, 12:30 PM
hi all,

here's my code

createEmptyMovieClip("myMC",0);
// load a jpg
myMC.loadMovie("myImg.jpg")

// assign onPress event
myMC.onPress = function(){
trace("hi");
}

this ain't happening
:(

anyone know the correct way of doing it?

cheers

Johnny64
September 15th, 2003, 12:50 PM
Hoi

when you import a image into the library in flash it self. Then you drag the image from the library to the stage. Select it and you will see its not a MovieClip! Wall the same is your AS then you create a MC It is a MC but the second you load an image in to it, it will be like if you draged image in from the lidrary. And an onPress event is a MC event but it is no langer an mc ;)

Workround would been to make 2 mc's like this



createEmptyMovieClip("myImgMC",0);
myImgMC.createEmptyMovieClip("myImgHolder",0);
// load a jpg
myImgMC.myImgHolder.loadMovie("myImg.jpg")


// assign onPress event
myImgMC.onPress = function(){
trace("hi");
}


;)

claudio
September 15th, 2003, 12:52 PM
Trythis.createEmptyMovieClip("myMC", 0);
myMC.loadMovie("myImg.jpg");
this.onEnterFrame = function() {
myMC.onPress = function() {
trace("hi");
};
};

senocular
September 15th, 2003, 12:58 PM
what happens is, is that it takes some time to load the image into the movieclip. When you load anything using loadMovie into a movieclip, beit a swf or a jpg, the movieclip will clear out all its variables and functions 'refreshing' the movieclip to be a 'clean' movieclip. Id does this at the point of loading where the loaded swf or jpg is recognizable to the movieclip and this isnt instantly after the loadMovie command. It may take a couple of frames. Master64's solution is a good one. Otherwise, you would need some sort of loop, either based in an onEnterFrame event or using setInterval to check for the loading status of the movieclip - if ok to add new properties and functions, then add the onPress, otherwise, keep looping until it is ok. Checking for that can be just a matter of saying if (myMc) { ... something like claudio's example, though you may not want the onEnterFrame always assigning the event ;) (of course if it works, dont worry about it :))

Johnny64
September 15th, 2003, 01:02 PM
:h: i heet it whan am wrong :a:

well i still think it's easyer to just make 2 mc :P



;)

claudio
September 15th, 2003, 01:05 PM
Sen, what about deleting the onEnterFrame handler after the onPress even has been assigned?this.createEmptyMovieClip("myMC", 0);
myMC.loadMovie("myImg.jpg");
this.onEnterFrame = function() {
if (myMC._width>0) {
myMC.onPress = function() {
trace("hi");
};
delete this.onEnterFrame;
}
};

j0se
September 15th, 2003, 02:00 PM
this gives me lots to work on!! cheers!

Sen: bizarrely enough i came across one of your own fla's today inmy quest for answers (the XML Driven Portfolio) which does more or less what i'm trying to get mine to do (and much more)

i see those thumbs are being loaded dynamically (i took a peep in the xml file) and they have onPress events, which is what mine

i'll try the above first, then find your app again if i get any probs :nerd:

thanks very much chaps!

senocular
September 15th, 2003, 02:15 PM
Originally posted by claudio
Sen, what about deleting the onEnterFrame handler after the onPress even has been assigned?

yeah that works great :) the only problem there is having that this.onEnterFrame available. Is it being used by some other process? is it ok to assign? If you load more than one, you'll have to be sure another isnt using the onEnterFrame or that you put both onPress assignments in a single onEnterFrame. Usualy its not a big deal as long as you keep it organized. But yeah, for all events and puposes for this example, that should work like a charm :beam:

claudio
September 15th, 2003, 02:29 PM
Yea, thanks for the tips sen.
:)

and welcome j0se ;)

j0se
September 16th, 2003, 07:09 AM
Hi again,

and thanks again Claudio! :azn:

ok, i hope you guys don't mind but i'd like to ask a bit more about this example

i've attached my fla - i've used Claudio's code (without the delete onEnterFrame code, which i don't really understand)

i'm trying to attach the onPress event to each mc inside the loop, but only the last mc is getting it
(i can see why, doing a trace - the last movie loaded is flagged as the current movie, which I think is what Sen was talking about 'not loading quicker than the code runs'

cheers for all your help with this!
:)

j0se
September 16th, 2003, 09:33 AM
I also tried creating an array to store the individual paths of each mc inside the loop, to see if i could assign the events on a button click, using the stored paths, but still only the last mc loaded works

so basically, if i load more than 1 mc and try to assign an event to any mc other than the last one, it fails :(

Johnny64
September 16th, 2003, 03:28 PM
Hoi again

well i still think this is easyer


for (i=0; i<5; i++) {
currentMC = main_mc.createEmptyMovieClip("mc"+i, i);
currentMC._x = 110*i;
currentMC.createEmptyMovieClip("mcholder"+i, i);
currentMC["mcholder"+i].loadMovie("img"+i+".jpg");
currentMC.onPress = function() {
trace("I am mc"+(i-1)+"!");
};
}


the reason why this dosen't work


for (i=1; i<5; i++) {
currentMC = main_mc.createEmptyMovieClip("mc"+i, i);
currentMC.loadMovie("img"+i+".jpg");
currentMC._x = 110*i;
this.onEnterFrame = function() {
currentMC.onPress = function() {
trace("I am mc"+(i-1)+"!");
};
};
}

is.... well lots of reasons
1) the
this.onEnterFrame = function() {
is being overwriten evertime the for loop loops so only the last this.onEnterFrame is used.
2) and even if it didn't over write the onEnterFrame, the currentMC will be because the for loop loops 5 time very fast before the play head can enter the next frame. so the currentMC is the last mc you made and then when the onEnterFrame run it will make 5 onPress of one mc!?!

so can you see the prob you get with....time :}
i hope am right this time :sigh:

j0se
September 17th, 2003, 06:26 PM
hey Master64! cheers for that reply!

i haven't had chance to try your code - but i will soon - of course, it will work! :) so thank you very much!

i'm still a llittle loose on the whole concept though. :(

something you said there makes sense: "the for loop loops 5 time very fast before the play head can enter the next frame". That ties in with what Senocular explained earlier about 'timing'.

but what i don't get is this:

in your code you are making 1 extra mc to go inside each mc that i made to go inside the first movie: _root.main_mc (so in my version main_mc was the holding mc for all the little mcs named mc + i, that loaded a jpg each) But now the little mc's are holding other mc's inside them to!!!

hmm. so how does that solve the problem? how can that give the plahead time to catch up with the loop (so to speak)? if the onPress event is on the enterFrame of an mc buried deep inside other mc's, surely the playhead has to 'travel' that much deeper (and longer) to get there?

if the root of the prob is speed, wouldn't it be more accurate to set a delay before you assign the events? something like:

1 // createEmptyMovieClip (holding mc)
2 // loop through images to load and create a new mc inside the holding mc for each img
3 // set delay (gives playhead time to catch up)
4 // add event to current mc

that way you're addressing the problem directly, and not working around it

the answer is most likely a severe lack of coffee :eye: