【问题标题】:AS3 - Call a method between classesAS3 - 在类之间调用方法
【发布时间】:2014-01-30 09:10:24
【问题描述】:

我对 OOP 几乎是全新的,我只是不明白如何从另一个类调用一个类中的方法。我正在尝试从自定义类调用 Main.as 中的一个方法,但它总是以“调用可能未定义的方法”错误的形式出现,我不知道如何解决。

Main.as 代码:

    package {

import flash.display.MovieClip;

public class Main extends MovieClip {

    public var titleScreen:TitleScreen = new TitleScreen();
    public var feedMeShibe:FeedMeShibe = new FeedMeShibe; //These are exported
    public var milkbone:MilkboneItem = new MilkboneItem; //actionscript symbols
    public var openMouthArea:OpenMouthArea = new OpenMouthArea; //in the library

    public function Main() {
        titleScreen.x = 350;
        titleScreen.y = 250;
        addChild(titleScreen);
    }
    public function addShibe(shibeX:Number, shibeY:Number, shibeSize:Number):void {
        feedMeShibe.x = shibeX;
        feedMeShibe.y = shibeY;
        feedMeShibe.width = feedMeShibe.width*shibeSize;
        feedMeShibe.height = feedMeShibe.height*shibeSize;
        addChild(feedMeShibe);
    }

    public function addMilkbone(milkboneX:Number, milkboneY:Number, milkboneSize:Number):void {
        milkbone.x = milkboneX;
        milkbone.y = milkboneY;
        milkbone.width = milkbone.width*milkboneSize;
        milkbone.height = milkbone.height*milkboneSize;
        addChild(milkbone);
    }
    public function addOpenMouthArea(OMAX:Number, OMAY:Number, OMAW:Number, OMAH:Number):void {
        openMouthArea.x = OMAX;
        openMouthArea.y = OMAY;
        openMouthArea.width = OMAW;
        openMouthArea.height = OMAH;
        addChild(openMouthArea);
    }

}

}

TitleScreen.as 代码:

    package {

import flash.display.MovieClip;
import flash.events.Event;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import fl.transitions.TweenEvent;


public class TitleScreen extends MovieClip {


    public function TitleScreen() {
        createListeners();
    }
    private function createListeners():void {
        this.addEventListener(Event.ENTER_FRAME, stopFrame);
    }
    public function stopFrame(e:Event):void {
        if (this.currentFrame == 510){
            this.removeEventListener(Event.ENTER_FRAME, stopFrame);
            this.stop();
            addShibe(100, 100, 0.5); //How do I
            addMilkbone(50, 50, 0.6); //access the Main class
            addOpenMouthArea(200, 200, 1, 1); //with these?
        }

    }

}

}

我通常不会在网上提问,但我已经走到了尽头。我完全被难住了,在线浏览教程和以前的问题只会让我更加困惑。任何帮助将不胜感激。

【问题讨论】:

    标签: actionscript-3 class oop actionscript


    【解决方案1】:

    最简单的方法是让一个类引用另一个类。假设您有一个开关和一个灯泡。开关可以告诉灯泡打开和关闭灯。

    这是一个简约的示例:

    var bulbInstance:Bulb = new Bulb();
    
    var switchInstance:LightSwitch = new LightSwitch();
    switchInstance.bulbReference = bulbInstance;
    

    现在您的开关类中引用了一个灯泡。在其中,您可以调用公共方法并访问 Bulb 类的公共变量。 不要忘记在 LightSwitch 类中创建一个公共变量bulbReference。

    有许多方法可以在代码元素之间形成依赖关系,以打破强依赖关系并使代码:可扩展、可重用和可维护。要了解它,请查找设计模式。关于这个主题有一本很棒的书:http://shop.oreilly.com/product/9780596528461.do

    但是互联网上有很多关于这个主题的免费资料。

    【讨论】:

      【解决方案2】:

      如果我没记错的话,你要做的就是

      Main.addShibe(100, 100, 0.5); //How do I Main.addMilkbone(50, 50, 0.6); //access the Main class Main.addOpenMouthArea(200, 200, 1, 1); //with these?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多