AS3 import class not working

2019-09-07 03:11发布

问题:

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.

回答1:

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
            ...