Actionscript 3 AS3垂直射手:鼠标单击不工作
我只是试着制作一个简单的垂直射击游戏,玩家的飞船由鼠标控制,当你点击鼠标时发射激光。但是,当我尝试运行代码时,我不断收到相同的错误消息: “1046:找不到类型或类型不是编译时常量:MouseEvent。” 问题是,我宣布了MouseEvent。我知道我做到了。详情如下: --==-- { 导入flash.display.MovieClip 导入flash.utils.Timer 导入flash.events.TimerEvent 导入flash.events.MouseEventActionscript 3 AS3垂直射手:鼠标单击不工作,actionscript-3,flash,mouse,Actionscript 3,Flash,Mouse,我只是试着制作一个简单的垂直射击游戏,玩家的飞船由鼠标控制,当你点击鼠标时发射激光。但是,当我尝试运行代码时,我不断收到相同的错误消息: “1046:找不到类型或类型不是编译时常量:MouseEvent。” 问题是,我宣布了MouseEvent。我知道我做到了。详情如下: --==-- { 导入flash.display.MovieClip 导入flash.utils.Timer 导入flash.events.TimerEvent 导入flash.events.MouseEvent public
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();
}
//*/
}
}
public class SpaceShooter_II扩展MovieClip//public类将该类扩展为电影剪辑。
{
public var army:Array;//敌人将成为此阵列的一部分。
///*
//激光拍摄和鼠标点击:
导入flash.events.MouseEvent;
public var playerShot:Array;//玩家的激光射击将填充此数组。
public var mouseClick:Boolean;
stage.addEventListener(MouseEvent.MOUSE_DOWN,mouseGoDown);
stage.addEventListener(MouseEvent.MOUSE_UP,mouseGoUp);
stage.addEventListener(Event.ENTER_FRAME,onTick);
//*/
//回到代码的其余部分:
public var playerShip:playerShip;//这将建立一个连接到playerShip AS的变量。
public var onScreen:GameScreen;//这将建立一个变量,该变量作为连接到GameScreen。
public var gameTimer:Timer;//这将建立一个名为gameTimer的新变量,该变量连接到计时器实用程序。
///*
//通过鼠标单击连接到拍摄的功能:
公共函数mouseGoDown(事件:MouseEvent):无效
{
mouseClick=true;
}
公共函数mouseGoUp(事件:MouseEvent):无效
{
mouseClick=false;
}
//*/
//此函数包含游戏的大部分组件。
公共功能太空射手II()
{
//这将启动游戏屏幕。
屏幕上=新游戏屏幕;
addChild(屏幕上);
//这就组建了敌军。
army=new Array();//将“army”设置为Array的新实例。
var new敌军=新敌军(100,-15);//这将创建新敌军。有一个新的var new敌军语句,因此我们称之为var。
(新敌人);//新敌人加入军队。
addChild(new敌军);//新敌军被添加到游戏中。
//这将设置玩家的化身,一艘宇宙飞船。
playerShip=new playerShip();//这将调用playerShip的新实例。。。
addChild(playerShip);/…这会将其添加到游戏中。
playerShip.x=mouseX;//这两个变量将“playerShip”放在屏幕上。。。
playerShip.y=mouseY;//…位于鼠标位置。
///*
//这将设置玩家的激光射击:
playerShot=new Array();//将“army”设置为Array的新实例。
var goodShot=new goodLaser(playerShip.x,playerShip.y);//这将创建新的敌人。有一个新的var new敌军语句,因此我们称之为var。
playerShot.push(好球);//新的敌人加入了军队。
addChild(goodShot);//游戏中添加了新的敌人。
//*/
//这将设置游戏计时器,其中会发生很多动作。
游戏计时器=新计时器(25);
gameTimer.addEventListener(TimerEvent.TIMER,onTick);
gameTimer.start();
}
//此功能包含游戏中发生的事情(玩家移动、敌人蜂群等)
公共函数onTick(timerEvent:timerEvent):无效
{
//这个“if”语句是包含敌舰的数组初始化的地方。
if(Math.random()<0.05)//设置一次出现的船只数量。
{
var randomX:Number=Math.random()*800//生成一个介于0和1之间的随机数。
var new敌手:敌手=新敌手(randomX,-15);//这显示了敌手的出发点——在X平面上的一个随机位置,但在Y平面上的某个点。
army.push(new敌军);//这会将新敌军添加到“army”阵列中。
addChild(新敌人);//这使新敌人成为游戏的一部分。
}
//这个“for”语句在屏幕上向下发送敌人。
for each(var敌军:陆军中的敌军)//每次将敌军添加到“陆军”阵列时,它都会向下发送。
{
敌人。向下移动();//这是使敌人向下移动的部分。
//现在是碰撞部分——确定如果敌人击中玩家的宇宙飞船会发生什么的部分:
if(playerShip.hitTestObject(敌人))//如果玩家与敌人接触。。。
{
gameTimer.stop();//这将停止游戏。
dispatchEvent(新建PlayerEvent(PlayerEvent.BOOM));//这会在PlayerEvent中的屏幕上触发游戏,如下所示
}
}
//顺便说一句,这是玩家的移动控制:
playerShip.x=鼠标;
playerShip.y=mouseY;
///*
//这应该是射击控制,如果鼠标功能正常的话。。。
if(mouseClick=true)
{
var goodShot=new goodLaser(playerShip.x,playerShip.y);//这将创建新的激光器。语句中有一个新变量,因此我们称之为变量。
playerShot.push(goodShot);//新的激光器被加入军队。
addChild(goodShot);//新的激光被添加到游戏中。
}
每个(var goodlaser:goodShot中的goodlaser)
{
goodlaser.beamGood();
}
//*/
}
}
}
--==--
很抱歉,如果括号不均匀,我只想概括代码的整体,并显示我添加的部分在哪里出错,以便有人可以告诉我需要做什么才能使这项工作
基本上
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();
}
//*/
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, mouseGoDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseGoUp);
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;
}
}
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
}
}
}