# Creating Dynamic Buttons with Different identity

**URL:** <https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107>\
**Category:** flash\
**Created:** [May 23, 2008, 6:08am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107 "2008-05-23T06:08:50Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 6:08am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/1 "2008-05-23T06:08:50Z")

</div>

Hello All,

I am looking for a way to give different instance names or ids to each button/MovieClip while creating multiple movie clips using for loop like

for(var i:int = 0; i\<3 ;i++)  
{  
var newBtn:Button = new Button();

addChild(newBtn);

newBtn.y = 100 + 50\*i;

}

here in above script, multiple buttons are being generated, but I am not able to control them individual, to apply different effects or different sounds on rollover of each buttons.

Please help with proper example.

Thanks and Best regards

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 23, 2008, 6:54am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/2 "2008-05-23T06:54:08Z")

</div>

try something like this:

```auto

var btnArr:Array = new Array()

for(var i:int = 0; i<3 ;i++){
    var newBtn:Button = new Button();

    addChild(newBtn);
    newBtn.y = 100 + 50*i;

    btnArr.push(newBtn);
}

```

that way you can control each single button with for example:

```auto
btnArr[0].x += 10;

```

or if you have a textfield callet “tf” in your button:

```auto
btnArr[1].tf.text = "I did this with an array"

```

hth  
Carlo

PS: [Tutorial](http://www.flepstudio.org/english/tutorials/mix/the-importance-of-an-array-20071029163.html)

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 7:06am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/3 "2008-05-23T07:06:47Z")

</div>

[quote=hasch2o;2329929]try something like this:

```
ActionScript Code:
[LEFT][COLOR=#000000] **var** [/COLOR] btnArr:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Array[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000])[/COLOR]

```

[COLOR=#0000FF]for[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000FF]int[/COLOR] = [COLOR=#000080]0[/COLOR]; i\<[COLOR=#000080]3[/COLOR] ;i++[COLOR=#000000])[/COLOR][COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] newBtn:[COLOR=#0000FF]Button[/COLOR] = [COLOR=#000000] **new** [/COLOR] [COLOR=#0000FF]Button[/COLOR]COLOR=#000000[/COLOR];

```
addChild[COLOR=#000000]([/COLOR]newBtn[COLOR=#000000])[/COLOR];
newBtn.[COLOR=#000080]y[/COLOR] = [COLOR=#000080]100[/COLOR] + [COLOR=#000080]50[/COLOR]*i;

btnArr.[COLOR=#0000FF]push[/COLOR][COLOR=#000000]([/COLOR]newBtn[COLOR=#000000])[/COLOR];

```

[COLOR=#000000]}[/COLOR]  
[/LEFT]

that way you can control each single button with for example:  
ActionScript Code:  
[LEFT]btnArr[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]x[/COLOR] += [COLOR=#000080]10[/COLOR];  
[/LEFT]

or if you have a textfield callet “tf” in your button:  
ActionScript Code:  
[LEFT]btnArr[COLOR=#000000][[/COLOR][COLOR=#000080]1[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]tf[/COLOR].[COLOR=#0000FF]text[/COLOR] = [COLOR=#FF0000]“I did this with an array”[/COLOR]  
[/LEFT]

hth  
Carlo

PS: [Tutorial](http://www.flepstudio.org/english/tutorials/mix/the-importance-of-an-array-20071029163.html)[/quote]

Let’s check !!

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 7:21am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/4 "2008-05-23T07:21:30Z")

</div>

> [@seek\_nik;2329931](#):
>
> Let’s check !!

but when I am applying some effects

btnArr\*.addEventListener(MouseEvent.MOUSE\_OVER, showEffect);  
btnArr\*.addEventListener(MouseEvent.MOUSE\_OUT, remEffect);

public function showEffect(event:MouseEvent):void

```
    {
        
        for (var i:int = 0; i &lt; 3; i++) 
            {
                btnArr*.filters = [new DropShadowFilter(1,90)];
            }
    }
    
    
    public function remEffect(event:MouseEvent):void
    
    {
            
        for (var i:int = 0; i &lt; 3; i++) 
            {
                btnArr*.filters = [];
            }

    }

```

Drop shadow effect is coming to all buttons together while taking cursor over one button only

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 23, 2008, 7:37am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/5 "2008-05-23T07:37:34Z")

</div>

Hey Seek\_nik

the problem is, that you are applying the filter to all your buttons (with the for loop)

try this:

```auto

btnArr*.addEventListener(MouseEvent.MOUSE_OVER, showEffect);
btnArr*.addEventListener(MouseEvent.MOUSE_OUT, remEffect);

public function showEffect(event:MouseEvent):void{
    event.Target.filters = [new DropShadowFilter(1,90)];
}

public function remEffect(event:MouseEvent):void{
        event.Target.filters = [];
    }
}

```

does that work?

PS: no messenger, sorry, but I will keep an eye on the thread

---

<div class="post-metadata">

**Author:** ![doridori](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@doridori](https://forum.kirupa.com/u/doridori)\
**Post date:** [May 23, 2008, 7:43am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/6 "2008-05-23T07:43:20Z")

</div>

If you are using or extending the MovieClip class instead, you can give each button a unique id as an attribute.

```auto

for(var i:int = 0; i<3 ;i++){
    var newBtn:MovieClip = new MovieClip();

    addChild(newBtn);
    newBtn.y = 100 + 50*i;
    
    newBtn.id = i; // you can call this what you want, for example newBtn.powerLevel

    newBtn.addEventListener(MouseEvent.CLICK, btnClick);

}

private function btnClick(e:MouseEvent):void
{
    trace("You clicked button number " + e.target.id);
}

```

This makes it easy to identify individual buttons, but also requires you to keep references to each button id (so that you know what should happen when button # is clicked).

Hope it works and helps

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 23, 2008, 8:03am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/7 "2008-05-23T08:03:22Z")

</div>

In the message you sent me, you said:  
“event.target.filters is undefined”

try

```auto
movieClip(event.target).filters

```

let me know if it worked 🙂

cheers

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 23, 2008, 8:04am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/8 "2008-05-23T08:04:14Z")

</div>

Oh, and btw: it has to be “target” and not “Target”, sorry for the typo!

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 8:05am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/9 "2008-05-23T08:05:18Z")

</div>

[quote=doridori;2329940]If you are using or extending the MovieClip class instead, you can give each button a unique id as an attribute.

```auto

for(var i:int = 0; i<3 ;i++){
    var newBtn:MovieClip = new MovieClip();

    addChild(newBtn);
    newBtn.y = 100 + 50*i;
    
    newBtn.id = i; // you can call this what you want, for example newBtn.powerLevel

    newBtn.addEventListener(MouseEvent.CLICK, btnClick);

}

private function btnClick(e:MouseEvent):void
{
    trace("You clicked button number " + e.target.id);
}

```

This makes it easy to identify individual buttons, but also requires you to keep references to each button id (so that you know what should happen when button # is clicked).

Hope it works and helps[/quote]

The moment I am doing newButton.id = i, the following error is coming

1119: Access of possibly undefined property id through a reference with static type fl.controls:Button.

while doing

event.Target.id.filters = [new DropShadowFilter(1,90)];

```
                            or

```

event.Target.filters = [new DropShadowFilter(1,90)];

The following error is coming…

1119: Access of possibly undefined property Target through a reference with static type flash.events:MouseEvent.

Please advise…

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 23, 2008, 8:09am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/10 "2008-05-23T08:09:33Z")

</div>

use “target” not “Target” and try again

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 8:30am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/11 "2008-05-23T08:30:44Z")

</div>

> [@hasch2o;2329949](#):
>
> use “target” not “Target” and try again

Hey dear its Working, let me apply now sound on doing hover…

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 23, 2008, 8:31am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/12 "2008-05-23T08:31:53Z")

</div>

good 🙂

have fun!

cheers Carlo

---

<div class="post-metadata">

**Author:** ![doridori](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@doridori](https://forum.kirupa.com/u/doridori)\
**Post date:** [May 23, 2008, 8:35am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/13 "2008-05-23T08:35:24Z")

</div>

[quote=seek\_nik;2329948]The moment I am doing newButton.id = i, the following error is coming

1119: Access of possibly undefined property id through a reference with static type fl.controls:Button.[/quote]  
That is because your button has to be a MovieClip object to have custom attributes!  
The Button class won’t work.

But don’t change it now if you got it working the other way 🙂

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 9:15am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/14 "2008-05-23T09:15:51Z")

</div>

[quote=doridori;2329957]That is because your button has to be a MovieClip object to have custom attributes!  
The Button class won’t work.

But don’t change it now if you got it working the other way :)[/quote]

I think I need the id thing, because, I am storing sound paths in a array from xml, now while doing hover, I want to play some sound. So for that I need to know which button has been hovered so that accordingly that particular sound can be played according to the sound stored in particular array position. So here my code is,

newButton.addEventListener(MouseEvent.MOUSE\_OVER, playHandler);  
newButton.addEventListener(MouseEvent.CLICK, stopHandler);

function playHandler(event:MouseEvent):void  
{

```
           if( sndPlaying ){
              stopSound();
           }
           sndPlaying = true;
         [COLOR=SandyBrown] [COLOR=Red]snd = new Sound(new URLRequest("soundA.mp3"));[/COLOR][/COLOR]
           sndChannel = snd.play();
           sndChannel.addEventListener(Event.SOUND_COMPLETE, completeHandler);
        }
        function stopHandler(event:MouseEvent):void
        {
           stopSound();
        }
        function completeHandler(event:Event):void
        {
           stopSound();
        }
        function stopSound():void
        {
           sndPlaying = false;
           sndChannel.stop();
           sndChannel.removeEventListener(Event.SOUND_COMPLETE, completeHandler);
        }

```

[COLOR=SandyBrown] [COLOR=Red]snd = new Sound(new URLRequest(“soundA.mp3”)); in this line I need to place the relevant variable to replace this “soundA.mp3”, so that dynamically the button can play the relevant sound.[/COLOR][/COLOR]

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 9:39am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/15 "2008-05-23T09:39:04Z")

</div>

[quote=seek\_nik;2329970]I think I need the id thing, because, I am storing sound paths in a array from xml, now while doing hover, I want to play some sound. So for that I need to know which button has been hovered so that accordingly that particular sound can be played according to the sound stored in particular array position. So here my code is,

newButton.addEventListener(MouseEvent.MOUSE\_OVER, playHandler);  
newButton.addEventListener(MouseEvent.CLICK, stopHandler);

function playHandler(event:MouseEvent):void  
{

```
           if( sndPlaying ){
              stopSound();
           }
           sndPlaying = true;
         [COLOR=SandyBrown] [COLOR=Red]snd = new Sound(new URLRequest("soundA.mp3"));[/COLOR][/COLOR]
           sndChannel = snd.play();
           sndChannel.addEventListener(Event.SOUND_COMPLETE, completeHandler);
        }
        function stopHandler(event:MouseEvent):void
        {
           stopSound();
        }
        function completeHandler(event:Event):void
        {
           stopSound();
        }
        function stopSound():void
        {
           sndPlaying = false;
           sndChannel.stop();
           sndChannel.removeEventListener(Event.SOUND_COMPLETE, completeHandler);
        }

```

[COLOR=SandyBrown] [COLOR=Red]snd = new Sound(new URLRequest(“soundA.mp3”)); in this line I need to place the relevant variable to replace this “soundA.mp3”, so that dynamically the button can play the relevant sound.[/COLOR][/COLOR][/quote]

Hello dear,

I have resolved also, I mean the id, what I have done is, I have already made a button component into a movieclip, so I used that as var newButton:clickButton = new clickButton(); actually clickButton which I made a movie clip in library.

So id Problem is resolved, nd now to call sound files…

---

<div class="post-metadata">

**Author:** ![seek\_nik](https://avatars.discourse-cdn.com/v4/letter/s/f17d59/32.png) [@seek\_nik](https://forum.kirupa.com/u/seek_nik)\
**Post date:** [May 23, 2008, 9:43am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/16 "2008-05-23T09:43:20Z")

</div>

[quote=seek\_nik;2329984]Hello dear,

I have resolved also, I mean the id, what I have done is, I have already made a button component into a movieclip, so I used that as var newButton:clickButton = new clickButton(); actually clickButton which I made a movie clip in library.

So id Problem is resolved, nd now to call sound files…[/quote]

Loadz of Thanks to both of you, its done… 🙂 :party:

---

<div class="post-metadata">

**Author:** ![doridori](https://avatars.discourse-cdn.com/v4/letter/d/3da27b/32.png) [@doridori](https://forum.kirupa.com/u/doridori)\
**Post date:** [May 23, 2008, 11:02am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/17 "2008-05-23T11:02:57Z")

</div>

Great, congratulations!  
Hope you’re happy with your result 🙂

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [May 26, 2008, 6:32am UTC](https://forum.kirupa.com/t/creating-dynamic-buttons-with-different-identity/261107/18 "2008-05-26T06:32:21Z")

</div>

Good to hear

and thanks for the feedback

Carlo
