Question

Ok, let me start off by saying that this is my first AS3 project. I'm ok with Java, so OOP and inheritance aren't new to me. I have literally spent hours sitting here and pondering why my code isn't working like I wanted it to. I'm starting off with a turret trying to shoot a bullet. However, the bullet isn't being drawn onto the screen, even though the x and y coords are still changing. The method where the bullet/projectile is created is called OnMouseClick. Here's my code:

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

    }

}

and the Projectile class:

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;        

    }
  }

I just don't know what I'm missing... I'm calling addChild to all of my newly created objects, what else is there to do?

Était-ce utile?

La solution

You are not adding the sprite projectile to the displaylist of Projectile in your constructor.

addChild(projectile);

Also you are doubling up on x and y co-ordinates in your projectile class. Rectify this by changing projectile.graphics.drawCircle (x, y, 4); to projectile.graphics.drawCircle (0, 0, 4);

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top