ActionScript compilation error: The name of definition 'Main' does not reflect the location of this file
Can someone tell me what I am doing wrong with this?
I have a movie clip called turret that is on the screen and is instanced as Turret, I have a movie clip called bullet and that is in the library exported for AS "bullet" no quotes.
Here is the website http://wonderfl.net/c/du34
My frame (main) class is called du34
My code for du34 is:
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 =开发者_Go百科 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()
}
}
I keep getting the compiled error
5008: The name of definition 'Main' does not reflect the location of this file. Please change the definition's name inside this file, or rename the file.du34.as
Follow the advice in the error. Rename the Main
class inside the package definition to du34
(or whatever the AS3 file is called), or rename the file it is in to Main. The class and filename have to match:
ActionScript 3.0 allows you to include multiple classes in one source file, but only one class in each file can be made available to code that is external to that file. In other words, only one class in each file can be declared inside a package declaration. You must declare any additional classes outside your package definition, which makes those classes invisible to code outside that source file. The name of the class declared inside the package definition must match the name of the source file.
Do you need more semicolons ? It seems a lot of semicolons are missing. Perhaps changing the class name might help.
In actionscript under normal uses you need one class per "as" file.
The "as" file needs to be named the same as the class.
Class names should be named with a captial first letter ex:MyClass
All class files need to be included in a package.
A package structure needs to reflex the director it is in relation to the source fla/mxml files
In your sample code you supplied you have 5 classes and you need 5 "as" files
The following example assumes the package/class is in the same directory as your FLA
saved off as 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
}
}
}
you still need "as" files for BulletEvent, Bullet, Barrel, Turret
Try this.. Rename the Main class inside the package definition to du34. In the directory (same location with .fla) create folder name du34 and put du34.as into it.
精彩评论