Actionscript 2:函数不会运行

时间:2011-05-19 15:48:43

标签: flash actionscript actionscript-2

我定义了一个函数并尝试运行但它不会运行,如果我从函数中取出代码它运行正常而我无法解决原因,这里是函数和调用...

function Boomclat() { 
        var TweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Strong.easeOut, this._x, 16.9, 1, true);
        var TweenY:Tween = new Tween(this, "_y", mx.transitions.easing.Strong.easeOut, this._y, listY, 1, true);
        }
        Boomclat();

这是我的整个代码(在这个MC上,还有更多的外部代码):

    onClipEvent (load) {
    import mx.transitions.Tween;
    import mx.transitions.easing.*;
    startX = this._x;
    startY = this._y;
    mn = 0;
}
onClipEvent (enterFrame) {
    this.onRollOver = function() {
        this.gotoAndStop("over");
    };
    this.onRollOut = function() {
        this.gotoAndStop("up");
    };
    this.onPress = function() {
        this.gotoAndStop("down");
    };
    this.onReleaseOutside = function() {
        this.gotoAndStop("up");
    };
    this.onRelease = function() {
        this.gotoAndStop("up");
        this.enabled = false;
        this.arrow.gotoAndStop("ordered");
        if (_global.hasAnswered != 1) {
            this.arrow.listNumber = this._parent.Order.length+1;
            listY = 35+(74.9*this._parent.Order.length);
            this._parent.Order.push(this);
            function Boomclat() { 
            var TweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Strong.easeOut, this._x, 16.9, 1, true);
            var TweenY:Tween = new Tween(this, "_y", mx.transitions.easing.Strong.easeOut, this._y, listY, 1, true);
            };
            Boomclat();
            this._parent.buttonHolder.previousOrder.push(this);
        } else {
            for (i=0; i<this._parent.trueOrder.length; i++) {
                if (this == this._parent.trueOrder[i]) {
                    _global.previousButton.enabled = true;
                    _global.previousButton.gotoAndStop("up");
                    myColor = new Color(_global.previousButton);
                    myColor.setTint(255,255,255,0);
                    myColor = new Color(this);
                    myColor.setTint(113,205,0,23);
                    this.gotoAndStop("down");
                    var TweenX:Tween = new Tween(_global.lastText, "_x", mx.transitions.easing.Back.easeOut, 276.4, -210, 0.7, true);
                    _global.whichText = this._parent.textFile[i];
                    var TweenX:Tween = new Tween(_global.whichText, "_x", mx.transitions.easing.Back.easeOut, 760, 276.4, 0.7, true);
                    _global.lastText = whichText;
                    _global.previousButton = this;
                }
            }
        }
    };

}

任何人都可以看到什么是错的?欢呼声。

3 个答案:

答案 0 :(得分:0)

AS2中最常见的错误原因之一是范围问题。也许this关键字不会引用您在Boomclat函数中预期的对象。您可以在函数中添加跟踪调用trace(this),以查看它是否是您想要补间的对象。

编辑:我现在看到Boomclat是在另一个函数onRelease处理程序中定义的。我不确定你能用那种确切的语法做到这一点。如果你想要嵌套函数,一种方法就是这样,将函数引用存储在局部变量中:

var boomclat:Function = function () { 
   var TweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Strong.easeOut, this._x, 16.9, 1, true);
   var TweenY:Tween = new Tween(this, "_y", mx.transitions.easing.Strong.easeOut, this._y, listY, 1, true);
};
boomclat();

或者你可以在onRelease处理程序之外的其他地方定义函数,而只是从onRelease处理程序调用它。

但是考虑到你发布的代码,我会说你也可以执行内联代码,我真的不明白为什么你需要那里的函数。

答案 1 :(得分:0)

为什么要循环Boomclat()函数?
很明显,补间效果将无法完成,甚至可能在您再次触发补间之前甚至无法启动,实质上是重置补间 [编辑]
Opps,出于某种原因,我认为函数调用是在函数内部。

无论如何,您是否验证了函数内部可以访问的变量。

function Boomclat() { 
  trace( this._x )
  trace( this._y _ )
  trace( listY )
  var TweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Strong.easeOut, this._x, 16.9, 1, true);
  var TweenY:Tween = new Tween(this, "_y", mx.transitions.easing.Strong.easeOut, this._y, listY, 1, true);
}
Boomclat();

[EDIT2]

//remove the  onClipEvent completely and you should be ok
    import mx.transitions.Tween;
    import mx.transitions.easing.*;

    startX = this._x;
    startY = this._y;
    this.onRollOver = function() {
        this.gotoAndStop("over");
    };
    this.onRollOut = function() {
        this.gotoAndStop("up");
    };
    this.onPress = function() {
        this.gotoAndStop("down");
    };
    this.onReleaseOutside = function() {
        this.gotoAndStop("up");
    };


// you might have to fix the references to this._parent to get it target what you want.
    this.onRelease = function() {
        this.gotoAndStop("up");
        this.enabled = false;
        this.arrow.gotoAndStop("ordered");
        if (_global.hasAnswered != 1) {
            this.arrow.listNumber = this._parent.Order.length+1;
            listY = 35+(74.9*this._parent.Order.length);
            this._parent.Order.push(this);
            orderTween();
            this._parent.buttonHolder.previousOrder.push(this);
        } else {
            for (i=0; i<this._parent.trueOrder.length; i++) {
                if (this == this._parent.trueOrder[i]) {
                    _global.previousButton.enabled = true;
                    _global.previousButton.gotoAndStop("up");
                    myColor = new Color(_global.previousButton);
                    myColor.setTint(255,255,255,0);
                    myColor = new Color(this);
                    myColor.setTint(113,205,0,23);
                    this.gotoAndStop("down");
                    var TweenX:Tween = new Tween(_global.lastText, "_x", mx.transitions.easing.Back.easeOut, 276.4, -210, 0.7, true);
                    _global.whichText = this._parent.textFile[i];
                    var TweenX:Tween = new Tween(_global.whichText, "_x", mx.transitions.easing.Back.easeOut, 760, 276.4, 0.7, true);
                    _global.lastText = whichText;
                    _global.previousButton = this;
                }
            }
        }
    };

// as you can see I put it last
    function orderTween() {
        var TweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Strong.easeOut, this._x, 16.9, 1, true);
        var TweenY:Tween = new Tween(this, "_y", mx.transitions.easing.Strong.easeOut, this._y, listY, 1, true);
    }

答案 2 :(得分:0)

奇怪的是,如果我在onRelease之前定义函数它的工作原理......这里是最终代码:

onClipEvent (load) {
    import mx.transitions.Tween;
    import mx.transitions.easing.*;

    function orderTween() {
        var TweenX:Tween = new Tween(this, "_x", mx.transitions.easing.Strong.easeOut, this._x, 16.9, 1, true);
        var TweenY:Tween = new Tween(this, "_y", mx.transitions.easing.Strong.easeOut, this._y, listY, 1, true);
    }
    startX = this._x;
    startY = this._y;
    this.onRollOver = function() {
        this.gotoAndStop("over");
    };
    this.onRollOut = function() {
        this.gotoAndStop("up");
    };
    this.onPress = function() {
        this.gotoAndStop("down");
    };
    this.onReleaseOutside = function() {
        this.gotoAndStop("up");
    };
    this.onRelease = function() {
        this.gotoAndStop("up");
        this.enabled = false;
        this.arrow.gotoAndStop("ordered");
        if (_global.hasAnswered != 1) {
            this.arrow.listNumber = this._parent.Order.length+1;
            listY = 35+(74.9*this._parent.Order.length);
            this._parent.Order.push(this);
            orderTween();
            this._parent.buttonHolder.previousOrder.push(this);
        } else {
            for (i=0; i<this._parent.trueOrder.length; i++) {
                if (this == this._parent.trueOrder[i]) {
                    _global.previousButton.enabled = true;
                    _global.previousButton.gotoAndStop("up");
                    myColor = new Color(_global.previousButton);
                    myColor.setTint(255,255,255,0);
                    myColor = new Color(this);
                    myColor.setTint(113,205,0,23);
                    this.gotoAndStop("down");
                    var TweenX:Tween = new Tween(_global.lastText, "_x", mx.transitions.easing.Back.easeOut, 276.4, -210, 0.7, true);
                    _global.whichText = this._parent.textFile[i];
                    var TweenX:Tween = new Tween(_global.whichText, "_x", mx.transitions.easing.Back.easeOut, 760, 276.4, 0.7, true);
                    _global.lastText = whichText;
                    _global.previousButton = this;
                }
            }
        }
    };

}