ActionScript编译错误:定义“Main”的名称不反映此文件的位置

时间:2011-04-25 02:00:30

标签: actionscript-3

有人可以告诉我这件事我做错了吗?

我有一个名为炮塔的电影剪辑,它位于屏幕上并被设为Turret, 我有一个名为bullet的影片剪辑,它出现在AS“bullet”没有引号的库中。

这是网站http://wonderfl.net/c/du34

我的frame(main)类名为du34

我的du34代码是:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [SWF(width=1000, height=1000, framerate=24)]
    public class Main extends Sprite 
    {
        private static const SCREEN_WIDTH: int = 1000
        private static const SCREEN_HEIGHT: int = 1000

        private var turret: Turret = null
        private var me: MouseEvent = null
        private var trigger: Boolean = false

        private var bullets: Array = []

        public function Main():void 
        {
            graphics.beginFill(0x0)
            graphics.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
            graphics.endFill()

            turret = new Turret(SCREEN_WIDTH / 2, SCREEN_HEIGHT)
            turret.addEventListener(Turret.ADD_BULLET, onAddBullet)
            addChild(turret)

            addEventListener(Event.ENTER_FRAME, onEnterFrame)
            stage.addEventListener(MouseEvent.MOUSE_DOWN, function(): void { trigger = true } )
            stage.addEventListener(MouseEvent.MOUSE_UP, function(): void { trigger = false } )
        }

        private var nextAddBullet: Bullet = null;
        private function onAddBullet(e: BulletEvent): void {
            nextAddBullet = new Bullet(turret.x + e.pos, turret.y, mouseX, mouseY)
        }

        private var frameProcessing: Boolean = false

        private function onEnterFrame(e: Event): void {
            if (frameProcessing) return
            frameProcessing = true

            if (null != nextAddBullet) {
                bullets.push(nextAddBullet)
                addChild(nextAddBullet)
                nextAddBullet = null
            }

            turret.frameAction(trigger)
            var i: int
            for (i = 0; i < bullets.length; i++) {
                var bullet: Bullet = bullets[i]

                if (null != bullet) {
                    if (bullet.frameAction()) {
                        removeChild(bullet)
                        bullets[i] = null
                    }
                }
            }

            i = bullets.length
            while (0 < i--) {
                if (null == bullets[i]) bullets.splice(i, 1)
            }
            frameProcessing = false
        }
    }
}

import flash.events.Event
import flash.display.Sprite
import flash.geom.Point

class BulletEvent extends Event {

    private var _pos: int = 0
    public function get pos(): int { return _pos }

    public function BulletEvent(type:String, pos: int, bubbles:Boolean = false, cancelable:Boolean = false) {
        super(type, bubbles, cancelable);
        _pos = pos;
    }
}

// 弾丸
class Bullet extends Sprite {
    private var t: Point
    private var d: Point

    private static const BULLET_SIZE: int = 3
    public function Bullet(_x: int, _y: int, _tx: int, _ty: int): void {
        graphics.beginFill(0xFFFFFF)
        graphics.drawEllipse(-BULLET_SIZE/2, -BULLET_SIZE/2, BULLET_SIZE, BULLET_SIZE)
        graphics.endFill()

        x = _x
        y = _y
        t = new Point(_tx, _ty)
        d = t.subtract(new Point(x, y))
        d.normalize(10)
        frameAction()
    }

    public function frameAction(): Boolean {
        x += d.x
        y += d.y

        var cx: Boolean = (0 <= d.x) ? t.x <= x : x <= t.x;
        var cy: Boolean = (0 <= d.y) ? t.y <= y : y <= t.y;

        return (cx && cy)
    }
}

// 砲身
class Barrel extends Sprite {

    private static const BARREL_LENGTH: int = 20

    private var _pos: int = 0
    private var _loading: int = 0
    public function get pos(): int { return _pos }
    public function loading(): Boolean { return 0 < _loading }

    public function Barrel(pos: int = 0) { _pos = pos }

    public function frameAction(): void {
        if (0 < _loading) _loading -= 2

        var d: Point = new Point(mouseX, mouseY)
        d.normalize(BARREL_LENGTH - _loading)

        graphics.clear()
        graphics.lineStyle(2, 0xc0c0c0)
        graphics.moveTo(_pos, 0)
        graphics.lineTo(d.x + _pos, d.y)
    }

    public function shot(): void {
        _loading = 8
    }
}

// 砲塔
class Turret extends Sprite {
    public static const ADD_BULLET: String = "addBullet"

    private static const SIZE: int = 25

    private var barrels: Array = []
    private var actionIndex: int = 0
    private var _loading: int = 0

    public function Turret(_x: int, _y: int): void {
        x = _x
        y = _y

        barrels.push(new Barrel(-3))
        barrels.push(new Barrel(0))
        barrels.push(new Barrel(+3))
        for each (var barrel: Barrel in barrels) addChild(barrel)

        var armor: Sprite = new Sprite()
        with (addChild(armor)) {
            graphics.beginFill(0xe0e0e0)
            graphics.drawEllipse(-SIZE/2, -SIZE/2, SIZE, SIZE)
            graphics.endFill()
        }
    }

    public function loading(): Boolean { return 0 < _loading }

    public function shot(): void {
        if (loading()) return

        actionIndex = (actionIndex + 1) % barrels.length
        var barrel: Barrel = barrels[actionIndex]
        if (barrel.loading()) return
        barrel.shot()
        _loading = 3;

        dispatchEvent(new BulletEvent(ADD_BULLET, barrel.pos))
    }

    public function frameAction(trigger: Boolean): void {
        if (0 < _loading) _loading -= 1

        if (trigger) shot()
        for each (var barrel: Barrel in barrels) barrel.frameAction()
    }
}

我不断收到编译错误

  

5008:定义“Main”的名称不反映此文件的位置。请更改此文件中的定义名称,或重命名file.du34.as

4 个答案:

答案 0 :(得分:2)

按照错误中的建议操作。将包定义中的Main类重命名为du34(或调用任何AS3文件),或将其所在的文件重命名为Main。 The class and filename have to match

  

ActionScript 3.0允许您在一个源文件中包含多个类,但每个文件中只能有一个类可用于该文件外部的代码。换句话说,每个文件中只能在包声明中声明一个类。您必须在包定义之外声明任何其他类,这使得这些类对该源文件之外的代码不可见。 在包定义中声明的类的名称必须与源文件的名称匹配。

答案 1 :(得分:0)

你需要更多的分号吗?似乎很多分号都丢失了。 也许改变班级名称可能有所帮助。

答案 2 :(得分:0)

在正常使用的动作中,每个“as”文件需要一个类 “as”文件需要与该类命名相同 类名应使用首字母ex:MyClass
命名 所有类文件都需要包含在一个包中 包结构需要反映导向器与源fla / mxml文件的关系 在您提供的示例代码中,您有5个类,您需要5个“as”文件
以下示例假定包/类与FLA

位于同一目录中

保存为Main.as

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;

    [SWF(width=1000, height=1000, framerate=24)]
    public class Main extends Sprite
    {
        private static const SCREEN_WIDTH: int = 1000
        private static const SCREEN_HEIGHT: int = 1000

        private var turret: Turret = null
        private var me: MouseEvent = null
        private var trigger: Boolean = false

        private var bullets: Array = []

        public function Main():void
        {
            graphics.beginFill(0x0)
            graphics.drawRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)
            graphics.endFill()

            turret = new Turret(SCREEN_WIDTH / 2, SCREEN_HEIGHT)
            turret.addEventListener(Turret.ADD_BULLET, onAddBullet)
            addChild(turret)

            addEventListener(Event.ENTER_FRAME, onEnterFrame)
            stage.addEventListener(MouseEvent.MOUSE_DOWN, function(): void { trigger = true } )
            stage.addEventListener(MouseEvent.MOUSE_UP, function(): void { trigger = false } )
        }

        private var nextAddBullet: Bullet = null;
        private function onAddBullet(e: BulletEvent): void
        {
            nextAddBullet = new Bullet(turret.x + e.pos, turret.y, mouseX, mouseY)
        }

        private var frameProcessing: Boolean = false

        private function onEnterFrame(e: Event): void
        {
            if (frameProcessing) return
            frameProcessing = true

            if (null != nextAddBullet) {
                bullets.push(nextAddBullet)
                addChild(nextAddBullet)
                nextAddBullet = null
            }

            turret.frameAction(trigger)
            var i: int
            for (i = 0; i < bullets.length; i++) {
                var bullet: Bullet = bullets[i]

                if (null != bullet) {
                    if (bullet.frameAction()) {
                        removeChild(bullet)
                        bullets[i] = null
                    }
                }
            }

            i = bullets.length
            while (0 < i--) {
                if (null == bullets[i]) bullets.splice(i, 1)
            }
            frameProcessing = false
        }
    }
}

你还需要BulletEvent,Bullet,Barrel,Turret的“as”文件

答案 3 :(得分:0)

试试这个..将包定义中的Main类重命名为du34。在目录中(与.fla相同的位置)创建文件夹名称du34并将du34.as放入其中。