闪存AS3调用在一个单独的类从一个类中的函数 [英] Flash AS3 calling a function from a class within a seperate class

查看:175
本文介绍了闪存AS3调用在一个单独的类从一个类中的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找周围试图现在的工作该出去一会儿。有被各种想法雨后春笋般冒出来,就像则dispatchEvent等,但没有什么似乎是这样做的明确的,简单的方法。

I've been searching around trying to work this out for a while now. There's been various ideas popping up, like dispatchEvent etc but nothing that seems to be a clear, simple way of doing this.

我想要做的是调用从类单独的函数(但在同一文件夹中)我的文档类。 具体而言,我想'重生'的对象,并从我的Main.as.运行该对象的类内的一个函数 该片段我目前所面对的是如下:

What I'm trying to do is call a function from a class seperate (but in the same folder as) my document class. Specifically, I want to 'spawn' an object and run a function within that object's class from my Main.as. The snippet I have at the moment is as follows;

在Main.as:

var object:class_Object = new class_Object();
object.spawn();
addChild(object);

该菌种功能是在class_Object公共职能,但是当我尝试运行它,我得到的错误:错误#1006:产卵不是一个函数 如果我参加了'object.spawn();' C时$ C $添加对象就好了。

The spawn function is a public function within class_Object but when I try and run this, I get the error: 'Error #1006: spawn is not a function' If I take out the 'object.spawn();' code it adds the object fine.

我一定是失去了一些东西很明显,但不能找出

I must be missing something obvious, but can't figure out what

感谢

修改

修正了问题,我忘了指定的路径,类在我的图书馆对象的AS联动,以为我做到了!

Fixed the problem, I had forgotten to specify the path to the class in the AS linkage of my Library object, thought I'd done that!

推荐答案

您标志着方法静态。这意味着,它属于的的本身没有的实例的那个类的。

You marked that method as static. That means that it belongs to the class itself not instances of that class.

要调用方法的定义,你会说:

To invoke the method as defined you would say:

class_Object.spawn()

不过,这未必是何意,但很难没有看到实施的话。从定义中删除静态关键字将其附加到的实例的并且能够您所描述的方式。

However, this may not be what is intended, but it's difficult to say without seeing the implementation. Removing the static keyword from its definition will attach it to an instance and will be accessible the way you have described.

请参阅<一href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f30.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7f25"相对=nofollow>此处关于这一主题的土坯文档。

See here for the adobe documentation on the subject.

要回答你的意见,想想这是一个类是一个蓝图,共创实例的简单方法。

To answer your comment, a simple way to think about it is a "class" is a blueprint to create "instances".

所以,当你说新class_Object()你说的蓝图,以建设蓝图的新实例。在一侧的蓝图,你可以定义方法/属性,应该提供给实例(或实例方法)。此外,您也可以定义方法/属性可用的蓝图本身(或静态方法)。

So, when you say new class_Object() you telling the "blueprint" to "construct" a new instance of the blueprint. In side the blueprint you can define methods/properties that should available to the instance (or instance methods). Also, you can also define methods/properties that are available to the "blueprint" itself (or static methods).

因此​​,使用的一个经典的例子

So using the classic example of a Car

public class Car {

    public function startEngine():void {
        // This is an instance method, it will be available to
        // any instance of a car, or new Car();

        // Note: "this" in this context refers to the current instance of the car
        // that the method is being called from
    }

    public static function compare(Car car1, Car car2):bool {
        // This method belongs to the blueprint of a car

        // Note: "this" doesn't make any sense in this context, because we 
        // aren't talking about a particular instance.
    }
} 

再比如说:

var mercedes:Car = new Car();
var bmw:Car = new Car();

mercedes.startEngine(); // call an instance method. notice we call it from a particular instance of a car.
Car.compare(mercedes, bmw); // call an static method. notice we call it from the class of Car.

当你从另一个类扩展,你是借用从另一个类的功能,并添加/替换你自己的功能。但是,只要你有一个实例,如果它是一个公的方法,那么这将是外部可用在以往的实例可用。我不知道这是否有助于回答您的评论。

When you "extend" from another class, you are "borrowing" the functionality from another class and adding/replacing your own functionality. But as long as you have an instance, if it is a "public" method, then it will be externally available from where ever that instance is available. I don't know if that helps answer you comment.

这篇关于闪存AS3调用在一个单独的类从一个类中的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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