AS3 导入 class 不工作
AS3 import class not working
我迷路了。对于我的生活,我似乎无法创建和导入我自己的 class。我已经阅读了很多帖子和关于它的文章,甚至还遵循了教程(效果很好),但现在这都是我的原创作品,没有骰子!
因此,我的名为 GeoMath 的 .as 文件包含我想要导入的 class,如下所示:
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
扩展名,我正在成功实例化它们。好的,现在主要 class:
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.
我试过将一个名为 'calculations' 的文件夹放在与 .fla 相同的文件夹中,然后将 GeoMath.as 放在该文件夹中,然后在 GeoMath.as 文件中执行此操作:
package calculations {
...
主要class这样做:
import calculations.GeoMath
但这返回了相同的结果。我是瞎了还是傻了?谢谢参观。不胜感激,因为我现在眼睛都快流血了。
如果要在不实例化 GeoMath 副本的情况下调用函数,则需要将其设为静态。
public static function distance(x1: Number, x2: Number, y1: Number, y2: Number): Number {
并且函数调用将更改为...
mouseVel = GeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);
但是,这还有其他影响。因此,请确保实例化 class 并调用实例的方法。
var myGeoMath:GeoMath = new GeoMath();
mouseVel = myGeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);
至于您的 class 导入,您的想法是正确的,尽管您可以通过添加以下路径来更改 classes 的位置:Advanced ActionScript 3.0 Settings
> Source path
.从那里,点击加号并添加基本路径。
您会注意到默认路径只是 .
,这确保 .fla
在同一文件夹中搜索 classes。如果你想上一个目录,你可以使用 ../
的相对路径,就像在 web 上一样。在任何情况下,它都会从该位置搜索您的 namespace.package.class
例如,flash.display.MovieClip
字面上是 2 个文件夹和一个 .as
格式的文件:
SOURCE PATH
↪ flash/
↪ display/
↪ MovieClip.as
Shape.as
Sprite.as
...
将您的 classes 保留在您自己的命名空间中是明智的(因为它有助于防止冲突)。您的包裹可能看起来像 package nealdavis.calculations {
,而您的 class 可能是 public class GeoMath {
。然后你需要一个类似的文件夹结构...
SOURCE PATH
document.fla
↪ nealdavis/
↪ calculations/
↪ GeoMath.as
...
我迷路了。对于我的生活,我似乎无法创建和导入我自己的 class。我已经阅读了很多帖子和关于它的文章,甚至还遵循了教程(效果很好),但现在这都是我的原创作品,没有骰子!
因此,我的名为 GeoMath 的 .as 文件包含我想要导入的 class,如下所示:
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
扩展名,我正在成功实例化它们。好的,现在主要 class:
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.
我试过将一个名为 'calculations' 的文件夹放在与 .fla 相同的文件夹中,然后将 GeoMath.as 放在该文件夹中,然后在 GeoMath.as 文件中执行此操作:
package calculations {
...
主要class这样做:
import calculations.GeoMath
但这返回了相同的结果。我是瞎了还是傻了?谢谢参观。不胜感激,因为我现在眼睛都快流血了。
如果要在不实例化 GeoMath 副本的情况下调用函数,则需要将其设为静态。
public static function distance(x1: Number, x2: Number, y1: Number, y2: Number): Number {
并且函数调用将更改为...
mouseVel = GeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);
但是,这还有其他影响。因此,请确保实例化 class 并调用实例的方法。
var myGeoMath:GeoMath = new GeoMath();
mouseVel = myGeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);
至于您的 class 导入,您的想法是正确的,尽管您可以通过添加以下路径来更改 classes 的位置:Advanced ActionScript 3.0 Settings
> Source path
.从那里,点击加号并添加基本路径。
您会注意到默认路径只是 .
,这确保 .fla
在同一文件夹中搜索 classes。如果你想上一个目录,你可以使用 ../
的相对路径,就像在 web 上一样。在任何情况下,它都会从该位置搜索您的 namespace.package.class
例如,flash.display.MovieClip
字面上是 2 个文件夹和一个 .as
格式的文件:
SOURCE PATH
↪ flash/
↪ display/
↪ MovieClip.as
Shape.as
Sprite.as
...
将您的 classes 保留在您自己的命名空间中是明智的(因为它有助于防止冲突)。您的包裹可能看起来像 package nealdavis.calculations {
,而您的 class 可能是 public class GeoMath {
。然后你需要一个类似的文件夹结构...
SOURCE PATH
document.fla
↪ nealdavis/
↪ calculations/
↪ GeoMath.as
...