AS3 - 在类之间调用方法

时间:2014-01-11 09:07:35

标签: actionscript-3 class oop actionscript

我对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?
        }

    }

}

}

我通常不会在网上提问,但我在这里结束了。我完全被难倒了,在线浏览教程和以前的问题只会让我更加困惑。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

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

这是一个简洁的例子:

var bulbInstance:Bulb = new Bulb();

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

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

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

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

答案 1 :(得分:0)

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

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?