0

Is there a way / I would like to know how to use a variable dynamically. My idea is to have a movieclip calling a function at the same time passing a parameter. The parameter value will then be pass to the function for more script.

How to make use of the function's parameter variable, use its value to execute as instanceName.method with in the function?

Sample Code, I have two movieclip with instance names, box1 and box2. once, any of the movieclip is press, the function will execute.

// movieClip, instance name : box1
on(press){
    _root.functionName(this._name);
}
on(release){ stopDrag(); }
  • movieClip, instance name : box2, has the same code.
  • there identity is being passed to the function through "this._name"

Finally, here is the code for the function. I trace "b" in order to know its value. Added startDrag() to be able to drag the movieclip. "How can I able to trace "--drag "+b while dragging the movieclip?"

Thank you in advance. I have thought of using this code. Is this allowed?

function functionName(b){

    trace(b+" selected.");
    startDrag(b);

    b.onMouseMove = function(){        // <---- please help
        trace("--drag "+b);
    }   
}

The code may look simple and useless :) This is not really the code I am working with. I am just trying to get the proper line to execute onmousemove inside a function.

function functionName(param_var){    
    object.eventMethod = function () {
        // some code here.
    }
}

CAN I MAKE USE OF THE PARAMETER VARIABLE AS OBJECT?

4

2 回答 2

0

您可能应该以不同的方式构建代码。这是一个例子:

//Import Delegate
import mx.utils.Delegate;

//First you set the event with Relegate which will prevent from Flash scope problems
box1.onPress = Delegate.create(this, onBoxPressed);
box1.onPress.currentBox = box1;
box1.onRelease = Delegate.create(this, onBoxReleased);
box1.onRelease.currentBox = box1
box2.onPress = Delegate.create(this, onBoxPressed);
box2.onPress.currentBox = box2;
box2.onRelease = Delegate.create(this, onBoxReleased);
box2.onRelease.currentBox = box2;

//Function executed when the box is pressed
function onBoxPressed():Void {
    var box:MovieClip = arguments.caller.currentBox;
    box.startDrag();
    box.onMouseMove = Delegate.create(this, onMouseMoved);
}

//Function executed when the box is released
function onBoxReleased():Void {
    var box:MovieClip = arguments.caller.currentBox;
    delete box.onMouseMove;
    box.stopDrag();
}

//Function executed when the box is pressed and the mouse moves
function onMouseMoved():Void {
    trace("I'm moving my box");
}
于 2012-07-31T21:39:20.153 回答
0

我设法找到了答案。感谢您查看我的帖子。

//Having function that would dynamically manage parameters into object
//given that the object is found in the ROOT.

function myFunction(param_var) {
    // _root[param_var] => can be use as an Object regards to the parameter
    _root[param_var].eventMehtod= function(){    
        // some code here;
    } 
}
于 2012-08-10T08:44:04.680 回答