Groups | Blog | Home
all groups > flash actionscript > september 2007 >

flash actionscript : duplicateMovieClip


Don97
9/14/2007 11:50:11 PM
after successfully creating six movieclips, each holds a different image,
within a loop structure that calls duplicateMovieClip,
i am unable to use these movie clips as a button, ie my_mc.onRelease =
function(){}.. does not work, yet debug shows that the
clips exists and i use the name that debug shows and i can see them..to keep
this message short, i have attached pertintent code..
thanks Don97

1.) i created a dummy movie clip in lib and place an instance named "button"
on the stage,
but not viewable..
2.) then i make six duplicates using your syntax..
function showThumb(n:Number):MovieClip{
button.duplicateMovieClip("button"+n,100+n);
if(n<=4){
_root["button"+n]._x=80 * n;
_root["button"+n]._y = 345;
}else{
_root["button"+n]._x = 80 * (n-5);
_root["button"+n]._y = 410;
}
_root["button"+n].loadMovie(thumbImage[n]);
return _root["button"+n];
} //this code works in
that it produces
the six thumbnails
with images and
properly located

3.) then i call the function, which returns a movieclip..
for (j=0;j<total;j++){
thmAr[j] = showThumb(j);
trace("thmAr["+j+"] = "+thmAr[j]);
}

the trace, which is OUTSIDE of the showthumb() reveals
thmAr[0] = _level0.button0
thmAr[1] = _level0.button1
thmAr[2] = _level0.button2
thmAr[3] = _level0.button3
thmAr[4] = _level0.button4
thmAr[5] = _level0.button5
thmAr[6] = _level0.button6

4.) but when i try to use mc as button
thmAr[0].onRelease = function(){
trace("Button 0 Released");}

or

_level0.button6.onRelease = function(){
trace("Button 6 Released");}

No LUCK ... I am perplexed?? Don97
kglad
9/15/2007 1:10:47 AM
code attached to a movieclip is lost when that movieclip is the target of a
loadMovie() or loadClip() method. to remedy, define your mouse handlers after
loading is complete or load into a child of the movieclip that has mouse
handlers.
Don97
9/15/2007 3:34:31 PM
thank you for responding, i removed the loadMovie but the resultant duplicate
movieclips still do not respond as buttons when created with
duplicateMovieClip, actually same thing happens if i use CreateMovieClip..
Don97
kglad
9/15/2007 3:49:58 PM
Don97
9/15/2007 9:25:41 PM
function loadXML(loaded) {
if (loaded) {
xmlNode = this.firstChild;
image = [];
url = [];
thumbImage = [];
thmAr = [];
total = xmlNode.childNodes.length;
for (i=0; i<total; i++) {
image[i] = xmlNode.childNodes[i].childNodes[0].firstChild.nodeValue;
url[i] = xmlNode.childNodes[i].childNodes[1].firstChild.nodeValue;
thumbImage[i] = xmlNode.childNodes[i].childNodes[2].firstChild.nodeValue;
}

firstImage();

for (j=0;j<total;j++){
thmAr[j] = showThumb(j);
trace("thmAr["+j+"] = "+thmAr[j]);
}
} else {
content = "file not loaded!";
}
}

xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("images.xml");


/////////////////////////////////////
listen = new Object();
listen.onKeyDown = function() {
if (Key.getCode() == Key.LEFT) {
prevImage();
} else if (Key.getCode() == Key.RIGHT) {
nextImage();
}
};
Key.addListener(listen);
previous_btn.onRelease = function() {
prevImage();
};
next_btn.onRelease = function() {
nextImage();
};
/////////////////////////////////////
p = 0;
this.onEnterFrame = function() {
filesize = picture.getBytesTotal();
loaded = picture.getBytesLoaded();
preloader._visible = true;
if (loaded != filesize) {
preloader.preload_bar._xscale = 100*loaded/filesize;
} else {
preloader._visible = false;
if (picture._alpha<100) {
picture._alpha += 7;
}
}
};
var intervalCounter:Number = setInterval(nextImage,3000);
function nextImage() {
if (p<(total-1)) {
p++;
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[p], 1);
//desc_txt.text = url[p];
picture_num();
}
}else{
p=0;
firstImage();
}
}
function prevImage() {
if (p>0) {
p--;
picture._alpha = 0;
picture.loadMovie(image[p], 1);
//desc_txt.text = url[p];
picture_num();
}
}
function firstImage() {
if (loaded == filesize) {
picture._alpha = 0;
picture.loadMovie(image[0], 1);
//desc_txt.text = url[0];
picture_num();
}
}
function picture_num() {
current_pos = p+1;
pos_txt.text = current_pos+" / "+total;
}
function showThumb(n:Number):MovieClip{
button.duplicateMovieClip("button"+n,100+n);

if(n<=4){
_root["button"+n]._x=80 * n;
_root["button"+n]._y = 345;
}else{
_root["button"+n]._x = 80 * (n-5);
_root["button"+n]._y = 410;
}
// recommended suggestion from kglad
////////////////////////////////////////////////
// _root["button"+n].loadMovie(thumbImage[n]);//
////////////////////////////////////////////////
return _root["button"+n];
}

//use thumbnail moveclips as a button
thmAr[0].onRelease = function(){
trace("Button 0 Released");}
_root._level0.button6.onRelease = function(){
trace("Button 6 Released");}
n=1;
_root["button"+n].onRelease = function(){
trace("Button 1 release");}
kglad
9/15/2007 9:38:13 PM
Don97
9/16/2007 8:17:36 PM
problem solved..
The problem was not letting the dup function complete before adding the event
handlers.

I just extended the time line to 12 frames, probably more than enough, then
added the event handlers in the frame script..
who would have 'thunk it'.. i should probably test for "something" before
adding event handlers..
don
AddThis Social Bookmark Button