A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [F8] dynamic function?

  1. #1
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755

    [F8] dynamic function?

    Im sure how to even ask this one..LOL

    but I want to be able to set a variable (parameter) that holds a function name..

    example:

    I have a parameter for user input (callBackFuntion)

    I want the user to be able to enter in a function name that they created..(example: allDone() is a function the user created.) and when 'callBackFunction' is found in the code..it executes the 'allDone();' function they created and defined as the callBackFuntion variable/function.

    so in this snippet of code:
    Code:
    if (lb>=tb) {
    	trace("alwaysOn3: "+alwaysOn);
    	if (alwaysOn == true) {
    		_visible = false;
    		trace("visible2 (false):"+_visible);
                    callBackFuntion;
    	} else {
    		trace("deleting onEnterFrame code");
    		trace("Back to frame 1");
    		gotoAndStop(1);
                    callBackFuntion;
    	}
    }
    I want the part in BOLD to really be var that passes in the user define function they want to execute in that place..

    (makes sense?)


    how would I go about doing that? make a function the calls that function? or something? LOL

    thanks

  2. #2
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    soemthing along these lines?

    Code:
    var userInput:String = tInput.text; //suppose the user inputted greeting
    
    
    _root[userInput]();
    
    function greeting():Void{
    	trace("hello there");
    }
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  3. #3
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    more like if user input was the name of a function they wrote.

    so that drag the component to the stage (I created a custom component that has parameters you can define)

    I want them of them to be 'callBackFunction' where they put the name of a function they want to execute when the component is done doing what it does.

    so callBackFuntion = userFunction();

    (then of course would have to create this 'userFunction()
    so:
    Code:
    function userFunction() {
        trace("callBackFunction executed");
    }
    so now..in my code for the 'component' (simple preloader component..nothing special)

    when it gets this part in the code...
    Code:
    if (lb>=tb) {
    	trace("alwaysOn3: "+alwaysOn);
    	if (alwaysOn == true) {
    		_visible = false;
    		trace("visible2 (false):"+_visible);
                    callBackFuntion;
    	} else {
    		trace("deleting onEnterFrame code");
    		trace("Back to frame 1");
    		gotoAndStop(1);
                    callBackFuntion;
    	}
    }
    I want the user defined function to execute whenever I have the 'callBackFuntion' (variable I guess?) is 'called'

    I basically want a VAR to hold a function.. (so I can change the function but keep the same var)...but how do I get a 'VAR' to execute the function?

    (am I making sense?) lol sorry if not..

  4. #4
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    along the same lines as Silentweed,
    where user input is the variable - callBackFuntion
    if user inputs - allDone

    _root[callBackFuntion](); -- will activate the function - allDone();

    hope i'm reading you correctly

  5. #5
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    yes.. I think you are.. I guess I just want reading silentweeds post correctly.. I just saw .text and brain turned off I guess..LOL

    so this woudl be correct:
    Code:
    if (lb>=tb) {
    	trace("alwaysOn3: "+alwaysOn);
    	if (alwaysOn == true) {
    		_visible = false;
    		trace("visible2 (false):"+_visible);
                    _parent[callBackFuntion]();
    	} else {
    		trace("deleting onEnterFrame code");
    		trace("Back to frame 1");
    		gotoAndStop(1);
                    _parent[callBackFuntion]();
    	}
    }
    parentheses need to be OUTSIDE the brackets?

    or should the user input be:

    userFunction(); that they declare?

  6. #6
    FK'n_dog a_modified_dog's Avatar
    Join Date
    Apr 2003
    Location
    "aaarf"
    Posts
    9,176
    does this simple test file make any sense to you

  7. #7
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    sure does.. just wanted to make sure (I guess I'll need to check for and strip out if they do) they didnt need to add the () in the parameter field.



    thanks both you guys.

  8. #8
    Flashmatics silentweed's Avatar
    Join Date
    Mar 2005
    Location
    London, UK
    Posts
    4,875
    no worries dude..glad u got it sorted
    Flashmatics | Flashmatics Library (free downloads)
    -----------------------------------------------------
    Latest Projects:
    The Dead Room | Are You Smarter | Prison Break (Frame A Friend) | Beck's Fusions | The 24 Experience

    -----------------------------------------------------
    Flash Certified Developer

  9. #9
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    funny you shodl post.. I 'just' tried it now...

    and it ISNT working.. (not executing the 'userFunction')

    Code:
    if (lb >= tb) {
    			trace("alwaysOn3: "+alwaysOn);
    			if(alwaysOn == true){
    				_parent[callBackFuntion]();
    				_visible = false;
    				trace("visible2 (false):"+_visible);
    			}else{
    				trace("deleting onEnterFrame code");
    				delete rotatingArrows.onEnterFrame;
    				trace("Back to frame 1");
    				_parent[callBackFuntion](); // does NOT work
    				_parent.userFunction();// test to see if HARDCODING calls it...works
    				gotoAndStop(1);
    			}
    		}

  10. #10
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    on a side note.. since you (or anyone) is around.. i got a puzzeling post here in the AS forum about calling javaScript from dynamically loaded text..

    works.. but if I try to append somethign on the URL/query string.. it breaks..

  11. #11
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    ok..well I think it was because a typo (spelled function funtion) DOH!!

    and it works.. (sorry)

    still have that javaScript question though...

  12. #12
    Senior Member whispers's Avatar
    Join Date
    Mar 2001
    Location
    CFA2h (respect the HEX)
    Posts
    12,755
    You can test it out if you like.. (you helped me complete it)

    documentation:
    Welcome to the simplePreloader component.

    Desc:
    This component will give you a visual representation (both through animation & percetange loaded text) on how much of your content is loaded into it's 'target clip'.

    Overview:
    it is very easy to use. You drag a component to the stage, and you 'assign' it a target clip (movie clip) to watch. Anything that gets loaded into this 'target clip' will trigger the simplepreloader to show and display how much left there is to load. When complete, the movieClip will disappear. Load another piece of content (image, swf or video clip) and the preloader will trigger again.

    Once you have your component instance on the stage, make sure to give it an INSTANCE NAME. By selecting the simplePreloader instance, you can then go the PARAMETERS panel in the PROPERTY INSPECTOR and assign the custom values for you component & movie.

    PARAMETERS:

    1.) loaderColor: choose a #color that you want the rotatingArrows to be.

    2.) percentVisible: choose to display the percentage textField or not.

    3.) percentColor: choose a #color that you want the percentage text to be.

    4.) rotateSpeed: number value that depicts the speed of the rotatation.

    5.) targetClip: 'path' (relative or absolute) to the movie clip you want the simplePreoader to 'watch' and give results on. (Note: the path is /home to whatever timeline the simplePreloader is in)

    6.) callbackFunction: lets you declare a function name that you want executed after the 'loading' is complete. (NOTE: do NOT include the () after the funciton name)

    7.) alwaysOn: lets you decide to have the 'check' (onEnterFrame) running continuously.

    (Note: it is recommended to run this default in FALSE (off) mode, and manually move the playHead to frame 2 on the simplePreloader component)

    TRUE MODE: Lets you ONLY do a simple 'loadMovie()' on your target..and the 'simplePreloader' will do the rest. (but leaves code running all the time and can eat up resources)

    Example: (alwaysOn set to TRUE)
    Code:
    button1_btn.onPress = function(){
        containerClip.imageHolder.loadMovie("image1.jpg");
    }
    FALSE MODE: (recommended) Means you have to manually advance the playhead to frame 2 in the 'simplePreloader' component, but does not leave messy or rampant code running.

    Example: (alwaysOn set to FALSE)
    Code:
    button1_btn.onPress =function(){
        containerClip.imageHolder.loadMovie("image1.jpg");
        simplePreloader.gotoAndStop(2);
    }
    I removed the .jpg's (so I could attach/file size) just put any images called image1.jpg, image2.jpg, image3.jpg in the folder of the TEST .swf (to test the component)

    I included the.swc so you can install it if you desire and have it in your components panel for a quick preloader.

    any suggestions would be great... not sure what else you can do I suppose..
    Attached Files Attached Files

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