2014-02-05 55 views
0

我一直在努力嘗試使用as3和flash遊戲。一切看起來都很好,但子彈仍然卡在大炮裏面。當我用我的鼠標拍攝,而不是走出去的位置,它只是停留拿到大炮裏面:AS3炮塔和子彈錯誤

了3 AS3文件,以及一個flash文件:

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

} 

} 

回答

0

它們卡在原地也許是因爲你根本沒有移動它們?如果你是那麼請告訴我在哪裏。嘗試添加到炮塔的輸入幀事件下面的代碼:

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

並作出如文件Bullet類和補充一點:在正常化

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

    } 
} 
+0

更改4,調整你喜歡 – user3217163

+0

速度對不起隊友,忘了給我的子彈評論as3課。這裏是:package { \t import flash.display.Sprite; \t import flash.events.Event; \t \t公共類子彈擴展Sprite { \t \t //屬性 \t \t私人變種速度:INT; \t \t \t 公共\t功能子彈(){ \t \t速度= 10; \t \t} \t \t功能更新(){ \t \t \t // Retning skuddene斯凱特MOT \t \t X = X + Math.cos(旋轉/ 180 * Math.PI)*速度; y = y + Math.sin(rotation/180 * Math.PI)* speed; \t \t y = y + Math.sin \t \t \t // \t刪除子彈時其關閉屏幕 \t \t如果(X <0 || x> 500 ||ý<0 || y> 500){ \t \t \t //從屏幕 \t \t \t母體取出子彈。removeChild之(本); \t \t \t \t \t \t} \t \t} \t \t} \t \t } – user3276442

+0

我測試你的代碼,它工作得很好,問題是你是不是調用更新功能,嘗試添加一個輸入框項目符號類上的事件監聽器並在其中調用更新。 – user3217163

0

在炮臺級子彈被添加到舞臺和陣列中,但沒有像船隻一樣更新每一幀。看到你自己對更新子彈的評論!