PDA

View Full Version : Scripted dynamic button problem



mprzybylski
March 18th, 2005, 11:52 PM
my problem is that the button which i name through the loop does not work.


// function that executes gallery commands
function chooseGallery(whichOne) {
var currentGallery = xmlGallery.firstChild.childNodes[whichOne];

// array that holds the galleries
//var galleries = xmlGallery.firstChild.childNodes;

// fill text box with the name of the gallery
var galleryTitle:String = xmlGallery.firstChild.childNodes[whichOne].attributes.title;
gallery_txt.text = galleryTitle;

// make array for images
var images = xmlGallery.firstChild.childNodes[whichOne].childNodes;
for (i = 0; i < images.length; i++) {
var largeIMG = images[i].attributes.image;
var thumbnail = images[i].attributes.thumb;
var imgButton = "image" + i + "_btn";
imgButton.id = i;
imgButton.onRelease = function() {
trace("button clicked");
_root.onEnterFrame = function() {
_root.containerMC.loadPic(xmlGallery.firstChild.ch ildNodes[whichOne].childNodes[2].attributes.image);
};
};
}
}

as you can see the onRelease of imgButton doesn't work (ignore the loadPic i'm going to make that dynamic later) but i cant even get the trace command to come out of it. when i trace imgButton it lists the buttons as they should be, image1_btn, imgae2_btn, and so on, but that doesn't work. anyone have any ideas why? thanks in advance!

CyanBlue
March 19th, 2005, 12:02 AM
Give that the full path like this and see if that works...

var imgButton = _root.someMC["image" + i + "_btn"];

mprzybylski
March 19th, 2005, 01:05 AM
"CyanBlue is a genius!" - the New York Times
"Magnificent Performance by CyanBlue!!!" - Chicago Sun Times
"FIVE STARS" - LA Times

:P thanks a ton

mprzybylski
March 19th, 2005, 05:06 PM
ok now i have a problem that i can't catch the id of each button. here is my new code:


// function that executes gallery commands
function chooseGallery(whichOne) {
var currentGallery = xmlGallery.firstChild.childNodes[whichOne];

// array that holds the galleries
//var galleries = xmlGallery.firstChild.childNodes;

// fill text box with the name of the gallery
var galleryTitle:String = xmlGallery.firstChild.childNodes[whichOne].attributes.title;
gallery_txt.text = galleryTitle;

// make array for images
var images = xmlGallery.firstChild.childNodes[whichOne].childNodes;
for (i = 0; i < images.length; i++) {
var largeIMG = images[i].attributes.image;
var thumbnail = images[i].attributes.thumb;
var imgButton = _root["image" + i + "_btn"];
_root[imgButton].id = i;
imgButton.onRelease = function() {
_root.onEnterFrame = function() {
_root.containerMC.loadPic(xmlGallery.firstChild.ch ildNodes[whichOne].childNodes[_root[imgButton].id].attributes.image);
};
};
}
}

as you can see i created an id property for imgButton when traced outputs 0, 1, 2, 3, 4. however, when i try to use it in my loadPic function, it only loads up the last picture, 4, for all teh buttons. i always have trouble trapping the numbers in the loop so i can use them, and its happening again here. anyone have any ideas on how i can trap the number on each current button to use the same value of i to load up that corresponding xml image? if i hardcode this, like put a 2 in the xml part of loadPic it loads up image 2 for every button, etc.

MichaelxxOA
March 19th, 2005, 05:08 PM
that's because you're variable imgButton, is already _root["image"+i+"_btn"];

so all you need is

imgButton.id = i;

-Michael

mprzybylski
March 19th, 2005, 05:11 PM
i tried that, but then what do i put in the xml part, cuz i tried various things and i couldnt get it to work still.

MichaelxxOA
March 19th, 2005, 05:13 PM
did you change both of your _root[imgButton], to just imgButton?
-Michael

mprzybylski
March 19th, 2005, 05:27 PM
yeah, i feel like i have tried everything, i tried imgButton.id = i and in the xml i tried imgButton.id, _root.imgButton.id, imgButton[this.id] etc

MichaelxxOA
March 19th, 2005, 05:29 PM
this might be dumb, but did you notice that in the word childNodes you have to spaces so its like ch ld, like i said it might be so stupid but it might be what's wrong.

mprzybylski
March 19th, 2005, 05:39 PM
im not sure why but thats how it pastes in here, its not really like that in the flash file.

MichaelxxOA
March 19th, 2005, 05:41 PM
what is the instance names of your four buttons?

scotty
March 19th, 2005, 05:41 PM
for (i=0; i<images.length; i++) {
var largeIMG = images[i].attributes.image;
var thumbnail = images[i].attributes.thumb;
var imgButton = _root["image"+i+"_btn"];
imgButton.id = i;
imgButton.onRelease = function() {
_root.containerMC.loadPic(xmlGallery.firstChild.ch ildNodes[whichOne].childNodes[this.id].attributes.image);
};
}
?

scotty(-:

mprzybylski
March 19th, 2005, 05:46 PM
scotty wins once again. last time i tested it without the onEnterFrame it wasnt working, but that was before i made a bunch of changes, but taking it out seems to work, thanks again scotty.

and thanks michael for all your help as well.

scotty
March 19th, 2005, 05:47 PM
welcome=)

mprzybylski
March 19th, 2005, 07:51 PM
here is a question for you guys. this is more so for my own personal knowledge, but i have an xml file as follows:


<portfolio>
<gallery title="PEOPLE">
<picture thumb="images/gamer_thumb.jpg" image="galleries/people/guy_desk.jpg"/>
<picture thumb="images/pic1_thumb.jpg" image="galleries/people/gq.jpg"/>
<picture thumb="images/pic2_thumb.jpg" image="galleries/people/stilts.jpg"/>
<picture thumb="images/pic3_thumb.jpg" image="galleries/people/girl.jpg"/>
</gallery>
<gallery title="JESSICA">
<picture thumb="images/2gamer_thumb.jpg" image="galleries/jessica/jessica1.jpg"/>
<picture thumb="images/pic1_thumb.jpg" image="galleries/jessica/jessica2.jpg"/>
<picture thumb="images/pic2_thumb.jpg" image="galleries/jessica/jessica3.jpg"/>
</gallery>
<gallery title="RANDOM">
<picture thumb="images/3gamer_thumb.jpg" image="galleries/random/preloader.jpg"/>
<picture thumb="images/pic1_thumb.jpg" image="galleries/random/roscoes.jpg"/>
<picture thumb="images/pic2_thumb.jpg" image="galleries/random/school.jpg"/>
<picture thumb="images/pic3_thumb.jpg" image="galleries/random/scarface.jpg"/>
<picture thumb="images/pic4_thumb.jpg" image="galleries/random/shirt.jpg"/>
<picture thumb="images/pic4_thumb.jpg" image="galleries/random/sis.jpg"/>
</gallery>
</portfolio>

now im testing what i will be using eventually in a project where i have five buttons on the stage and 3 buttons to pick between the galleries. when i load up a gallery through the buttons, i use that function up there that i had in the other post. but when i load up the second gallery which only has 3 pics in it, the other two buttons (still visible for testing purposes) load the fourth image from the previous gallery (if it had four or more images) and i dont know how to "clear" the xml that was used before so that only the 3 buttons load the 3 corresponding images rather than the buttons that arent technically part of the current xml part that im using load the leftover images from the other xml part. does that make sense?

CyanBlue
March 19th, 2005, 08:07 PM
I know this totally depends on how your movie is set up, but how about deleting that movieClip that you have on the stage when you need to display the different category???

Something like this in that function would do the trick...

_root.containerMC.removeMovieClip();
_root.createEmptyMovieClip("containerMC", 1234);

mprzybylski
March 19th, 2005, 09:02 PM
well that movie clip is not created dynamically, its actually on the stage because it has a white box in it that resizes based on the image thats loaded into it. but even if i did do that, would that clear the xml?

CyanBlue
March 19th, 2005, 09:34 PM
The answer would be NO... But I do not think that has to do with the XML not being cleared... (I could be totally wrong without seeing what you have there...)

Anyways... This variable, images, looks like the one that's holding the value from the XML with given 'whichOne' variable...
You already have 'var' in front, so that means that the var variable will be recreated everytime you execute that 'chooseGallery' function, but try adding this line before that 'var images =' line to see if it makes any difference... If not, please post some sample file that we can play with for better understanding... :)

images.length = 0;

mprzybylski
March 20th, 2005, 04:09 AM
nope, that didnt work.

http://www.reintroducing.com/apnw/img_xml_test.zip

i've included the gallery files so you can test them with easy, along with the fla and xml. thanks for your help.

scotty
March 20th, 2005, 04:18 AM
Remove them (if you create them dynamically) or in your case disable the buttons that you're not using:)

var curLength;
function chooseGallery(whichOne) {
for (var j = 0; j<curLength; j++) {
this["image"+i+"_btn"].enabled = 0;
}
var currentGallery = xmlGallery.firstChild.childNodes[whichOne];
var galleryTitle:String = xmlGallery.firstChild.childNodes[whichOne].attributes.title;
gallery_txt.text = galleryTitle;
var images = xmlGallery.firstChild.childNodes[whichOne].childNodes;
curLength = images.length;
for (var i = 0; i<images.length; i++) {
var largeIMG = images[i].attributes.image;
var thumbnail = images[i].attributes.thumb;
var imgButton = this["image"+i+"_btn"];
imgButton.id = i;
imgButton.enabled = 1;
imgButton.onRelease = function() {
_root.containerMC.loadPic(xmlGallery.firstChild.ch ildNodes[whichOne].childNodes[this.id].attributes.image);
};
}
}

scotty(-:

mprzybylski
March 20th, 2005, 11:18 AM
that didnt work scotty, it only made 3 buttons work for every gallery.

scotty
March 20th, 2005, 11:27 AM
My bad, I had an i i/o a j ....
correct line

this["image"+j+"_btn"].enabled = 0;

scotty(-:

mprzybylski
March 20th, 2005, 11:38 AM
doesnt work either, the buttons are disabled when u click on another gallery.

scotty
March 20th, 2005, 11:48 AM
works for me though...:)
On gallery 1>>4 buttons enabled
On gallery 2>>3 buttons enabled
On gallery 3>>5 buttons enabled

scotty(-:

mprzybylski
March 20th, 2005, 11:51 AM
is this the code you used?


var curLength;

// function that executes gallery commands
function chooseGallery(whichOne:Number) {
for (var j = 0; j < curLength; j++) {
this["image" + j + "_btn"].enabled = 0;
}
var currentGallery = xmlGallery.firstChild.childNodes[whichOne];

// array that holds the galleries
//var galleries = xmlGallery.firstChild.childNodes;

// fill text box with the name of the gallery
var galleryTitle:String = xmlGallery.firstChild.childNodes[whichOne].attributes.title;
gallery_txt.text = galleryTitle;

// make array for images
var images = xmlGallery.firstChild.childNodes[whichOne].childNodes;
curLength = images.length;
for (i = 0; i < images.length; i++) {
var largeIMG = images[i].attributes.image;
var thumbnail = images[i].attributes.thumb;
var imgButton = _root["image" + i + "_btn"];
imgButton.id = i;
imgButton.onRelease = function() {
_root.containerMC.loadPic(xmlGallery.firstChild.ch ildNodes[whichOne].childNodes[this.id].attributes.image);
};
}
}

mprzybylski
March 20th, 2005, 11:55 AM
what the, i just pasted ur code and it does work, but mine doesnt, and i cant find a difference...

mprzybylski
March 20th, 2005, 11:58 AM
oh wow i'm dumb, i didn't see the imgButton.enabled = 1, thanks scotty :P

mprzybylski
March 20th, 2005, 11:59 AM
ok here is a little bug, if you click on gallery 2 and try to click image 5 its clickable but it doesnt do anything, then go to gallery 3 and theya ll work, then go back to gallery 2 and 5 isnt clickable anymore, so its like the first time 2 is loaded 5 works....

CyanBlue
March 20th, 2005, 12:32 PM
I guess I got up too late... :D

This is what I have... I got rid of all those buttons at the bottom... Created another movieClip(MC_Button) which has square and textField(num_txt) and dynamically attaching the button and set the maxLength according to the number of images you have... and next time when you run the function again, the for loop will delete all the buttons on the stage and recreate them according to the new image number...

// function that executes gallery commands
function chooseGallery(whichOne:Number) {
for (var i = 0 ; i < _root.maxLength ; i++)
{
_root["image" + i + "_btn"].removeMovieClip();
}

var currentGallery = xmlGallery.firstChild.childNodes[whichOne];

// array that holds the galleries
//var galleries = xmlGallery.firstChild.childNodes;

// fill text box with the name of the gallery
var galleryTitle:String = xmlGallery.firstChild.childNodes[whichOne].attributes.title;
gallery_txt.text = galleryTitle;

// make array for images
var images = xmlGallery.firstChild.childNodes[whichOne].childNodes;

for (i = 0; i < images.length; i++) {
var largeIMG = images[i].attributes.image;
var thumbnail = images[i].attributes.thumb;
var imgButton = _root.attachMovie("MC_Button", "image" + i + "_btn", 10000 + i, {_x:260 + i * 40, _y:350});
imgButton.num_txt.text = i + 1;
imgButton.id = i;
imgButton.onRelease = function() {
_root.containerMC.loadPic(xmlGallery.firstChild.ch ildNodes[whichOne].childNodes[this.id].attributes.image);
};
}
_root.maxLength = i;
}

scotty
March 20th, 2005, 12:32 PM
In the first run curLength is undefined so all 5 buttons work even if they don't "have a picture. If you define curLength like the following

//set curLength to the number of buttons you have
var curLength=5;
in the for loop all buttons will get disabled and later just the buttons you need get enabled:)

scotty(-:

mprzybylski
March 20th, 2005, 02:19 PM
cyan, thats very cool, i'm going to be converting this soon to use attached movie clips actually, so thanks for that. do you post on the devshed forums? i think you helped me on there before, or maybe it was on here, i'm not sure...

scotty, thanks to you as well, always helpful.

scotty
March 20th, 2005, 02:28 PM
I think it's better way to do it as CyanBlue has showed you:)
Thanks for the thank you;)

scotty(-:

CyanBlue
March 20th, 2005, 03:27 PM
I've been programming more than enough and one thing that I know for sure is that we don't really care as long as it does the job... :D

Devshed... I sure did lurk around at the forum once or twice, but things started to become hectic at work, so I basically come to AS.org and Kirupa(mainly server side scripting forum only though)... No other forums no more... I go to WH Only when I have some free time after I do routine visit to other forums... ;)

mprzybylski
March 20th, 2005, 04:26 PM
i thought i saw your name there. in any case, i appreciate the help guys, thanks a ton.