AS3导入类不起作用

时间:2016-05-08 04:47:12

标签: class actionscript-3 flash

我迷路了。对于我的生活,我似乎无法创建和导入我自己的课程。我已经阅读了很多帖子,甚至还有关于教程(已经工作正常),但现在这是我原创的作品,没有骰子!

因此,名为GeoMath的.as文件包含我要导入的类,如下所示:

package {

public class GeoMath {

    public function GeoMath() {
        // Get Distance Between 2 points.
        public function distance(x1: Number, x2: Number, y1: Number, y2: Number): Number {
            var d: Number;
            d = Math.pow(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2), .5);
            return d;
        }... etc.

此.as文件与我的.fla文件位于同一文件夹中。顺便说一下,它还有另外两个.as文件,它们有Sprite个扩展,我正在成功实例化。好的,现在是主要班级:

package {

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.events.*;

import GeoMath; // C:\Users\User\Desktop\oldProjects\oldProject_1
    ...
public class Document extends MovieClip {
    ...    
    mouseVel = distance(me.stageX, mouseLastX, me.stageY, mouseLastY); 

...Document.as, Line 174, Column 17 1180: Call to a possibly undefined method distance.

我尝试将名为'computation'的文件夹放在与.fla相同的文件夹中,然后将GeoMath.as放在THAT文件夹中,然后在GeoMath.as文件中执行此操作:

package calculations {
...

在主要课堂上这样做:

import calculations.GeoMath

但这已经返回了相同的结果。我是盲人还是傻瓜?谢谢参观。我非常感激,因为我现在已经流出了眼睛。

1 个答案:

答案 0 :(得分:4)

如果要在不实例化GeoMath副本的情况下调用该函数,则需要将其设置为静态。

$scope

并且函数调用将更改为...

public static function distance(x1: Number, x2: Number, y1: Number, y2: Number): Number {

然而,这还有其他后果。因此,请确保实例化该类并调用实例的方法。

mouseVel = GeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);

对于您的类导入,您有正确的想法,但您可以通过添加以下路径来更改类的位置:var myGeoMath:GeoMath = new GeoMath(); mouseVel = myGeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY); > Advanced ActionScript 3.0 Settings。从那里,点击加号并添加基本路径。

example of AS3 settings panel

您会注意到默认路径只是Source path,这可确保.搜索同一文件夹中的类。如果您想要上一个目录,可以使用.fla的相对路径,就像在网络上一样。在任何情况下,都会从该位置搜索您的../

例如,namespace.package.class实际上是2个文件夹和flash.display.MovieClip文件格式:

.as

将类保存在自己的命名空间中是明智的(因为它有助于防止冲突)。您的软件包可能看起来像SOURCE PATH ↪ flash/ ↪ display/ ↪ MovieClip.as Shape.as Sprite.as ... ,而您的课程将是package nealdavis.calculations {。然后你需要一个类似的文件夹结构...

public class GeoMath {
相关问题