A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: [RESOLVED] Variable question

  1. #1
    Member
    Join Date
    Nov 2007
    Posts
    46

    resolved [RESOLVED] Variable question

    Can anyone help me make this work?

    var test1;
    test1 = "test";

    ["load"+test1+"()"]*****


    function loadtest():void
    {
    trace("it worked")
    }

    *****this keeps coming back as loadundefined()

    Basically what I am trying to do is use a variable to help reduce the amount of code that i have to write. However, I am not having any luck with this. Ultimately, I plan on using this to call a function from a loaded swf movie. This function will then load another swf movie depending on what the variable is set to. for instance if the variable is home it will call to the function loadhome() which will load home.swf. likewise, if the variable is about then it will call the function loadabout() which will load the swf movie about.swf.

    Thanks for any help!

    Stephen E.
    Last edited by StephenErickson; 11-26-2007 at 12:02 AM.

  2. #2
    strandedgenius.com
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    187
    Basically when using [] - square brackets you need to preface it with the scope, so something like this or whatever your scope is. I think however this syntax might not work. I think I know what you're trying to do. You might want to create a public variable that's strict datatyped as a Function in your main class (master class whatever you want to call it). And each time you load a seperate swf there would be code in that swf that defines a function in that swf equal to the var you have in your main class.

    For Example;
    Code:
    package {
    
    	public class MainClass {
    		
    		public static var myCustomFunction:Function;
    		
    		public function MainClass() {
    			
    		}
    		
    	}
    	
    }

    Some other SWF
    Code:
    package {
    	
    	
    	public class LoadedSWFClass {
    		
    		private function someFunction:Function
    		
    		public function LoadedSWFClass() {
    			
    			MainClass.myCustomFunction = someFunction;
    			
    		}
    		
    		public function someFunction():void {
    			
    		}
    		
    	}
    	
    	
    }
    JScarlip,
    That Was Nice!

  3. #3
    strandedgenius.com
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    187
    Also try this:

    this["load"+test1]();

    What you did won't work for several reasons

    1. no scope the square brackets need a scope
    2. the function brackets need to be outside the square brackets.

    Hope this helps. I think my fight example is cooler though. FYI try to make it a habit of strict dataTyping your variables it will help debug your code for sure. So var test1:String = "test";
    JScarlip,
    That Was Nice!

  4. #4
    Member
    Join Date
    Nov 2007
    Posts
    46
    Hey,

    Thanks for the reply but this isn't quite what I'm looking for.
    I am using adobe flash cs3 pro and I am scripting in the actions panel of a selected frame. Does anyone else have an idea?

  5. #5
    strandedgenius.com
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    187
    Yeah so am I. I was just assuming since you didn't specify that you where using Classes. I didn't know people actually still wrote code on frames. Regardless you can still do this. Did you read my second post? Did you try that? Or did you just assume it won't work? Anyone else? Any ideas?
    JScarlip,
    That Was Nice!

  6. #6
    Member
    Join Date
    Nov 2007
    Posts
    46
    It still isnt working. Whats wrong with writing code on frames? is their a noticeable difference between writing code on a frame and writing it in pure as?

    heres what I have now.

    MovieClip(parent.parent.root).menu_mc.nextsection

    nextsection is the variable that will determine which function to call. I used a trace in the line right under it and it returned with the correct function to be called. However, it won't work in that line of code any idea why?

  7. #7
    strandedgenius.com
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    187
    Did you try making a variable that's a function, you can even make it _global if it's easier. So when you load a new swf you can change that global variable to whatever you need it to be. Just remember that the variable itself won't have the brackets "()". But when you call it you do use them.

    So for example. On your main swf (the top level swf) make a variable such as like this:

    var mySpecialFunction:Function;

    Then when you load a swf you can set that variable to be equal to whatever function you want. So for example inside the loaded swf you would have this code on the first frame.

    _root.mySpecialFunction = whateverFunctionINeedItToBe;


    function whateverFunctionINeedItToBe():void {
    trace("I'm here!");
    }

    And then you call it:

    _root.mySpecialFunction();
    JScarlip,
    That Was Nice!

  8. #8
    Member
    Join Date
    Nov 2007
    Posts
    46
    Hey,

    Well, your scripting in actionscript 2 which is pretty different then actionscript 3, which is what i'm scripting in. I took what you wrote and converted it to as3 and this is what it look like:

    MovieClip(parent.parent.root).menu_mc.nextsection( )

    yet i'm getting this error:

    TypeError: Error #1006: value is not a function.
    at home_fla::Symbol1_1/home_fla::frame40()

    Which is funny because this:

    MovieClip(parent.parent.root).menu_mc.about()

    works and it is the same thing as the first except that the function to be called is in a variable. wierd... Also, when I trace the variable it comes back correct...So, I don't know whats going on.

  9. #9
    strandedgenius.com
    Join Date
    Apr 2005
    Location
    Toronto
    Posts
    187
    My bad, I went as2 on you. I'm working with both right now simultaneously. So I make mistakes sometimes. By I am thinking in as3. When I say "_root" I'm referring to the stage, or whatever your scope is of that variable which should be a Function. Make sure you strict data typing that variable as a Function, ie: var myFunction:Function;

    Can you post up a link to your source files there?

    I'm wondering what you're doing here "MovieClip(parent.parent.root)", I'm pretty sure the MovieClip constructor doesn't have any parameters. So what are you try to do with that?

    If you where to be using Classes instead of Frames it would be easier. AS3 was design to be complete Object Oriented, meaning Classes. It would be easier to know what is where and how to access it. This comes in handy when debugging.
    JScarlip,
    That Was Nice!

  10. #10
    Member
    Join Date
    Nov 2007
    Posts
    46
    Hey,

    my source file is much to big to post here i'm sorry. Also, movieclip(parent.parent.root) tells flash where to look for certain things like my variable. I stated before that I am working with multiple swf movies all tied together. In order for my loaded swf movies to communicate with the main movie I have to use MovieClip(parent.parent.root) I ended up going a different way with this(the long way). All I was trying to do was to simplify how much coding I needed to do. The problem that I am running into is that MovieClip(parent.parent.root).menu_mc.nextsection( ) is returning about(). However this isnt working because the function to be called is not located in the loaded swf movie rather, it lies in the same place as the variable does, MovieClip(parent.parent.root).menu_mc. So, i tried doing this:

    this["MovieClip(parent.parent.root).menu_mc."+MovieClip (parent.parent.root).menu_mc.nextsection]()
    but this also returns the same error as before. Any ideas?

  11. #11
    Member
    Join Date
    Nov 2007
    Posts
    46
    Hey!

    Thanks for your help I got it to work! The trick was to set up my variable as a function like you said! I was using string but when i changed it to function it worked! Now I have reduced all of this:

    if (MovieClip(parent.parent.root).menu_mc.nextsection == "about")
    {
    MovieClip(parent.parent.root).menu_mc.about()
    }
    if (MovieClip(parent.parent.root).menu_mc.nextsection == "home")
    {
    MovieClip(parent.parent.root).menu_mc.home()
    }
    if (MovieClip(parent.parent.root).menu_mc.nextsection == "bsl")
    {
    MovieClip(parent.parent.root).menu_mc.bsl()
    }

    (this would have grown as my project grew)

    and I simplified it to this:

    MovieClip(parent.parent.root).menu_mc.nextsection( )

    Whew so much better!

    Thanks so much for your help!

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