2013-03-04 1 views
1

감사합니다. advance ... 다음의 적 클래스를 사용할 때 적들은 무대의 중심을 향해 정확한 각도로 오지 않습니다. 그렇다면 코드를 더 정확하게 수정하기 위해 어떤 변화를해야합니까? 당신이 (X2, Y2)에 x1, y1 지점에서 각도를해야하는 경우적의 위치에서 무대의 중심을 향해 접근하는 적

{ 
import flash.display.MovieClip; 
import flash.events.*; 
import flash.display.Stage; 

public class Enemy1 extends MovieClip 
{ 
    private var BG:MovieClip; 
    private var speed:Number = 0.5; 

    public function Enemy1(BG:MovieClip) : void 
    { 
     var RandomX:Array = new Array(100,200,300,400,800,900,1000,1100); 
     var RandomY:Array = new Array(100,200,300,600,700,800); 
     var r:int = (Math.random() * 8); 
     var s:int = (Math.random() * 6); 

     x = RandomX[r]; 
     y = RandomY[s]; 

     this.BG = BG; 

     addEventListener(Event.ENTER_FRAME, moveEnemy); //false, 0, true);. 
    } 
    private function moveEnemy(e:Event):void 
    { 
     if (this.x > 660) 
     { 
      this.x -= speed; 
      this.rotation = 180; 
     } 

     if (this.x < 540) 
     { 
      this.x += speed; 
      this.rotation = 0; 
     } 

     if (this.y > 510) 
     { 
      this.y -= speed; 
      this.rotation = 270; 
     } 

     if (this.y < 390) 
     { 
      this.y += speed; 
      this.rotation = 90; 
     } 

     if (this.x > 660 && this.y > 510) 
     { 
      this.x -= speed; 
      this.y -= speed; 
      this.rotation = 225; 
     } 

     if (this.x < 540 && this.y < 390) 
     { 
      this.x += speed; 
      this.y += speed; 
      this.rotation = 45; 
     } 

     if (this.x < 540 && this.y > 510) 
     { 
      this.x += speed; 
      this.y -= speed; 
      this.rotation = 315; 
     } 

     if (this.x > 660 && this.y < 390) 
     { 
      this.x -= speed; 
      this.y += speed; 
      this.rotation = 135; 
     } 
    } 

} 

변경 ... 감사를 제안하십시오 ..

+0

그들은 중심을 가리키고 있지 않거나 물리적으로 움직이지 않고 있습니까? 또한 'BG'는 적의 모양입니까? BG를지나 변수로 설정했지만 표시 목록에 추가하거나 아무것도하지 않는 것이 좋습니다. – Scott

+0

예 .. 그들은 중심쪽으로 움직이고 있지만 특정 방향 유형 (즉, 대각선, 수직 또는 수평)으로 여행하고 있습니다. 그러나 이제 나는 그들이 특정 방향으로뿐만 아니라 회전에 따라 적절한 방향으로 움직여야한다. 위. – MSV

답변

0
this.rotation=Math.atan2((stage.stageHeight>>1)-this.y,(stage.stageWidth>>1)-this.x)*180/Math.PI; 

, Math.atan2(y2-y1,x2-x1)를 사용합니다. 결과는 라디안이며, rotation 속성은도를 필요로합니다.이 경우 180을 곱하고 PI로 나눕니다. 그게 다야.

+0

부서 대신 점이 있습니다. – Gio

+0

@ 지오. 키보드 레이아웃 문제였습니다. – Vesper

+0

모두에게 감사드립니다. 이것은 많이 도움이됩니다 ...! 다시 감사합니다 .. – MSV

3

Vesper가 지적했듯이 atan2를 사용하면 하드 코딩 조건이 아닌 각도를 얻을 수 있습니다. 또한 적의 속도를 관리해야하며 적 당 하나씩 사용하는 대신 중앙 ENTER_FRAME 루프를 사용하는 것이 더 효율적일 수 있습니다.

package { 

    import flash.display.*; 
    import flash.events.*; 
    import flash.geom.*; 

    public class Enemy extends Sprite{ 

     public static const RAD_TO_DEG:Number = 57.2957795; 
     public static const RX:Array = [100,200,300,400,800,900,1000,1100]; 
     public static const RY:Array = [100,200,300,600,700,800]; 

     private var tx:Number;//target X 
     private var ty:Number;//target Y 
     private var size:Number = 20; 
     private var ease:Number = .05; 

     public function Enemy() { 
      addEventListener(Event.ADDED_TO_STAGE,init); 
     } 
     private function init(e:Event):void{ 
      //position 
      x = RX[(int)(Math.random() * RX.length)]; 
      y = RY[(int)(Math.random() * RY.length)]; 
      //render 
      graphics.lineStyle(7,Math.random() * 0xFF6600); 
      graphics.lineTo(-size,0); 
      graphics.moveTo(-size*.5,-size*.5); 
      graphics.lineTo(-size,0); 
      graphics.moveTo(-size*.5,size*.5); 
      graphics.lineTo(-size,0); 
     } 
     public function seek(ax:Number = NaN,ay:Number = NaN):void{ 
      if(ax) tx = ax; 
      if(ay) ty = ay; 
      //vector math stuff - direction to 
      var dx:Number = x-tx;//difference vector x component 
      var dy:Number = y-ty;//difference vector x component 
      //update position 
      x -= dx * ease; 
      y -= dy * ease; 
      //and rotation based on direction (using atan2 then converting radians to degrees) 
      rotation = Math.atan2(dy,dx) * RAD_TO_DEG; 
     } 

    } 

} 

및 기본 테스트 문서 클래스 : 분명히 당신은 당신이 보이는 수 있도록 작품에 회전을 약간 조정할 필요가 일부 작품이있는 경우

package { 

    import flash.display.*; 
    import flash.events.*; 

    public class EnemyTester extends Sprite{ 

     private var cenemies:Vector.<Enemy> = new Vector.<Enemy>();//central enemies 
     private var menemies:Vector.<Enemy> = new Vector.<Enemy>();//mouse enemies 

     public function EnemyTester() { 
      init(); 
     } 
     private function init():void{ 
      for(var i:int = 0; i < 30; i++){ 
       cenemies[i] = addChild(new Enemy) as Enemy; 
       cenemies[i].seek(stage.stageWidth * .5, stage.stageHeight * .5); 
       menemies[i] = addChild(new Enemy) as Enemy; 
      } 
      addEventListener(Event.ENTER_FRAME,update,false,0,true); 
     } 
     private function update (e:Event):void{ 
      for(var i:int = 0; i < 30; i++){ 
       cenemies[i].seek(); 
       menemies[i].seek(mouseX,mouseY); 
      } 
     } 



    } 

} 

여기

은 주석 예제 권리.

고급 AI의 경우 Steering Behaviours을보고 싶을 수 있습니다. Justin Windle's과 같은 as3 포트가 있습니다. 그들은 또한 Keith Peter's excelent Actionscript 3.0 Animation (코드 다운로드 가능)에 포함되어 있습니다.

+0

+1 Steering Behaviors 기사 링크. – null

+0

@MSV - 사용하려는 경우이 클래스는 Vector2D 클래스입니다. 그것은 위와 비슷한 것을하는 lookAt() 함수를 가지고있다. https://gist.github.com/leegrey/1039712 – null