A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: [RESOLVED] Functions Delema

  1. #1
    It's not 1997 ?! mrgrim333's Avatar
    Join Date
    Feb 2004
    Posts
    254

    resolved [RESOLVED] Functions Delema

    So, how do I explain this?...
    I'm working with all dynamic stuff.

    On the main frame, I have a function, with parameters.
    Each tile (there's 25), holds it's own call on the function.

    Main Frame Function
    Code:
    function tilePlant(plant,id,level,plantStrand){
    //do stuff
    return plant;
    }
    Call of the function
    Code:
    on (release) {
    	_root.a1[1] = _root.tilePlant (_root.a1[1],1,11,_root.plant1[4]);
              // _root.a2[1] = _root.tilePlant (_root.a2[1],2,12),_root.plant2[4];
              // _root.a3[1] = _root.tilePlant (_root.a3[1],3,13),_root.plant3[4];
              // ect..
    }
    So, I need to call the function from an external button that works for all 25.
    But I can't specify the call of the function from there, it breaks the dynamics.

    Could I like, somehow send the call of the function to the button somehow?
    like
    Code:
    sender = "_root.a1[1] = _root.tilePlant (_root.a1[1],1,11),_root.plant1[4];";
    and call the sender?

    Does that make sense? Or am I a babbling fool?
    Last edited by mrgrim333; 11-30-2009 at 08:07 PM.
    Man, I need a new signature.

  2. #2
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    create some objects, you can store information like that.
    Code:
    var plant //whatever plant is
    
    var f1 = {};
    
    f1.func = _root.tilePlant;
    f1.args = [plant, 1, 11, _root.plant1[4]];
    
    _root.a1[1] = f1;
    then you can call the function like so:
    Code:
    _root.a1[1].func.apply(_root.a1[1].args, this);
    lather yourself up with soap - soap arcade

  3. #3
    It's not 1997 ?! mrgrim333's Avatar
    Join Date
    Feb 2004
    Posts
    254
    So I need _root.a1[1] to be dynamic too.
    Would I just do like
    Code:
    var plant //whatever plant is
    
    var f1 = {};
    
    f1.func = _root.tilePlant;
    f1.args = [plant, 1, 11, _root.plant1[4]];
    f1.base = _root.a1[1];
    and call
    Code:
    base.func.apply(base.args, this);
    Sorry if I'm not getting it, oop is a newer concept to me
    Last edited by mrgrim333; 11-30-2009 at 08:14 PM.
    Man, I need a new signature.

  4. #4
    M.D. mr_malee's Avatar
    Join Date
    Dec 2002
    Location
    Shelter
    Posts
    4,140
    what you should be doing is storing function calls (not actual calls themselves, but the components of the call i.e the function and the arguments) in an array then sending that data to a button when you need it.

    the object "f1" represents a function call, maybe call it something like "call1". So this is how we would create our first call object
    Code:
    var call1 = {} //create a new object
    
    call1.func = _root.tilePlant //pointer to the function we want to run
    call1.args = [something, something else, something else again] //an array of arguments which gets passed to the function
    thats it, thats all a function call needs to know, if you want extra data you can add it if you wish, just like you had above:
    Code:
    call1.base = _root.a1[1];
    but its not necessary for the call. You would then add this call to an array of other calls.
    Code:
    calls = new Array();
    
    calls.push(call1); //add the call to an array
    now we have an array with 1 call, you could now add your 25 other calls in the same way:
    Code:
    calls.push(call2);
    calls.push(call3);
    calls.push(call4);
    etc. However you want to do it, it doesn't matter. Next you set a call to the button:
    Code:
    button.callObject = calls[0]; //give the button the first call
    button.onRelease = function() {
    
    this.callObject.func.apply(this.callObject.args, this); //call the function with the arguments
    
    //here we can do something with extra data in the callObject
    
    trace(callObject.base); //traces _root.a1[1];
    }
    lather yourself up with soap - soap arcade

  5. #5
    It's not 1997 ?! mrgrim333's Avatar
    Join Date
    Feb 2004
    Posts
    254
    PM'd with the file, so you can help a noobie.
    Man, I need a new signature.

  6. #6
    It's not 1997 ?! mrgrim333's Avatar
    Join Date
    Feb 2004
    Posts
    254
    [**SOLVED**]
    So the answer for this one was both Recursion and Pass by reference.
    Recursion to call the function from the function.
    Pass by reference because I never knew flash would let you pass entire arrays as parameters.

    Here's the final code.

    (on first frame)
    Code:
    function func(array,id){
       if(condition >= 3)
       {
    	 _root.button.onRelease = function() 
    	{
                    array[id] += 1;
    		_root.func (array,id);
                    
    	}
       }
    }
    (to call function)
    Code:
    on (release) 
    {
    		_root.func (_root.myArray,1,0);
    }
    Last edited by mrgrim333; 12-03-2009 at 05:38 PM. Reason: Completely solve my problems :D
    Man, I need a new signature.

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