/ / Actionscript 2 मूवीक्लिप पर फ़ंक्शन को परिभाषित करने का उचित तरीका - एक्शनस्क्रिप्ट, एक्शनस्क्रिप्ट -2, मूवीक्लिप

एक्शनस्क्रिप्ट 2 मूवीक्लिप पर फ़ंक्शन को परिभाषित करने का सही तरीका - क्रियालेख, क्रियालेख-2, मूवीक्लिप

मैं मूवी क्लीप पर एक फ़ंक्शन लिखने की कोशिश कर रहा हूं, और इसे रूट क्लिप से कॉल करता हूं। एक्शनस्क्रिप्ट 3 में ठीक काम नहीं करता है "एक्शनस्क्रिप्ट 2 में ठीक से काम नहीं करता है।

का 1 फ्रेम _root मूवी क्लिप:

var newMovieClip:MovieClip = _root.attachMovie("Notification", id, 0);
newMovieClip.SetNotificationText("Test text");

का 1 फ्रेम Notification मूवी क्लिप:

function SetNotificationText(inputText : String){
notificationText.text = inputText;
}

नतीजा यह है कि मूवीक्लिप बनाया जाता है लेकिन टेक्स्ट नहीं बदला जाता है।

क्या मैं यह गलत कर रहा हूँ?

उत्तर:

जवाब के लिए 2 № 1

AS2 में MovieClip में फ़ंक्शंस जोड़ने के लिए, आपको इनमें से किसी एक विधि का उपयोग करना होगा:

  1. मूवीक्लिप के प्रोटोटाइप में विधि जोड़ें:

    MovieClip.prototype.SetNotificationText = function(inputText:String):Void
    {
    if(this["notificationText"] !== undefined)
    {
    // If we"re going to use the prototype, at least do some checks
    // to make sure the caller MovieClip has the text field we expect.
    this.notificationText.text = inputText;
    }
    }
    
    newMovieClip.SetNotificationText("Test text");
    
  2. मूवीक्लिप और फ़ंक्शन का तर्क दें:

    function SetNotificationText(mc:MovieClip, inputText:String):Void
    {
    mc.notificationText.text = inputText;
    }
    
    SetNotificationText(newMovieClip, "Test text");
    
  3. नए बनाए गए मूवीक्लिप में सीधे विधि जोड़ें:

    var newMovieClip:MovieClip = _root.attachMovie("Notification", id, 0);
    
    newMovieClip.SetNotificationText(inputText:String):Void
    {
    notificationText.text = inputText;
    }
    
    newMovieClip.SetNotificationText("Test text");
    

विकल्प 2 कुल मिलाकर सबसे अच्छा है - यह सबसे साफ और हैहर नए मूवीक्लिप के लिए एक नया फ़ंक्शन बनाने के ओवरहेड से बचा जाता है। यह प्रोटोटाइप के साथ खिलवाड़ करने से भी बचता है, जिसका उपयोग जेनेरिक विधियों को जोड़ने के लिए किया जाना चाहिए removeItem() विधि पर Array.