all groups > flash actionscript > january 2006 >
You're in the

flash actionscript

group:

Script in dynamically generated movie clip?


Script in dynamically generated movie clip? simonkuong
1/1/2006 2:15:19 PM
flash actionscript:
Im make a shopping centre floor plan according to my XML file, and I generated
all the stores as a movie clip in run time, the codes are as following:

function floorplan() {
max = myshop.childNodes[0].childNodes.length;
spacing = 20;

//The following is to generate Stores and to put a number in the box
for (var r=0; r<max; r++) {
name = "shop" + r;
_root.list.attachMovie("shop", name, r);
_root.list[name]._x = r*20;
_root.list[name]._y = 0;
_root.list[name].name_text.text = r;
_root.list[name].name_text.textColor = 0xFFFFFF;

//The following is the generate the pop up boxes above the stores,
//and put the store name in text box:
shopname = myshop.childNodes[0].childNodes[r].attributes.name;
name1 = "frame" + r;
_root.list.attachMovie("frame", name1, r+1*100);
_root.list[name1]._x = r*20;
_root.list[name1]._y = -50;
_root.list[name1].store_name.text = shopname;
_root.list[name1]._alpha = 0;

// This is to make the pop up appear according to mouse roll over on store:
_root.list[name].onRollOver = function () {
temp = _root.list[name].name_text.text;
trace (temp);
temp2 = "frame" + temp;
_root.list[temp2]._alpha = 100;}
trace (shopname);

// The following is for the white box on the left:
_root.list[name].onRollOver = function () {
temp = _root.list[name].name_text.text;
trace (temp);
shopname = myshop.childNodes[0].childNodes[temp].attributes.name;
description = myshop.childNodes[0].childNodes[temp].attributes.url;
_root.board.store_name.text = shopname;
_root.board.store_description.text = description;
}

}

}
myshop = new XML ();
myshop.ignoreWhite = true;
myshop.load ("shops.xml");
myshop.onLoad = function (success) {
floorplan();
}

But I found it impossible to make the onRollOver function working properly
according to the stores you point to. It will only display information from the
last store generated.

Is there a way to assign individual script to the movie clip when it is
generated?

Thanks.
Re: Script in dynamically generated movie clip? Rothrock
1/1/2006 3:07:57 PM
I don't know what the whole temp and temp2 and such is. But your problem is
that the variable isn't evaluated when you assign the handler, but when you do
the onRollOver. At that point everything has been assigned so the name value
that will pull something out of the list is the last one created. Instead do
something like this.

_root.list[name].onRollOver=function(){
this.myStoreName=_root.list[name].name_text.text;
trace(this.myStoreName);
}

For the purpose of explaining the above I call it "Make each clip hold its
own." I assign each clip its own variable/property (they are the same thing
really) so that if I need it for some purpose it is just right there and handy.
And you would get it with:

this.whatEverVariablePropertyIputHere

Or something like that. But as I look at this there are a lot of things that
don't make sense. You can't assign an onRollOver twice. The second assignment
will replace the first, so perhaps that is the problem?

This could be cleaned up a lot. Instead of doing this:

temp2=temp+"something";
list[temp2]=somethingElse;

Do this instead.

list[temp+"something"]=somethingElse;

Re: Script in dynamically generated movie clip? simonkuong
1/1/2006 3:24:33 PM
Thanks Rothrock, its helpful to know the this.storename = _root.list[name].text
approach, but my problem stil remain unsolve :(
Forget about the seoncd onRollOver function, i have deleted it.

Its just that I want the _alpha value to change in the "frame" movieclip
when I mouse over the "shop" movie clip. for example:

Mouse over shop1 : frame1._alpha = 100
Mouse over shop2 : frame2._alpha = 100
...etc
and later I need to do mouse out shop1 : frame1._alpha = 0 as well, but if i
got the first thing working it will be easy.

Im really streched out now.....please help me.
Re: Script in dynamically generated movie clip? kglad
1/1/2006 4:45:34 PM
function floorplan() {
max = myshop.childNodes[0].childNodes.length;
spacing = 20;
//The following is to generate Stores and to put a number in the box
for (var r = 0; r<max; r++) {
name = "shop"+r;
_root.list.attachMovie("shop", name, r);
_root.list[name]._x = r*20;
_root.list[name]._y = 0;
_root.list[name].name_text.text = r;
_root.list[name].name_text.textColor = 0xFFFFFF;
_root.list[name].rvar=r; // <-- this is where you store the variable r so
it can be retrieved later (like onRollOver)
//The following is the generate the pop up boxes above the stores,
//and put the store name in text box:
shopname = myshop.childNodes[0].childNodes[r].attributes.name;
name1 = "frame"+r;
_root.list.attachMovie("frame", name1, r+1*100);
_root.list[name1]._x = r*20;
_root.list[name1]._y = -50;
_root.list[name1].store_name.text = shopname;
_root.list[name1]._alpha = 0;
// This is to make the pop up appear according to mouse roll over on store:
_root.list[name].onRollOver = function() {
_root.list["frame"+this.rvar]._alpha = 100;
};
trace(shopname);
// The following is for the white box on the left:
_root.list[name].onRollOver = function() {
temp = _root.list[name].name_text.text;
trace(temp);
shopname = myshop.childNodes[0].childNodes[temp].attributes.name;
description = myshop.childNodes[0].childNodes[temp].attributes.url;
_root.board.store_name.text = shopname;
_root.board.store_description.text = description;
};
}
}
myshop = new XML();
myshop.ignoreWhite = true;
myshop.load("shops.xml");
myshop.onLoad = function(success) {
floorplan();
};

p.s. you can have more than one rollOver handler and they will all execute.

Re: Script in dynamically generated movie clip? Rothrock
1/1/2006 6:36:51 PM
kglad ? really? I guess I wasn't clear in my post. If you attach another
rollOver to the same clip it will replace the first. Yes, there can be multiple
rollOvers, but they have to be attached to different clips. Or do you know
something I don't? (I know that you know many, many things I don't, but I mean
with regard to this specific issue. :) )

simonkuong ? same trick. You can just give each clip an number. Give each
movie clip a myID property that is its "number" as it were. Then, if you have
named everything with a number at the end you can just use the array notation
to add that number to the end.

So then you can do this.

_root["frame"+this.myID]._alpha=100;

Which I think is what kglad has given you. I just prefer the mnemonic of
"myID" to something like "rvar". But you can call it whatever you want that
helps you remember what it is for.
Re: Script in dynamically generated movie clip? kglad
1/1/2006 8:15:29 PM
Re: Script in dynamically generated movie clip? simonkuong
1/1/2006 10:44:35 PM
Thanks kglad and rothrock, my problem is now solved :) here is the complete
script:


function floorplan() {
max = myshop.childNodes[0].childNodes.length;
spacing = 20;

//The following is to generate Stores and to put a number in the box
for (var r=0; r<max; r++) {
name1 = "shop" + r;
this.shop = _root.list.attachMovie("shop", name1, r);
this.shop._x = r*20;
this.shop._y = 0;
this.shop.name_text.text = r;
this.shop.name_text.textColor = 0xFFFFFF;
this.shop.number = r;
this.shop.name = myshop.childNodes[0].childNodes[r].attributes.name;
this.shop.description = myshop.childNodes[0].childNodes[r].attributes.url;
// This is to make the pop up appear according to mouse roll over on store:
this.shop.onRollOver = function () {
_root.board.store_name.text = this.name;
_root.board.store_description.text = this.description;
_root.board._alpha = 100;
}
this.shop.onRollOut = function () {
_root.board._alpha = 0;
_root.board.store_name.text = "";
_root.board.store_description.text = "";
}
this.shop.onRelease = function () {
maxitem =
myshop.childNodes[0].childNodes[this.number].childNodes[0].childNodes.length;
trace(maxitem);
for (var t=o; t<maxitem; t++){
itemcount = "item" + t + "_name";
this.item =
myshop.childNodes[0].childNodes[this.number].childNodes[0].childNodes[t];
_root.interior[itemcount].text = this.item.attributes.name;
trace (this.item);
}
}
}
}
myshop = new XML ();
myshop.ignoreWhite = true;
myshop.load ("shops.xml");
myshop.onLoad = function (success) {
floorplan();
}
// Mouse folower
speed = 0.1;
setInterval(CursorMovement, 40);
function CursorMovement() {
board._x = speed*(board._x-_xmouse)+_xmouse;
board._y = (speed*(board._y-_ymouse)+_ymouse)-30;
}
board._alpha = 0;


I used a single movie clip to display the information instead of using lots of
them for every store, and I make it follow the mouse and hide automatically. :)
But there is another problem.... its that within the OnRelease function, the
for loop doesnt seems to work... it doest react no matter what i put in there
(even trace).
Thanks.
Re: Script in dynamically generated movie clip? kglad
1/2/2006 1:54:05 AM
maxitem is a string. convert it to a number:

this.shop.onRelease = function () {
maxitem =
myshop.childNodes[0].childNodes[this.number].childNodes[0].childNodes.length;
trace(maxitem);
for (var t=o; t<Number(maxitem); t++){
itemcount = "item" + t + "_name";
this.item =
myshop.childNodes[0].childNodes[this.number].childNodes[0].childNodes[t];
_root.interior[itemcount].text = this.item.attributes.name;
trace (this.item);
}

Re: Script in dynamically generated movie clip? simonkuong
1/2/2006 11:30:02 AM
Actually, I miss typed the number 0 as a character o in the for loop condition....lol
Everything is now working fine :)
Re: Script in dynamically generated movie clip? forumnotifier
1/2/2006 11:30:29 AM
Actually, I miss typed the number 0 as a character o in the for loop condition....lol
Everything is now working fine :)
Re: Script in dynamically generated movie clip? simonkuong
1/2/2006 11:35:32 AM
Actually, I miss typed the number 0 as a character o in the for loop condition....lol
Everything is now working fine :)
AddThis Social Bookmark Button