PDA

View Full Version : naming dynamic created movieclips and targeting them in another movieclip on stage


bas1c
09-17-2004, 09:40 PM
Ive searched the forum and couldnt find anything on this.. sorry if it is out there somewhere.

Im trying to dynamically make a list of "buttons" that change the color of a MC on stage. The movieclips is made in another movieclip on stage.

My problem is that i cant name the movieclips dynamically and targeting them dynamically.

Anyone got a solution ?


Here is my code:

// string from php
var monsterfarvevars:String = "20FF00~2FFFFF~200000~2FF000~200FFF~20F0F0"
// splitting it up into an array
var Monstertabel:Array = monsterfarvevars.split("~");
var x:Number = 0;

// function to fill a movieclip with a 10 * 10 pixel square with a certain color.
MovieClip.prototype.square = function(x, y, clor) {
with (this) {
beginFill(clor);
moveTo(x, y);
lineTo(x+10, y);
lineTo(x+10, y+10);
lineTo(x, y+10);
endFill();
}
};

// loop that makes the movieclips with an onRelease function to each.
for (var i:Number = 0; i < Monstertabel.length; i++) {
var clipname:String = "color" + i;
var farve:String = "0x" + Monstertabel[i];
monsterfarver.createEmptyMovieClip(clipname, this.getNextHighestDepth());
monsterfarver.clipname.square(x, 0, farve);
monsterfarver.clipname.onRelease = function():Void {
var setcol = new Color("test");
setcol.setRGB(farve);
}
x = x + 10;
}

thx in advance.
bas1c

Tink
09-17-2004, 09:52 PM
u can use the value of i to namemonsterfarver.createEmptyMovieClip("myClip" + i + "_mc", this.getNextHighestDepth());

//then to target them
monsterfarver["myClip" + i + "_mc"].square(x, 0, farve);
monsterfarver["myClip" + i + "_mc"].onRelease = function():Void {
var setcol = new Color("test");
setcol.setRGB(farve);
}

bas1c
09-17-2004, 10:02 PM
thank you, it seems to work with the new code.
I still have some problems.

it is only applying the square and onrolover event to the last movieclip.

any idea why ?

Regards
bas1c

Tink
09-17-2004, 10:06 PM
it looks to me like all the clips are sitting on top of each other, so u can only see the clip that is on the top. if you want to move then on the x axis 10 pisxels usemonsterfarver["myClip" + i + "_mc"]._x = 10 * i;

bas1c
09-17-2004, 10:14 PM
thanks alot for the help so far.

Its only the last movieclip that actually shows.

My code looks like this now:

// string from php
var monsterfarvevars:String = "20FF00~2FFFFF~200000~2FF000~200FFF~20F0F0"
// splitting it up into an array
var Monstertabel:Array = monsterfarvevars.split("~");

// function to fill a movieclip with a 10 * 10 pixel square with a certain color.
MovieClip.prototype.square = function(x, y, colr) {
with (this) {
beginFill(colr);
moveTo(x, y);
lineTo(x+10, y);
lineTo(x+10, y+10);
lineTo(x, y+10);
endFill();
}
};

// loop that makes the movieclips with an onRelease function to each.
for (var i:Number = 0; i < Monstertabel.length; i++) {
var farve:String = "0x" + Monstertabel[i];
trace("myClip" + i + "_mc");
monsterfarver.createEmptyMovieClip("myClip" + i + "_mc", this.getNextHighestDepth());
monsterfarver["myClip" + i + "_mc"]._x = 12 * i;
monsterfarver["myClip" + i + "_mc"].square(0, 0, farve);
monsterfarver["myClip" + i + "_mc"].onRelease = function():Void {
var setcol = new Color("test");
setcol.setRGB(farve);
}
}

Tink
09-17-2004, 10:36 PM
getNextHighestDepth() function is the problem. replace this function with 'i' monsterfarver.createEmptyMovieClip("myClip" + i + "_mc", i);ormonsterfarver.createEmptyMovieClip("myClip" + i + "_mc", monsterfarver.getNextHighestDepth());

bas1c
09-17-2004, 11:16 PM
thx alot it works. :)


But now i have a new problem, it seems that the color that applies to the "test" movieclip on stage when i press the "buttons" is the same on all movieclips.


its like they all have the same onRelease function.

Tink
09-17-2004, 11:29 PM
by storing a reference to farve inside each clip, you can then access its valuethis.createEmptyMovieClip ("monsterfarver", 1);
// loop that makes the movieclips with an onRelease function to each.
for (var i:Number = 0; i < Monstertabel.length; i++) {
var farve:String = "0x" + Monstertabel[i];
monsterfarver.createEmptyMovieClip("myClip" + i + "_mc", i);
monsterfarver["myClip" + i + "_mc"]._x = 12 * i;
//store reference
monsterfarver["myClip" + i + "_mc"].farve = farve;
monsterfarver["myClip" + i + "_mc"].square(0, 0, farve);
monsterfarver["myClip" + i + "_mc"].onRelease = function():Void {
trace (this.farve);
var setcol = new Color(_root.test);
setcol.setRGB(this.farve);
}
}

bas1c
09-17-2004, 11:40 PM
thx ;)

I learnt alot from that !

actually i made it this way, but i like your method better, because then i avoid 2 movieclips getting the same name if 2 colors are alike. :D

// string from php
var monsterfarvevars:String = "20FF00~2FFFFF~200000~2FF000~200FFF"
// splitting it up into an array
var Monstertabel:Array = monsterfarvevars.split("~");
var setcol = new Color("test");
// function to fill a movieclip with a 10 * 10 pixel square with a certain color.
MovieClip.prototype.square = function(colr) {
with (this) {
beginFill(colr);
moveTo(0, 0);
lineTo(10, 0);
lineTo(10, 11);
lineTo(0, 11);
endFill();
}
};

// loop that makes the movieclips with an onRelease function to each.
for (var i:Number = 0; i < Monstertabel.length; i++) {
var farve:String = Monstertabel[i];
var lillefarve:String = "0x" + Monstertabel[i];
monsterfarver.createEmptyMovieClip("0x" + farve, monsterfarver.getNextHighestDepth());
monsterfarver["0x" + farve]._x = 12 * i;
monsterfarver["0x" + farve].square(lillefarve);
monsterfarver["0x" + farve].onRelease = function():Void {
setcol.setRGB(this._name);
}
}