0

好的,首先让我说这是我的第一个 AS3 项目。我对 Java 没问题,所以 OOP 和继承对我来说并不陌生。我确实花了几个小时坐在这里思考为什么我的代码没有像我想要的那样工作。我从一个试图发射子弹的炮塔开始。然而,子弹并没有被绘制到屏幕上,即使 x 和 y 坐标仍在变化。创建子弹/弹丸的方法称为 OnMouseClick。这是我的代码:

public class Main extends Sprite
{
    public var projectileArr : Array = new Array(); //array which the projectile objects are stored
    public var projCount : Number = 0; //projectile counter

    public var curX:Number; //mouse coords
    public var curY:Number;



    private var tri1:Sprite = new Sprite();
    private var tri1Height:Number = 50;

    private var rect1:Sprite = new Sprite();

    private var rect1W:Number = 2;
    private var rect1H:Number = 10;

    private var angle:Number = 0;
    private var acceleration:Number = 0;


    public var triSpeedX:Number = 0;
    public var triSpeedY:Number = 0;

    public var turretAngle:Number ;

    //keyboard booleans
    private var leftDown:Boolean = false, rightDown:Boolean = false, upDown:Boolean = false, downDown:Boolean = false;

    public function Main():void
    {
        if (stage)
            init();
        else
            addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        // entry point  

        //draw triangle
        addChild(tri1);

        tri1.graphics.lineStyle(1, 0xff00ff00);
        tri1.graphics.beginFill(0xff0000);
        tri1.graphics.moveTo(0, -tri1Height / 2)
        tri1.graphics.lineTo(tri1Height / 3, +tri1Height / 2);
        tri1.graphics.lineTo(-tri1Height / 3, +tri1Height / 2);
        tri1.graphics.endFill();

        tri1.x = 400;
        tri1.y = 300;

        //draw turret
        addChild(rect1);

        rect1.graphics.beginFill(0xFFFFFF);
        rect1.graphics.moveTo(rect1W / 2, -rect1H);
        rect1.graphics.lineTo(rect1W / 2, 0);
        rect1.graphics.lineTo(-rect1W / 2, 0);
        rect1.graphics.lineTo(-rect1W / 2, -rect1H);
        rect1.graphics.endFill();

        rect1.x = tri1.x;
        rect1.y = tri1.y;

        stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);

        stage.addEventListener(Event.ENTER_FRAME, Run);
        stage.addEventListener(MouseEvent.MOUSE_MOVE, getMouseCoord);
        stage.addEventListener(MouseEvent.MOUSE_DOWN, OnMouseClick);
    }

            //creation of projectile on click
    public function OnMouseClick (e:MouseEvent):void 
    {
        projectileArr[projCount] = new Projectile (rect1.x, rect1.y, turretAngle); //create a new projectile object
        addChild(projectileArr [projCount]); //... then it doesn't display
        projCount++;
        trace ("BAM");


    }
    public function getMouseCoord(e:MouseEvent):void
    {

        curX = mouseX;
        curY = mouseY;

    }

    public function onUp(e:KeyboardEvent):void
    {

        if (e.keyCode == Keyboard.LEFT)
            leftDown = false;
        else if (e.keyCode == Keyboard.RIGHT)
            rightDown = false;

        if (e.keyCode == Keyboard.UP)
            upDown = false;
        else if (e.keyCode == Keyboard.DOWN)
            downDown = false;

    }

    public function onDown(e:KeyboardEvent):void
    {
        if (e.keyCode == Keyboard.LEFT)
            leftDown = true;
        else if (e.keyCode == Keyboard.RIGHT)
            rightDown = true;

        if (e.keyCode == Keyboard.UP)
            upDown = true;
        else if (e.keyCode == Keyboard.DOWN)
            downDown = true;
    }

    public function addProjToScreen (projectile:Projectile) : void
    {
        addChild(projectile);
    }
    private function Run(e:Event):void
    {
        acceleration = 0; //resets acceleration back to 0 to limit speed
        if (leftDown)
        {   
            angle -= 5;
        }
        else if (rightDown)
        {
            angle += 5;
        }

        if (upDown)
        {
            acceleration += 3;
        }
        else if (downDown)
        {
            acceleration -= 3;
        }

        triSpeedX += acceleration * Math.cos(angle * Math.PI / 180);
        triSpeedY += acceleration * Math.sin(angle * Math.PI / 180);

        tri1.x += triSpeedX;
        tri1.y += triSpeedY;

        //borders
        if (tri1.x < 0)
        {

            triSpeedX *= -.5;
            tri1.x = 0 ;
        }

        if (tri1.x > 800 )
        {
            tri1.x = 800;
            triSpeedX *= -.5;
        }

        if (tri1.y < 0)
        {

            triSpeedY *= -.5;
            tri1.y = 0 ;
        }
        if (tri1.y > 600)
        {
            triSpeedY *= -.5;
            tri1.y = 600;
        }

        //friction
        triSpeedX *= 0.95
        triSpeedY *= 0.95


        rect1.x = tri1.x;
        rect1.y = tri1.y;


        var xCurDist:Number = curX - rect1.x;
        var yCurDist:Number = curY - rect1.y;

        turretAngle = Math.atan(yCurDist / xCurDist) * 180 / Math.PI ;

        if (xCurDist < 0  )
        {
            turretAngle += 180;
        }
        if (xCurDist > 0 && yCurDist < 0 )
        {
            turretAngle += 360;
        }

        rect1.rotation= turretAngle + 90; // + 90 to account for the sprite originally pointing up, which is 270 degrees ccw

        tri1.rotation = angle + 90;

        for (var i:int = 0 ; i < projCount ; i ++)
        {
            projectileArr [i].move();
        }

    }

}

和 Projectile 类:

public class Projectile extends MovieClip
{

    private var projectile : Sprite = new Sprite();

    private var speed: int = 10;
    private var angle :Number;

    private var xIncr: Number;
    private var yIncr: Number;

    public function Projectile(x:Number, y:Number, angle:Number) 
    {
        this.x = x;
        this.y = y;

        this.angle = angle;

        projectile.graphics.beginFill (0xFF0000);
        projectile.graphics.drawCircle (x, y, 4);
        projectile.graphics.endFill() ;
        xIncr = speed * Math.cos (angle); // angle needs to be converted to radians
        yIncr = speed * Math.sin (angle);
    }

    public function move () :void
    {
        trace (this.x + "  " + this.y);

        this.x += xIncr;
        this.y += yIncr;        

    }
  }

我只是不知道我错过了什么......我正在为我所有新创建的对象调用 addChild,还有什么可做的?

4

1 回答 1

2

您没有在构造函数中将精灵弹丸添加到 Projectile 的显示列表中。

addChild(projectile);

此外,您在射弹类中将 x 和 y 坐标加倍。projectile.graphics.drawCircle (x, y, 4);通过更改为来纠正此问题projectile.graphics.drawCircle (0, 0, 4);

于 2013-04-24T03:35:24.827 回答