Creating Dynamic Buttons with Different identity

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

try something like this:


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:

btnArr[0].x += 10;

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

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

hth
Carlo

PS: Tutorial

[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[/quote]

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

Hey Seek_nik

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

try this:


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

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


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

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

try

movieClip(event.target).filters

let me know if it worked :slight_smile:

cheers

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

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


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…

use “target” not “Target” and try again

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

good :slight_smile:

have fun!

cheers Carlo

[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 :slight_smile:

[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]

[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…

[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… :slight_smile: :party:

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

Good to hear

and thanks for the feedback

Carlo