0

I have been struggling with a as3 and flash game that i am trying to make. Everything looks fine, but still the bullet are stuck inside the cannon. When i use my mouse to shoot, instead of going out to a location, it just get stuck inside the cannon:

Got 3 as3 documents, and one flash document:

Ships.as

package{

import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Point;

public class Ship extends Sprite{

private var speed:int;
private var target:Point;

function Ship(){
    speed=2+Math.random()*5;
    //trace("Made a Ship!");
    //set the target for the ships
    target = new Point(Math.random()*500, Math.random()*500);
    //target.x = Math.random()*500;
    //target.y = Math.random()*500;
    //addEventListener(Event.ENTER_FRAME, update);
}
function update(){
    //Set the ships to point the target
    var dx = target.x - x;
    var dy = target.y - y;
    var angle = Math.atan2(dy, dx)/Math.PI*180;
    rotation = angle;
    x=x+Math.cos(rotation/180*Math.PI)*speed;
    y=y+Math.sin(rotation/180*Math.PI)*speed;
    //New target
    var hyp = Math.sqrt((dx*dx)+(dy*dy));   
    if(hyp < 5){
    target.x = Math.random()*500;
    target.y = Math.random()*500;
    }
}

}
}

Game.as

package{

import flash.display.MovieClip;
import flash.events.Event;

public class Game extends MovieClip{

    var ships:Array;

    public function Game(){
        trace("Made that game!");
        addEventListener(Event.ENTER_FRAME, loop);
        //set up the ship array
        ships = new Array();
    }
    function loop(e:Event){
        if(numChildren<10){
        var s = new Ship();
        addChild(s);

        s.x = Math.random()*stage.stageWidth;
        s.y = Math.random()*stage.stageHeight;
        s.rotation = Math.random()*360;

        //Add the ship to the list of ships
        ships.push(s);


        }
        //Make a for loop to iterate through all the ship
        for(var count=0; count<ships.length; count++){
            ships[count].update();
            //Add a new for loop to go through all the bullets


                }


            }


        }
    }
}

}

Turret.as

package{

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;

    public class Turret extends MovieClip{

        //Properties goes here
        var shotCooldown:int;
        var bullets:Array;
        const MAX_COOLDOWN = 10;

    public function Turret(){

        //Set the Shot Cooldown
        shotCooldown = MAX_COOLDOWN;
        bullets = new Array();
        addEventListener(Event.ENTER_FRAME, update);
        addEventListener(Event.ADDED_TO_STAGE, initialise);
    }

    function initialise(e:Event)
    {
        stage.addEventListener(MouseEvent.CLICK, fire);
    }

    function fire(m:MouseEvent)
    {
        //If we are allowed to shoot
        if(shotCooldown<=0)
        {

            //Reset the Shot Cooldown
            shotCooldown=MAX_COOLDOWN;
        //Spawn a Bullet
        var b = new Bullet();

        b.rotation = rotation;
        b.x = x;
        b.y = y;
        //Add the bullet to the list of bullets
        bullets.push(b);
        parent.addChild(b);
        play();
        }
    }

    function update(e:Event)
    {
        //Reduce the Shot Cooldown by 1
        //shotCooldown=shotCooldown-1;
        //shotCooldown-=1;
        shotCooldown--;
        if(parent != null)
        {
        var dx = parent.mouseX - x;
        var dy = parent.mouseY - y;
        var angle = Math.atan2(dy, dx) / Math.PI * 180;
        rotation = angle;
        }
    }

}

}
djv
  • 15,168
  • 7
  • 48
  • 72

2 Answers2

0

In Turret class bullets are added to the stage and array but doesn't have updated every frame like ships. See your own comment about updating bullets!

meps
  • 579
  • 2
  • 17
0

They are stuck in place maybe because you are not moving them at all? If you are then please show me where. Try adding to the turret's enter frame event the following code:

for (var a:int = 0; bullets.length > a ; a++)
{
    var temp:MovieClip;
    temp = bullets[a] as Bullet;

    var vel:Point = new Point();
    vel.x = temp.target.x-temp.x;
    vel.y = temp.target.y-temp.y;
    vel.normalize(4);

    temp.x += vel.x;
    temp.y += vel.y;
}

And make an as file for the Bullet Class and add this:

package
{

   import flash.geom.Point;

   public class Bullet extends MovieClip 
   {

      public var target:Point = new Point();

       public function Bullet()
       {
           target.x = stage.mouseX;
           target.y = stage.mouseY;
       }

   }
}
  • Change the 4 in normalize to adjust the speed you like –  Feb 06 '14 at 04:25
  • Sorry mate, forgot to comment my bullet as3 class. Here it is: package{ import flash.display.Sprite; import flash.events.Event; public class Bullet extends Sprite{ //Properties private var speed:int; public function Bullet(){ speed=10; } function update(){ //Retning skuddene skytter mot x=x+Math.cos(rotation/180*Math.PI)*speed; y=y+Math.sin(rotation/180*Math.PI)*speed; //Remove the Bullet when its off the screen if(x<0 || x>500 || y<0 || y>500){ //Remove the Bullet from the screen parent.removeChild(this); } } } } – user3276442 Feb 06 '14 at 09:26
  • I tested your code and it worked fine, the problem is you are not calling the update function, try adding an enter frame event listener on the bullet class and call update in it. –  Feb 06 '14 at 14:11