函数的参数变量作为对象
问题描述:
有没有办法/我想知道如何动态地使用变量。 我的想法是让一个movieclip在调用一个函数的同时传递一个参数。 然后将参数值传递给更多脚本的函数。函数的参数变量作为对象
如何使用函数的参数变量,使用它的值作为instanceName.method在函数中执行?
示例代码,我有两个movieclip实例名称,box1和box2。 有一次,任何的movieclip按下,功能将执行。
// movieClip, instance name : box1
on(press){
_root.functionName(this._name);
}
on(release){ stopDrag(); }
- 的动画片段,实例名称:BOX2,具有相同的代码。
- 有身份被传递给函数通过“this._name”
最后,这里是该函数的代码。我跟踪“b”以了解其价值。 添加了startDrag()以便能够拖动动画片段。 “我怎么能够跟踪” - 拖动“+ b而拖动动画片段?”
预先感谢您。我曾想过使用这些代码。这是否允许?
function functionName(b){
trace(b+" selected.");
startDrag(b);
b.onMouseMove = function(){ // <---- please help
trace("--drag "+b);
}
}
该代码可能看起来简单而无用:)这不是真的我正在使用的代码。我只是想获得适当的行来在函数内执行onmousemove。
function functionName(param_var){
object.eventMethod = function() {
// some code here.
}
}
我可以使参数变量作为对象的使用?
答
我已经设法找到答案。感谢您查看我的帖子。
//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;
}
}
答
你应该建立不同的代码。这里是一个例子:
//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");
}
感谢这种全新的方式... 但我怎么能够处理box2,box3等? 我实际上需要的东西可以让我使用单一功能访问任何一个框。如果这是可能的。 函数onBoxPressed(/ *我可以在这里有参数* /):void * {/和在这里使用它?* /。startDrag(); /*和here?* /。onMouseMove = Delegate.create(this,onMouseMoved); } – 2012-08-03 14:01:08
是** variable.method **允许的,其中as变量是一个参数? – 2012-08-03 15:06:34
嗨,对于迟到的答案感到抱歉。我用你需要的东西编辑了我的答案。 – 2012-08-08 20:37:23