2011-05-17 25 views
0

私のコードや何かに問題があります。物事は、私は1つのオブジェクトの座標から他のオブジェクトへの座標を使用していますが、2番目のオブジェクトがどこにあるかは常にわかりません左下のどこかにランダムな方向があり、私は立ち往生しています。lineが正しく動作しません

ここのコードです:私はすべての行が左下TT_TTに向けて[誤ってリーン書い]指していることが分かった数回の試行後に

 var spr:Shape = new Shape(); 
     spr.graphics.clear(); 
     spr.graphics.lineStyle(2,0xffffff); 
     spr.x = latM[1].x; 
     spr.y = latM[1].y; 
     spr.graphics.lineTo(latM[0].x,latM[0].y); 
     trace("latM[0].x = "+latM[0].x+"\tlatM[0].y = "+latM[0].y+ 
       "\nlatM[1].x = "+latM[1].x+"\tlatM[1].y = "+latM[1].y); 
     spr.graphics.lineTo(latM[0].x,latM[0].y); 
     addChild(spr); 

..

+0

あなたは、左下の方に傾く何を意味するのですか? –

+0

私はそれを簡単にする例を挙げます:私はx100とy100から始まり、x200とx50の間に線を入れたいとしましょう。同じものを指していれば、左下を下向きにしますスポットを使ってx 20 y 500と言うことができます。同じ結果が得られます – JustAddX

+0

Yを変更することで、ラインを「ポイントダウン」します。私は何かを逃していますか、これはあなたが探している望ましい効果ではありませんか? –

答えて

0

あなたが傾く何を意味するのかは、左下?
lineToを使用して直線を描くことができます。
"lineTo"は、現在のポイントからパラメータを使用して設定されたポイントにのみ移動します。
moveTo関数は、描画しないでポイントを移動します。
次のコードは、ボックスX 100 100

var spr:Shape = new Shape(); 
spr.graphics.clear(); 
spr.graphics.lineStyle(2,0xff00ff); 
spr.graphics.moveTo(0,0); 
spr.graphics.lineTo(0,100); 
spr.graphics.lineTo(100,100); 
spr.graphics.lineTo(100,0); 
spr.graphics.lineTo(0,0); 
addChild(spr); 
1

を描画します私はLATMを想定して、[1]とLATM [0]あなたが間に線を引くしようとしている2つの形状です。その場合、同じ点に行くために2つの線があることに気づいたか?

必要なものは次のとおりです。

spr.graphics.moveTo(latM[0].x, latM[0].y); 
spr.graphics.lineTo(latM[1].x, latM[1].y); 

どのように動作するかを示す小さなプロトタイプです。 (これは、それが迅速かつ汚いプロトタイプであるスーパーソリッドコードを意味するものではありません。)

package src 
{ 
    import flash.display.Sprite; 
    import flash.events.Event; 

    public class Main extends Sprite 
    { 
     private var obj1:Sprite = new Sprite(); 
     private var obj2:Sprite = new Sprite(); 
     private var lineSprite:Sprite = new Sprite(); 

     // for testing your line. 
     // we don't really need it for this prototype however it 
     // is being used since this is how your accessing your Objects. 
     private var latM:Array = []; 

     public function Main() 
     { 
      addEventListener(Event.ADDED_TO_STAGE, initMain); 
     } 

     private function initMain(e:Event):void 
     { 
      removeEventListener(Event.ADDED_TO_STAGE, initMain); 

      obj1.graphics.lineStyle(1, 0); 
      obj1.graphics.beginFill(0xccccff); 
      obj1.graphics.drawCircle(0, 0, 20); 
      obj1.graphics.endFill(); 
      obj1.x = 100; 
      obj1.y = 100; 

      obj2.graphics.lineStyle(1, 0); 
      obj2.graphics.beginFill(0xffcccc); 
      obj2.graphics.drawCircle(0, 0, 20); 
      obj2.graphics.endFill(); 
      obj2.x = 400; 
      obj2.y = 200; 

      // for testing your line. 
      latM.push(obj1, obj2); 

      addChild(obj1); 
      addChild(obj2); 
      addChild(lineSprite); 

      addEventListener(Event.ENTER_FRAME, handleEnterFrame); 
     } 

     private function handleEnterFrame(e:Event):void 
     { 
      // this will clear and redraw the line between the two sprites 
      // every frame and thus always be up to date. 
      lineSprite.graphics.clear(); 
      lineSprite.graphics.lineStyle(2, 0xff0000); 
      lineSprite.graphics.moveTo(latM[0].x, latM[0].y); 
      lineSprite.graphics.lineTo(latM[1].x, latM[1].y); 

      //obj1.x++; // uncomment this line and you can watch it move and keep the line perfect. 
     } 

    } 

} 
+0

私は最終的に私が間違っていたことを理解してくれてありがとう – JustAddX

関連する問題