AS3 - 调用类之间的方法 [英] AS3 - Call a method between classes

查看:183
本文介绍了AS3 - 调用类之间的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我几乎全新的OOP的,我只是不知道如何从另一个调用一个方法在一个类中。我试图从一个自定义类调用的Main.as的方法,但它总是出现的召唤可能未定义的方法的错误,我不知道变通的办法。

I'm almost entirely new to OOP, and I just don't understand how to call a method in one class from another. I'm trying to call a method in the Main.as from a custom class, but it always comes up as the "Call to possibly undefined method" error, and I don't know a way around it.

Main.as code:

Main.as code:

    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 code:

TitleScreen.as code:

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

    }

}

}

我通常不提问在线,但我在我的绳子结束了。我完全难住了,和浏览教程和previous问题,网上只有进一步混淆了我。任何帮助将大大AP preciated。

I don't normally ask questions online, but I'm at the end of my rope here. I'm completely stumped, and browsing tutorials and previous questions online is only further confusing me. Any help would be greatly appreciated.

推荐答案

最简单的方法来做到这一点是有一个类引用另一个类。比方说,你有一个开关和灯泡。开关可以告诉灯泡转向灯打开和关闭。

The most simple way to do that is to have one class to reference another class. Let's say you have a switch and a bulb. The switch can tell the bulb to turn lights on and off.

下面是它如何可能看起来像一个简约的例子:

Here's a minimalistic example of how it could look like:

var bulbInstance:Bulb = new Bulb();

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

现在,你有你的开关类引用一个灯泡。从里面它可以调用公共方法和访问灯泡类的公共变量。 不要忘了做一个公共变量bulbReference在你的LightSwitch类。

now you have a bulb referenced in your switch class. Inside from it you can call public methods and access public variables of the Bulb class. Don't forget to make a public variable bulbReference in your LightSwitch class.

有很多方法来形成你code元素,打破强烈的依赖性​​,使您的code之间的相关性:可扩展性,可重用性和可维护性。要了解它寻找设计模式。有一个伟大的书上写关于这个话题: http://shop.oreilly.com/product/9780596528461 。做

There are many ways to form dependencies between elements of your code to break strong dependencies and make your code: scalable, reusable and maintainable. To learn about it look for Design Patterns. There is a great book written about this topic: http://shop.oreilly.com/product/9780596528461.do

但有关于这一主题的大量素材免费在互联网上。

But there's a lot of material on the topic free on the Internet.

这篇关于AS3 - 调用类之间的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆