簡體   English   中英

AS3垂直射手:鼠標單擊不起作用

[英]AS3 vertical shooter: Mouse click not working

我只是在嘗試制作一個簡單的垂直射手,用鼠標控制玩家的飛船,並在單擊鼠標時發射激光。 但是,當我嘗試運行代碼時,我不斷收到相同的錯誤消息:

“ 1046:找不到類型或不是編譯時常量:MouseEvent。”

事情是,我宣布了MouseEvent。 我知道我做到了 如下:

-==-

package 

{

導入flash.display.MovieClip;

導入flash.utils.Timer;

導入flash.events.TimerEvent;

導入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日到期。有人可以幫助我嗎?

添加此import flash.events.MouseEvent ; 到班上的最高水平,它應該可以正常工作。 您需要導入您使用的所有內容。 這就是為什么您會收到該錯誤。

除了導入MouseEvent之外,在游戲初始化時,還應將事件偵聽器添加到偵聽MOUSE_DOWN和MOUSE_UP事件的階段:

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

但是,更簡單的方法是取消'mouseClick'變量,而只具有一個MouseEvent偵聽器,該偵聽器偵聽MOUSE_DOWN事件並觸發從處理程序中啟動導彈。

我有一個像這樣的游戲,如果您需要整個源,我可以給您,但是主要部分是

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM