AS3导入类不工作 [英] AS3 import class not working
问题描述
我迷路了。对于我的生活我似乎不能创建和导入我自己的类。我已经阅读了这么多的帖子,并且甚至遵循教程(这已经很好),但现在,这是我所有的原始工作,没有骰子!
我的.as文件名为GeoMath,其中包含我想导入的类,如下所示:
包{
public class GeoMath {
public function GeoMath(){
//获取2点之间的距离。
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;
} ...等。
我的.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,第174行,第17列1180:到一个可能未定义的方法距离。
我试图将一个文件夹'计算'放在同一个文件夹.fla,然后将GeoMath.as放在THAT文件夹中,然后在GeoMath.as文件中执行此操作:
包计算{
...
并在主类中执行此操作:
导入计算.GeoMath
返回的结果相同。我是盲人还是只是愚蠢?感谢您看看。
如果你想调用的函数没有
公共静态函数距离(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);
至于你的类导入,你有正确的想法,虽然你可以改变你的类的位置通过在高级ActionScript 3.0设置
> 源路径
中添加路径。从那里,点击加号,添加基本路径。
你会注意到默认路径只是。
,这确保了 .fla
搜索用于同一文件夹中的类。如果你想上去一个目录,你可以使用相对路径 ../
,就像在网上。在任何情况下,从那个位置,它将搜索您的 namespace.package.class
例如, flash.display.MovieClip
确实是2个文件夹和以这种格式的 .as
文件:
SOURCE PATH
↪flash /
↪显示/
↪MovieClip.as
Shape.as
Sprite.as
...
将类放在自己的命名空间中是明智的(因为它有助于防止碰撞)。你的包可能看起来像 package nealdavis.calculations {
,你的类将是 public class GeoMath {
。您将需要一个类似的文件夹结构...
SOURCE PATH
document.fla $ b $b↪nealdavis /
↪计算/
↪GeoMath.as
...
I'm lost. For the life of me I can't seem to create and import my own class. I've read so many posts and on it and even followed tutorials (which have worked fine), but now that it's all my original work, no dice!
So, my .as file named GeoMath, which contains the class I'd like to import, looks like this:
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.
and this .as file is in the same folder as my .fla file. Incidentally, it also has another two .as files that have Sprite
extensions that I am successfully instantiating. Ok, now the main 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.
I have tried putting a folder called 'calculations' in the same folder as the .fla, and then putting GeoMath.as in THAT folder, and then doing this in the GeoMath.as file:
package calculations {
...
and in the main class doing this:
import calculations.GeoMath
but this has returned the same results. I'm I blind or just stupid? Thanks for taking a look. It will be much appreciated as I'm bleeding out of my eyes by now.
If you want to call the function without instantiating a copy of GeoMath, you need to make it static.
public static function distance(x1: Number, x2: Number, y1: Number, y2: Number): Number {
and the function call would change to...
mouseVel = GeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);
However, that has other ramifications. Therefore, make sure you instantiate the class and call the method of the instance.
var myGeoMath:GeoMath = new GeoMath();
mouseVel = myGeoMath.distance(me.stageX, mouseLastX, me.stageY, mouseLastY);
As for your class import, you have the right idea, though you can change the location of your classes by adding the path in: Advanced ActionScript 3.0 Settings
> Source path
. From there, hit the plus symbol and add the base path.
You'll notice that the default path is simply .
, which ensures the .fla
searches for classes in the same folder. If you want to go up a directory, you can use relative paths with ../
, just like on the web. In any case, from that location it will search for your namespace.package.class
For example, flash.display.MovieClip
is literally 2 folders and an .as
file in this format:
SOURCE PATH
↪ flash/
↪ display/
↪ MovieClip.as
Shape.as
Sprite.as
...
It's wise to keep your classes in your own namespace (as it helps prevent collisions). Your package may look like package nealdavis.calculations {
and your class would be public class GeoMath {
. You would then need a similar folder structure...
SOURCE PATH
document.fla
↪ nealdavis/
↪ calculations/
↪ GeoMath.as
...
这篇关于AS3导入类不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!