A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] referring to an instance with a variable??

  1. #1
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24

    resolved [RESOLVED] referring to an instance with a variable??

    i can't really explain what I'm trying to do here... but here it goes
    i'm trying to access an instance by using a variable but when i try to manipulate a property to the instance (by referring to it with a variable) it only recognizes it as a string... for instance... (pun intended??)

    public var prevbutton:String = "btn1";

    trace(["page"+prevbutton]);
    // which traces - pagebtn1

    trace(pagebtn1);
    // which traces the instance class - [object btn1pageclass]

    I want to call the instance by using a variable... how do i do this?

  2. #2
    lemon juice hurts your eyes florianvanthuyn's Avatar
    Join Date
    Jul 2005
    Location
    Merelbeke, Belgium
    Posts
    546
    Try this:
    PHP Code:
    public var prevBtn:btn1pageclass pagebtn1
    So you make a variable with the same type as your pagebtn1 (I assumed it was btn1pageclass, but if it's any different, change the name after the colon) and set it equal to the object you want to access.

    Now you should be able to do prevBtn.x & prevBtn.y (if the object inherits of a DisplayObject) and functions inside of the class
    Florian Vanthuyne

    WAR AGAINST SOLVED THREADS
    mark yours as Resolved under Thread Tools!

  3. #3
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    this defeats the purpose - my variable changes from btn1 thru btn6 and i need to say

    lets just say...// if ["page"+variable] = pagebtn1... then i want something like this...

    this["page"+variable]gotoAndPlay(1);

    how do i do this??

  4. #4
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    ... or... now that i can get the string to say pagebtn? (?=1-6), how do i convert the string into an instance definition?

  5. #5
    WohOoooooo zompo's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    138
    not sure what your trying to do but you have the getChildByName() function

    if you have 5 movieclips named mc1, mc2 ... then your could use

    var mcNum:int = 1;
    getChildByName('mc' + mcNum);

    what do you get if you do trace(getChildByName("page"+prevbutton));?


    Try out my game http://alpha.bombwar.com - Alphakey: fkaplha

  6. #6
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    trace("getchild "+getChildByName("page"+prevbutton));

    = getchild null



    =(__

  7. #7
    WohOoooooo zompo's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    138
    You cant use the name of the class, here is a example of how u use the getChildByName()

    var s:Sprite = new Sprite();
    s.name = "btn1";
    addChild(s);

    // and then

    var btnNum:int = 1;
    getChildByName("btn" + btnNum)


    Try out my game http://alpha.bombwar.com - Alphakey: fkaplha

  8. #8
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    that sounds reasonable thanks i'll try right now

  9. #9
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    thanks... i think it worked right away...

    public var pagebtn1:btn1pageclass = new btn1pageclass();
    pagebtn1.name = "pagebtn1";
    addChild(pagebtn1);
    trace(this.getChildByName("page"+prevbutton));
    ////// traces --- [object btn1pageclass]


    thank you so much ZOMPO - SOLVED

  10. #10
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    so can i not now say...

    PHP Code:
    getChildByName(page+prevbutton).gotoAndPlay(1); 
    ?????

    i get error:
    1061: Call to a possibly undefined method gotoAndPlay through a reference with static type flash.displayisplayObject.

  11. #11
    WohOoooooo zompo's Avatar
    Join Date
    Oct 2008
    Location
    Sweden
    Posts
    138
    you could try

    var mc:MovieClip = getChildByName(page+prevbutton) as MovieClip;
    mc.gotoAndPlay(1);


    Try out my game http://alpha.bombwar.com - Alphakey: fkaplha

  12. #12
    LukewarmSteak - Jordan Embury
    Join Date
    Jul 2009
    Location
    Toronto Ontario Canada
    Posts
    24
    holy F... you're amazing... great thinking... ZOMPO +=1

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center