School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,479 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,749 people online right now. Registration is fast and FREE... Join Now!




Keeping a variable unique for a dynamically created function

 

Keeping a variable unique for a dynamically created function

letterafterz

22 May, 2009 - 09:33 PM
Post #1

New D.I.C Head
*

Joined: 22 May, 2009
Posts: 1

Hi guys,

Sorry if this is addressed elsewhere, I've been scouring the internet for over an hour but I'm not entirely sure even what to search for exactly.

What I'm trying to do is setup a function that shoots through a set of buttons within a subset and creates click code for these buttons. I'm using AS2.

THe three variables are the identifiers for the actual keyframe i wish to jump to, the movieclip in which i want to perform the action and the actual button to go to the right frame.

Presently the problem I'm having is that the variables aren't stored when the onRelease functions are created, so they all are just calling the LAST variables declared.

Here's a sample of the code I currently have
CODE

//function to load canvas/acrylic
function setSwatches(mySwatchCount:Number, myContainer:String)
{
    //set holder name
    var myContainerName = myContainer + "_mc"
    
    //cycle for each swatch
    for(var i = 0; i < mySwatchCount; i++){
        var mySwatchbtn = myContainer+ "_" + i + "_btn";
        var myFrameLabel = "a"+i; //adds an "a" because frame labels don't work with numbers

        //attach button code to load each swatch
        trace(myFrameLabel);
        
        this[mySwatchbtn].onRelease = function(){
            trace(myFrameLabel);
            _root[myContainerName].gotoAndStop(myFrameLabel);
        }
    }
}

//setup buttons
setSwatches(8, "fa_acrylic");
setSwatches(8, "fa_canvas");


Thanks guys!
Steve
Natural Cow Design
http://www.naturalcowdesign.com.au

This post has been edited by letterafterz: 22 May, 2009 - 09:35 PM

User is offlineProfile CardPM
+Quote Post


zaferaltun

RE: Keeping A Variable Unique For A Dynamically Created Function

26 May, 2009 - 02:48 AM
Post #2

New D.I.C Head
*

Joined: 26 May, 2009
Posts: 5


My Contributions
If i understood you, your style is wrong when defining the variable about frame. Try this:

CODE

this[mySwatchbtn].myFrameLabel = "a"+i;

this[mySwatchbtn].onRelease = function()
{
     _root[myContainerName].gotoAndStop(this.myFrameLabel);
}

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:41AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month