2012-06-09 60 views
0
public class GameEnter extends MovieClip 
{ 


    public function GameEnter() 
    { 
    addEventListener(MouseEvent.MOUSE_DOWN, shootBullet); 
    } 
    public var _bullet1:bullet = new bullet; 
    public var angleRadian = Math.atan2(mouseY - 300,mouseX - 300); 
    public var angleDegree = angleRadian * 180/Math.PI; 


    public function shootBullet(evt:MouseEvent) 
    { 
     _bullet1.x = 300; 
     _bullet1.y = 300; 
     _bullet1.angleRadian = Math.atan2(mouseY - 300,mouseX -300); 
     _bullet1.addEventListener(Event.ENTER_FRAME, bulletEnterFrame); 
     addChild(_bullet1); 
    } 


    public var speed1:int = 10; 
    public function bulletEnterFrame(evt:Event) 
    { 
     _bullet1.x += Math.cos(_bullet1.angleRadian) * speed1; 
     _bullet1.y += Math.sin(_bullet1.angleRadian) * speed1; 
     _bullet1.rotation = _bullet1.angleRadian*180/Math.PI; 
     checkHit(); 
     if (_bullet1.x < 0 || _bullet1.x > 600 || _bullet1.y < 0 || _bullet1.y > 600) 
     { 
      removeChild(_bullet1); 
      _bullet1.removeEventListener(Event.ENTER_FRAME, bulletEnterFrame); 
     } 
    } 



public function checkHit() 
{ 
    if(_bullet.hitTestObject(enemy)) 
    { 
      trace("Enemy hit"); 
    } 
} 

} 

} 本質上發生的是它打印出「敵人命中」約10次。我想知道:我如何讓它只跟蹤「敵人擊中」一次? 我認爲問題是,對於子彈在敵人身上的每一幀,它都將其打印出來,我如何解決這個問題只打印一次?我最終將取代「痕跡(」敵人命中「);」用一個函數或其他東西。hitTestObject輸出多次?

回答

0

checkHit()在每一幀都被調用,子彈只在遊戲區外面被刪除?這意味着「敵人命中」將在checkHit()爲真的每一幀中被追蹤。一旦命中發生,你應該刪除事件監聽器,類似於當子彈離開屏幕時。