AS3垂直射击游戏:鼠标点击不起作用

时间:2014-02-21 13:22:15

标签: actionscript-3 flash mouse

我只是想制作一个简单的垂直射击游戏,玩家的船只由鼠标控制并在你点击鼠标时发射激光。但是,当我尝试运行代码时,我不断收到相同的错误消息:

“1046:未找到类型或不是编译时常量:MouseEvent。”

问题是,我宣布了MouseEvent。我知道我做到了。它如下:

- == -

package 

{

import 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日到期。请有人帮帮我吗?

3 个答案:

答案 0 :(得分:0)

添加此import flash.events.MouseEvent;在你的班级的顶部,它应该工作。您需要导入您使用的所有内容。这就是你得到这个错误的原因。

答案 1 :(得分:0)

除了导入MouseEvent之外,在游戏初始化时,您应该将事件监听器添加到监听MOUSE_DOWN和MOUSE_UP事件的阶段:

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

更简单的方法是取消'mouseClick'变量并只有一个MouseEvent监听器,它监听MOUSE_DOWN事件并从处理程序触发你的missle启动。

答案 2 :(得分: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 CLASS

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
    }
}
}