2014-01-17 2 views
0

화면 보호기를 만들려고하는데 콘텐츠 내에 '수영하는 주 물고기'라고 부를 수 있습니다. 나는 주요 물고기가 방금 수영하는 곳에서 코딩의 일부를 할 수 있었다. 지금 나는 할 수 있었던 주된 물고기에 뒤이어서 더 작은 물고기를 더했다. 그러나 주된 물고기 뒤에 그것을 따라 또는 다른 물고기와 충돌하지 않는 것에 따라 내가 어떻게 주된 물고기 뒤에 머무르는 것을 만들 수 있냐. 이것은 내가이 물고기 클래스 (따르는 사람)AS3 : 충돌을 피하십시오.

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.geom.Point; 

    public class Fish extends MovieClip 
    { 
     var speed:Number = 3; 
     var target:Point; 

     public function Fish() 
     { 
      // constructor code 
      addEventListener(Event.ENTER_FRAME, update); 
     } 

     function update(e:Event) 
     { 
      //Point fish at main fish 
      var dx = MovieClip(parent).mainfish01.x - x; 
      var dy = MovieClip(parent).mainfish01.y - y; 
      var angle = Math.atan2(dy,dx)/Math.PI * 180; 
      rotation = angle; 

      //Move in the direction the fish is facing 
      x = x+Math.cos(rotation/180*Math.PI)*speed; 
      y = y+Math.sin(rotation/180*Math.PI)*speed; 

      //Calculate the distance to target 
      var hyp = Math.sqrt((dx*dx)+(dy*dy)); 
     } 
    } 
} 

답변

0

가 최대 거리를 설정입니다 지금까지

을 수행 한 다음 위치를

function update(e:Event) 
    { 
     //Point fish at main fish 
     var dx = MovieClip(parent).mainfish01.x - x; 
     var dy = MovieClip(parent).mainfish01.y - y; 
     var angle = Math.atan2(dy,dx)/Math.PI * 180; 
     rotation = angle; 


     //Calculate the distance to target 
     var hyp = Math.sqrt((dx*dx)+(dy*dy)); 

     // calculate based on size 
     var minDist = 10; 
     if(hyp > minDist){ 
      //Move in the direction the fish is facing 
      x = x+Math.cos(rotation/180*Math.PI)*speed; 
      y = y+Math.sin(rotation/180*Math.PI)*speed; 
     } 
    } 
+0

를 업데이트하기 전에 거리를 확인 한 것입니다 실제 거리를 계산해서는 안되지만 비싼 함수 인 제곱근을 계산하지 않아도되므로 거리의 제곱을 사용해야합니다. 그러나 약간의 물고기에서는 차이를 느끼지 못할 것입니다. – Daniel

+0

대니얼은 매력처럼 작동했습니다. 최근에는 조향 동작에 대해 알아 냈습니다. 나는 앉아서 공부해야하지만, 감사해야합니다. – Saf

관련 문제