子弹没有出现在AS3中

时间:2013-06-04 19:38:46

标签: flash actionscript

我正在创造一个自上而下的射手和AS3,并且在敌人的子弹上遇到了一些麻烦。我正在使用与玩家相同的子弹类并进行一些更改,我试图让敌人定期向玩家发射子弹。

EnemyBullet课程:

package{

import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.geom.Point;
import Enemy;
import Ship;
import Game;

public class EnemyBullet extends MovieClip
{

    var speed:Number;
    var moveX:Number;
    var moveY:Number;
    var enemyX:Number;
    var enemyY:Number;
    var shipX:Number;
    var shipY:Number;

    function EnemyBullet()
    {
        speed = 20;
        shipX = Game.ship.x;
        shipY = Game.ship.y;
        //Bullet needs the Enemy's location it is being fired from\\
        /*enemyX = enemy.x;
        enemyY = enemy.y;*/


        graphics.lineStyle(7,0x000000);
        graphics.moveTo(-2,0);
        graphics.lineTo(2,0);

        graphics.lineStyle(3,0xffffff);
        graphics.moveTo(-2,0);
        graphics.lineTo(2,0);

        addEventListener(Event.ADDED_TO_STAGE, onStage);

    }

    function onStage (e:Event)
    {
        //Code for firing bullet at mouse - Kelsey 6/3/2013

        addEventListener("enterFrame", enterFrame);

        var cy:Number = Game.ship.y - enemyY;
        var cx:Number = Game.ship.x - enemyX;
        trace("bullet");


        var angle:Number = Math.atan2(cy, cx);
        moveX = Math.cos(angle);
        moveY = Math.sin(angle);
        rotation = angle * 180 / Math.PI;
    }

    function enterFrame(e:Event)
    {

        this.x += moveX * speed;

        this.y += moveY * speed;




        if (this.x > 900)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }
        if (this.x < 0)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }
        if (this.y > 800)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }
        if (this.y < 0)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }

        if(this.hitTestObject(Game.ship))
        {
            Game.ship.takeDamage(10);
            removeEventListener("enterFrame", enterFrame);
            stage.removeChild(this);
            trace("enemy hit");


        }
    }
}

}

敌人类:

package 
 {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
import EnemyBullet;

public class Enemy extends MovieClip
{
    var speed:Number;
    static var list:Array = new Array();
    var health:Number;
    var shotLimit:Number = 0;

    //shoot timer


    function Enemy()
    {
        list.push(this);
        this.x = Math.random() * 800;
        this.y = -150;
        speed = Math.random() * 5 + 5;
        addEventListener("enterFrame", enterFrame);
        health = 1;

        var timer:Timer = new Timer(1800,0);
        timer.addEventListener(TimerEvent.TIMER, tickTock);
        timer.start();


    }

    function enterFrame(e:Event)
    {
        this.y +=  speed;

        if (this.x < -100)
        {
            kill();
            return;
        }

        if (this.hitTestObject(Game.ship))
        {
            kill();
            Game.ship.takeDamage(20);
        }

        if (shotLimit > 4)
        {
            this.shootBullet();
            shotLimit = 0;
            trace("Enemy fire");
        }
    }


    function takeDamage(d)
    {
        health -=  d;
        if (health <= 0)
        {
            Game.updateScore(50);
            kill();
        }
    }

    function shootBullet()
    {
        var n = new EnemyBullet();
        stage.addChild(n);
        n.x = this.x;
        n.y = this.y;
    }

    function tickTock(t:TimerEvent)
    {
        shotLimit +=  1;
        trace(shotLimit);
    }

    function kill()
    {
        removeEventListener("enterFrame", enterFrame);
        var explosion = new Explosion();
        stage.addChild(explosion);
        explosion.x = this.x;
        explosion.y = this.y;



        stage.removeChild(this);
        for (var i in list)
        {
            if (list[i] == this)
            {
                delete list[i];
            }
        }
    }


}

}

和原始子弹类供参考:

package{
import flash.display.MovieClip;
import flash.events.Event;
import flash.ui.Mouse;
import flash.events.MouseEvent;
import flash.geom.Point;
import Ship;

public class Bullet extends MovieClip
{
    var speed:Number;
    var moveX:Number;
    var moveY:Number;
    var shipX:Number;
    var shipY:Number;

    function Bullet()
    {
        speed = 20;
        shipX = Game.ship.x;
        shipY = Game.ship.y;

        graphics.lineStyle(7,0x000000);
        graphics.moveTo(-2,0);
        graphics.lineTo(2,0);

        graphics.lineStyle(3,0xffffff);
        graphics.moveTo(-2,0);
        graphics.lineTo(2,0);

        addEventListener(Event.ADDED_TO_STAGE, onStage);

    }

    function onStage (e:Event)
    {
        //Code for firing bullet at mouse - Kelsey 6/3/2013

        addEventListener("enterFrame", enterFrame);

        var cy:Number = stage.mouseY - shipY;
        var cx:Number = stage.mouseX - shipX;


        var angle:Number = Math.atan2(cy, cx);
        moveX = Math.cos(angle);
        moveY = Math.sin(angle);
        rotation = angle * 180 / Math.PI;
    }
    function enterFrame(e:Event)
    {

        this.x += moveX * speed;

        this.y += moveY * speed;




        if (this.x > 900)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }
        if (this.x < 0)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }
        if (this.y > 800)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }
        if (this.y < 0)
        {
            removeEventListener("enterFrame",enterFrame);
            stage.removeChild(this);
        }

        for(var i in Enemy.list)
            {
                if(this.hitTestObject(Enemy.list[i]))
                {
                    removeEventListener("enterFrame", enterFrame);
                    stage.removeChild(this);
                    Enemy.list[i].kill();
                    break;
                }
            }
    }
}

}

当我运行程序时,“子弹”和“敌人火力”踪迹将会消失,但没有任何一个碰撞,并且我似乎无法正确添加敌人子弹,任何帮助都将受到赞赏!

0 个答案:

没有答案