A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Combine Multiple Variable Names Together to Target New Variable

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    1

    Combine Multiple Variable Names Together to Target New Variable

    Heya

    I'm still learning flash and actionscript 3 and i am having trouble with variable and object names. I need to be able to combine variable names together (in the same way as php can combine by doing var1.var2).

    My swf contains 4 loaders (image1_loader, image2_loader etc..) which are a child of (image1_content, image2_content etc.....)
    I then have 4 buttons which load an image into the loader and while doing so they define the currently active loader.
    Finally i have 4 control buttons - scale up/down and rotate clockwise/anticlockwise which should only control the currently active loader (as set by the buttons above)

    So my buttons as well as loading the image have the event listener:
    Code:
        image1_btn.addEventListener(MouseEvent.CLICK, setCurrentSelection);
        image2_btn.addEventListener(MouseEvent.CLICK, setCurrentSelection); 
    (and so on..) 
    
        function setCurrentSelection(e:MouseEvent):void {
    	    if (e.currentTarget===image1_btn){activeLoader='image1';}
    	    if (e.currentTarget===image2_btn){activeLoader='image2';}
    	    if (e.currentTarget===image3_btn){activeLoader='image3';}
    	    if (e.currentTarget===image4_btn){activeLoader='image4';}
        }
    So after setting my activeLoader as the string 'imageX' i have a control functions as in this rotate one:
    Code:
        rotateClock_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotateClockwise);
        rotateAnti_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotateAntiClockwise);
    
        function rotateClockwise(event:Event):void {
    	    rotateAroundCenter(imageX_content, 10, ptR);
        }
    
        function rotateAntiClockwise(event:Event):void {
    	    rotateAroundCenter(imageX_content, -10, ptR);
        }
    So within the rotateClockwise and rotateAntiClockwise functions i need to be able to recognise which is the currently active loader and have that number instead of the X - so if it is image1_loader - it needs to be image1_content, if 4 - image4_content...
    I had tried to do it as this but it doesn't like it being a string:

    Code:
        rotateAroundCenter((activeLoader+'_content'), 10, ptR);
    Please could anyone help me understand how to solve (sorry if i havent explained it clearly!) and please go easy on me - i'm learning as i go myself

    Lauren

  2. #2
    Flactionscrish Baby Minion's Avatar
    Join Date
    Nov 2005
    Location
    Planet Earth
    Posts
    312
    well, i think you are looking for bracket notation access.

    Actionscript Code:
    var image1_content:Sprite;
    var activeImageNum:Number = 1;
    var activeImage = this["image" + activeImageNum + "_content"];
    ktu[k-two]
    he who hesitates is lost; so i guess i'll wander intently

    Are you sure this is real?
    Life is Love, Love is Blind, Blind we go through Life.
    Life isn't hard, dealing with your self is.

    The concept of life in a human brain is weakening day after day. Live every day like its your last. Take the chances, and opportunities, and never let authority push you around for fun.


  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That is what she actually asked for, but there's no reason to actually use string concatenation to identify the active object here.

    Just save a reference to the object you want, not its name (or some portion of its name).
    Code:
        var activeContent:DisplayObject;
    
        image1_btn.content = image1_content;
        image2_btn.content = image2_content;
        //and so on.  
    
        image1_btn.addEventListener(MouseEvent.CLICK, setCurrentSelection);
        image2_btn.addEventListener(MouseEvent.CLICK, setCurrentSelection); 
    (and so on..) 
    
        function setCurrentSelection(e:MouseEvent):void {
                activeContent = MovieClip(e.currentTarget).content;
        }
    
        rotateClock_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotateClockwise);
        rotateAnti_btn.addEventListener(MouseEvent.MOUSE_DOWN, rotateAntiClockwise);
    
        function rotateClockwise(event:Event):void {
    	    rotateAroundCenter(activeContent, 10, ptR);
        }
    
        function rotateAntiClockwise(event:Event):void {
    	    rotateAroundCenter(activeContent, -10, ptR);
        }
    Generally, when you find yourself thinking "why can't I do this the way I would in php?" the answer is "because php is an abomination."

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