PDA

View Full Version : how to create buttons dynamically



kamelus
October 16th, 2002, 04:54 AM
here is my contribution with this script you can create buttons dynamically and remove them
(howmany) is the variable of the number of the buttons to create.


function makebuttons(){
// if we have buttons created at first time we remove them to recreate others ones

if(_global.createdClips.length>0){
clearbuttons();
}
addbuttons(homany);
}

function addbuttons() {
// we create an array to save the buttons references
_global.createdClips = new Array();

for(i=1;i<=howmany;i++){
myButton = this.createEmptyMovieCli("but"+i,i+_global.maxSeed)
myButton.lineStyle(1);
myButton.beginFill(0x336699);
myButton.lineTo(22,0);
myButton.lineTo(22,20);
myButton.lineTo(0,20);
myButton.lineTo(0,0);
this["but"+i]._x = i*myButton._width*1;
myButton.createTextField("txt"+i,i+10);
myButton["but"+i].text=i;

// when the movieclip is created we save the reference in the array using this syntax

_global.createdClips.push(myButton);


//=====================we create another movie to make a 3Doutline to the buttonD========================

myButton = this.createEmptyMovieClip("contour"+i,i+_global.maxSeed+20);
myButton.lineStyle(1.75,0x999999);
myButton.lineTo(22,0);
myButton.lineTo(22,20);
myButton.lineStyle(1.75,0xffffff);
myButton.lineTo(0,20);
myButton.lineTo(0,0);
this["contour"+i]._x = i*myButton._width*1;

_global.createdClips.push(myButton);


//=============we create a textfield to store the caption ====================================
myButton.createTextField("text"+i, 0, 3, 1, 18, 20);
myButton["text"+i].multiline = false;
myButton["text"+i].wordWrap = false;
myButton["text"+i].border = false;
myButton["text"+i].background = false;
myButton["text"+i].variable = "";
myformat = new TextFormat();
myformat.font="verdana";
myformat.size="9";
myformat.color = 0xffffff;
myformat.bullet = false;
myformat.underline = false;
myButton["text"+i].text = i;
myButton["text"+i].setTextFormat(myformat);
_global.createdClips.push(myButton);


// we attach event to the button
myButton.onPress = function(){

//you place the code you want

}

myButton.onRelease = function(){

// you place the code you want

}
myButton.onRollOver = function(){

// you place the code you want
}

myButton.onRollOut = function(){

// you place the code you want
}

}

}

function clearbuttons() {
for(var myButton in _global.createdClips){
_global.createdClips[myButton].removeMovieClip();

// we delete the reference of the movieclip removed from the array

_global.createdClips.pop();

}
}


that's all
i will try to ameliorate this code

enjoy it
if you have any question post your comment

pom
October 16th, 2002, 06:53 AM
Looks good, Kamelus. I'll look at it in details later. First thing I noticed: shouldn't you declare function addbuttons(num)?

pom :q:

Somebody rate this thread please. :)

kamelus
October 16th, 2002, 10:52 AM
thank you ily

you 're right i have forgotten to declare the function addbuttons like this

function addbuttons(howmany){

}

instead

function addbuttons(){

}

thanks