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();
        }

    }

}

および発射物クラス:

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 座標を 2 倍にしています。に変更projectile.graphics.drawCircle (x, y, 4);してこれを修正しますprojectile.graphics.drawCircle (0, 0, 4);

于 2013-04-24T03:35:24.827 に答える