2011-05-19 23 views
0

我定義了一個函數,並嘗試運行它,但它不會運行,如果我把代碼從函數運行良好,我不能解決爲什麼,這裏是在功能和來電...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; 
       } 
      } 
     } 
    }; 

} 

任何人都可以看到什麼是錯的?乾杯。

+1

這不是一個答案,但我認爲你不應該在enterFrame事件中有onRelease等的定義。你只需要運行這些代碼,enterFrame就會重複執行很多次(例如24 fps每秒24次)。 – 2011-05-19 15:56:11

+0

確實,他們認爲他們需要持續不斷地識別翻車等,顯然不是歡呼。 – 2011-05-19 15:59:10

回答

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; 
       } 
      } 
     } 
    }; 

} 
+0

正如Lars在下面所述,您有一個範圍界定問題,加上您正在定義onClipEvent內的鼠標事件以及導入語句。您需要從onClipEvent函數中提取這些函數。您發佈的代碼不是高質量的代碼(對於直言不諱)。使用作用域和「this」關鍵字時,ActionScript 2會非常棘手。 「這個」可以指許多東西,這取決於它被使用的地方。我建議你做一些谷歌搜索「委託」的方法,並學習它,如果你打算留在ActionScript 2中。 – 2011-05-25 22:53:56

+0

做一些閱讀。 http://www.actionscript.org/resources/articles/205/1/The-Delegate-Class/Page1.html – 2011-05-25 22:54:07

+0

這不是範圍,函數plain不會運行。函數內部的簡單跟蹤語句不會觸發。你將如何將進口擺脫onclipevent?語句必須出現在on/onclipevent處理程序中。它如上所示正常工作。 – 2011-05-26 08:54:58

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處理程序。

但是,考慮到你發佈的代碼,我會說你可以執行內聯代碼,我真的不明白爲什麼你需要在那裏的功能。

+0

它工作正常,沒有函數外的補間,函數根本沒有運行我可以把'trace(「hello」)'放入函數中,它不會輸出,所以函數永遠不會運行。 – 2011-05-19 16:03:43

0

你爲什麼要循環Boomclat()函數?
顯然間效果將無法完成,可能你會在本質上再次觸發補間重置吐溫
[編輯]
哎呀,由於某種原因,我想在函數調用前甚至沒有啓動是在函數內部。

無論如何,你有沒有驗證變量是否可以在函數內部訪問。

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); 
    } 
+0

它沒有循環。 – 2011-05-19 21:51:55