我只是想製作一個簡單的垂直射擊遊戲,其中一個玩家的船由鼠標控制,當你點擊鼠標時發射激光。但是,當我嘗試運行代碼時,我不斷收到相同的錯誤消息: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日到期的。有人可以幫助我嗎?
那麼,對於你的幫助謝謝我有這樣的比賽。我添加了導入flash.events.MouseEvent,並將「MouseEvents」的「addEventListender」更改爲「stage」。在開始... – user3321704
...現在我得到一個全新的錯誤: 行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
我以爲我定義了這些屬性。我應該做些什麼...? – user3321704