2014-02-21 129 views
0

我只是想製作一個簡單的垂直射擊遊戲,其中一個玩家的船由鼠標控制,當你點擊鼠標時發射激光。但是,當我嘗試運行代碼時,我不斷收到相同的錯誤消息:AS3垂直射擊遊戲:鼠標點擊不起作用

「1046:未找到類型或不是編譯時常量:MouseEvent。」

事情是,我宣佈MouseEvent。我知道我做到了。其計算方法如下:

- == -

package 

{

進口的flash.display.MovieClip;

import flash.utils.Timer;

import flash.events.TimerEvent;

import flash.events.MouseEvent;

public class SpaceShooter_II extends MovieClip //The public class extends the class to a movie clip. 
{ 
    public var army:Array; //the Enemies will be part of this array. 
    ///* 
    //Laser Shots and Mouse clicks: 
    import flash.events.MouseEvent; 
    public var playerShot:Array; //the player's laser shots will fill this array. 
    public var mouseClick:Boolean; 
    stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseGoDown); 
    stage.addEventListener(MouseEvent.MOUSE_UP, mouseGoUp); 
    stage.addEventListener(Event.ENTER_FRAME, onTick); 
    //*/ 
    //Back to the rest of the code: 
    public var playerShip:PlayerShip; //This establishes a variable connected to the PlayerShip AS. 
    public var onScreen:GameScreen; //This establishes a variable that's connected to the GameScreen AS. 
    public var gameTimer:Timer; //This establishes a new variable known as gameTimer, connected to the timer utility. 

    ///* 
    //Functions connected to Shooting via mouse-clicks: 
    public function mouseGoDown(event:MouseEvent):void 
    { 
     mouseClick = true; 
    } 

    public function mouseGoUp(event:MouseEvent):void 
    { 
     mouseClick = false; 
    } 
    //*/ 

    //This function contains the bulk of the game's components. 
    public function SpaceShooter_II() 
    { 
     //This initiates the GameScreen. 
     onScreen = new GameScreen; 
     addChild (onScreen); 

     //This sets up the enemy army. 
     army = new Array(); //sets the "army" as a NEW instance of array. 
     var newEnemy = new Enemy(100, -15); //This will create new enemies. There's new var newEnemy statement, hence we call THIS a var. 
     army.push (newEnemy); //the new enemy is added to the army. 
     addChild(newEnemy); //the new enemy is added to the game. 

     //This sets up the player's avatar, a spaceship. 
     playerShip = new PlayerShip(); //This invokes a new instance of the PlayerShip... 
     addChild(playerShip); //...And this adds it to the game. 
     playerShip.x = mouseX; //These two variables place the "playerShip" on-screen... 
     playerShip.y = mouseY; //...at the position of the mouse. 

     ///* 
     //This sets up the player's laser shots: 
     playerShot = new Array(); //sets the "army" as a NEW instance of array. 
     var goodShot = new goodLaser(playerShip.x, playerShip.y); //This will create new enemies. There's new var newEnemy statement, hence we call THIS a var. 
     playerShot.push (goodShot); //the new enemy is added to the army. 
     addChild(goodShot); //the new enemy is added to the game. 
     //*/ 

     //This sets up the gameTimer, where a lot of the action takes place. 
     gameTimer = new Timer(25); 
     gameTimer.addEventListener(TimerEvent.TIMER, onTick); 
     gameTimer.start(); 
    } 

    //This function contains the things that happen during the game (player movement, enemy swarms, etc.) 
    public function onTick(timerEvent:TimerEvent):void 
    { 
     //This "if" statement is where the array that contains the enemy ships is initialized. 
     if (Math.random() < 0.05) //This sets the number of ships showing up at once. 
     { 
      var randomX:Number = Math.random() * 800 //Generates a random number between 0 and 1. 
      var newEnemy:Enemy = new Enemy (randomX, -15); //This shows where the enemy starts out--at a random position on the X plane, but at a certain points on the Y plane. 
      army.push(newEnemy); //This adds the new enemy to the "army" Array. 
      addChild(newEnemy); //This makes the new enemy part of the game. 
     } 

     //This "for" statement sends the enemies downward on the screen. 
     for each (var enemy:Enemy in army) //Every time an enemy is added to the "army" array, it's sent downward. 
     { 
      enemy.moveDown(); //This is the part that sends the enemy downward. 

      //And now for the collision part--the part that establishes what happens if the enemy hits the player's spaceship: 
      if (playerShip.hitTestObject (enemy)) //If the playerShip makes contact with the enemy... 
      { 
       gameTimer.stop(); //This stops the game. 
       dispatchEvent(new PlayerEvent(PlayerEvent.BOOM)); //This triggers the game over screen in the PlayerEvent AS 

      } 
     } 

     //This, incidentally, is the player's movement controls: 
     playerShip.x = mouseX; 
     playerShip.y = mouseY; 

     ///* 
     //And this SHOULD be the shooting controls, if the mouse function would WORK... 
     if (mouseClick = true) 
     { 
      var goodShot = new goodLaser(playerShip.x, playerShip.y); //This will create new lasers. There's new variable in the statement, hence we call THIS a variable. 
      playerShot.push (goodShot); //the new laser is added to the army. 
      addChild(goodShot); //the new laser is added to the game. 
     } 

     for each (var goodlaser: goodLaser in goodShot) 
     { 
      goodlaser.beamGood(); 
     } 
     //*/ 
    } 

} 

}

- == -

很抱歉,如果括號內的不均勻來,我只是想勾勒出全部的代碼,並顯示我加入那裏的東西的零件開始出錯,所以有人可以告訴我我需要做些什麼來完成這項工作。

基本上,其他所有的工作......但是當我連接到鼠標點擊和激光陣列的工作,程序停止工作。該錯誤似乎與功能「mouseGoUp」和「mouseGoDown」相關,但我不知道如何解決該問題。

這項工作是3月8日到期的。有人可以幫助我嗎?

回答

0

加這個import flash.events.MouseEvent;到你班上的最高層,它應該工作。您需要導入您使用的所有內容。這就是你得到這個錯誤的原因。

0

除了進口的MouseEvent,在你的遊戲的初始化,您應該事件偵聽器添加到監聽MOUSE_DOWN和MOUSE_UP事件階段:

stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseGoDown); 
stage.addEventListener(MouseEvent.MOUSE_UP, mouseGoUp); 

就這麼簡單,不過,將去掉'mouseClick'變量,並且只有一個MouseEvent偵聽器來監聽MOUSE_DOWN事件並觸發你的處理程序啓動。

+0

那麼,對於你的幫助謝謝我有這樣的比賽。我添加了導入flash.events.MouseEvent,並將「MouseEvents」的「addEventListender」更改爲「stage」。在開始... – user3321704

+0

...現在我得到一個全新的錯誤: 行15 \t 1120:訪問未定義的屬性階段。 第15行\t 1120:訪問未定義的屬性mouseGoDown。 第16行\t 1120:訪問未定義的屬性階段。 第16行\t 1120:訪問未定義的屬性mouseGoUp。 第17行\t 1120:訪問未定義的屬性階段。 第17行\t 1120:訪問未定義的屬性事件。 第17行\t 1120:未定義屬性onTick的訪問。 – user3321704

+0

我以爲我定義了這些屬性。我應該做些什麼...? – user3321704

0

,如果你想整個源,我可以給你,但主要部分是

var delayCounter:int = 0; 
var mousePressed:Boolean = false; 
var delayMax:int = 5;//How rapid the fire is 
var playerSpeed:int = 5; 
var bulletList:Array = []; 
var bullet:Bullet; 

var upPressed:Boolean = false; 
var downPressed:Boolean = false; 
var leftPressed:Boolean = false; 
var rightPressed:Boolean = false; 

stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseDownHandler,false,0,true); 
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler,false,0,true); 
stage.addEventListener(Event.ENTER_FRAME,loop,false,0,true); 
stage.addEventListener(KeyboardEvent.KEY_DOWN, fl_SetKeyPressed); 
stage.addEventListener(KeyboardEvent.KEY_UP, fl_UnsetKeyPressed); 

function loop(e:Event):void 
{ 
    player.rotation = Math.atan2(mouseY - player.y,mouseX - player.x) * 180/Math.PI + 90; 

    if (bulletList.length > 0) 
    { 
     for each (bullet in bulletList) 
     { 
      bullet.bulletLoop(); 
     } 
    } 
    if (mousePressed) 
    { 
     delayCounter++; 
     if ((delayCounter == delayMax)) 
     { 
      shootBullet(); 
      delayCounter = 0; 
     } 
    } 

    if (upPressed) 
    { 
     player.y -= playerSpeed; 

    } 
    if (downPressed) 
    { 
     player.y += playerSpeed; 

    } 
    if (leftPressed) 
    { 
     player.x -= playerSpeed; 

    } 
    if (rightPressed) 
    { 
     player.x += playerSpeed; 

    } 
} 

function mouseDownHandler(e:MouseEvent):void 
{ 
    mousePressed = true; 
} 

function mouseUpHandler(e:MouseEvent):void 
{ 
    mousePressed = false; 
} 

function shootBullet():void 
{ 
var bullet:Bullet = new Bullet(stage,player.x,player.y,player.rotation - 90); 
bulletList.push(bullet); 
stage.addChildAt(bullet,1); 
} 

function fl_SetKeyPressed(event:KeyboardEvent):void 
{ 
if (event.keyCode == 87) 
{ 
    upPressed = true; 
} 
if (event.keyCode == 83) 
{ 
    downPressed = true; 
} 
if (event.keyCode == 65) 
{ 
    leftPressed = true; 
} 
if (event.keyCode == 68) 
{ 
    rightPressed = true; 
} 
} 

function fl_UnsetKeyPressed(event:KeyboardEvent):void 
{ 
if (event.keyCode == 87) 
{ 
    upPressed = false; 
} 
if (event.keyCode == 83) 
{ 
    downPressed = false; 
} 
if (event.keyCode == 65) 
{ 
    leftPressed = false; 
} 
if (event.keyCode == 68) 
{ 
    rightPressed = false; 
} 

} 

bullet類

package 
{ 
import flash.display.Stage; 
import flash.display.MovieClip; 
import flash.events.Event; 

public class Bullet extends MovieClip 
{ 
    private var stageRef:Stage; 
    private var speed:Number = 10;//speed that the bullet will travel at 
    private var xVel:Number = 5; 
    private var yVel:Number = 5; 
    private var rotationInRadians = 0; 


    public function Bullet(stageRef:Stage, X:int, Y:int, rotationInDegrees:Number):void 
    { 
     this.stageRef = stageRef; 
     this.x = X; 
     this.y = Y; 
     this.rotation = rotationInDegrees; 
     this.rotationInRadians = rotationInDegrees * Math.PI/180; 
    } 
    public function bulletLoop():void 
    { 
     xVel = Math.cos(rotationInRadians) * speed; 
     yVel = Math.sin(rotationInRadians) * speed; 
     x += xVel; 
     y += yVel; 
     if (x > stageRef.stageWidth || x < 0 || y > stageRef.stageHeight || y < 0) 
     { 
      deleteBullet(); 
     } 
    } 
    public function deleteBullet() 
    { 
     this.visible=false 
     this.x=9999 
     this.y=9999 
    } 
} 
} 
+0

爲了讓主要功能進入課堂,還是在主文檔中沒有包含主要功能? – user3321704

+0

這是在時間軸上,雖然不會很難轉換 – BennettLiam