2013-10-25 2 views
2

Я работаю над игрой, в которой фигуры должны сталкиваться после изменения вращения и размера. Пока изображение1 не вращается, используя его .pivotX и .pivotY, работает hitTest, но как только я поворачиваюсь вокруг оси, столкновение ошибочно. Я попытался использовать матрицу, но без хороших результатов. Кто-нибудь может мне помочь? Вот код:Старлинг, hitTest: вращение и масштаб

package 
{ 
    import flash.display.Bitmap; 
    import flash.display.BitmapData; 
    import flash.geom.Matrix; 
    import flash.geom.Point; 
    import flash.geom.Rectangle; 

    import starling.core.Starling; 
    import starling.display.Image; 
    import starling.display.Sprite; 
    import starling.events.Event; 
    import starling.textures.Texture; 
    import starling.textures.TextureAtlas; 

    public class HitTest extends Sprite 
    { 
     [Embed(source="src/image.png")] 
     public static const ImagePng:Class; 

     public function HitTest() 
     { 
      addEventListener(starling.events.Event.ADDED_TO_STAGE, init); 
     } 

     private function init():void{ 
      //First Image 
      var bitmap1:Bitmap = new ImagePng(); 
      var texture1:Texture = Texture.fromBitmap(bitmap1); 
      var image1:Image = new Image(texture1); 
      addChild(image1); 
      /* 
      Here's the problem: 
      If you don't use .pivotX and .pivotY image1 doesn't rotate around its center but hits image2 
      If you use .pivotX and .pivotY image1 rotates around its center but the hittest doesn't work. 
      */ 
     // image1.pivotX = image1.width/2; 
     // image1.pivotY = image1.height/2; 
      image1.x=215; 
      image1.y=50; 
      image1.rotation=1; 

      var rect1:Rectangle = image1.getBounds(this); 
      var offset1:Matrix = new Matrix; 

      offset1.rotate(1); 

      offset1.tx = image1.x - rect1.x; 
      offset1.ty = image1.y - rect1.y; 

      var bitmapData1:BitmapData = new BitmapData(rect1.width, rect1.height, true, 0); 
      bitmapData1.draw(bitmap1, offset1); 

      //Second Image 
      var bitmap2:Bitmap = new ImagePng(); 
      var texture2:Texture = Texture.fromBitmap(bitmap2); 
      var image2:Image = new Image(texture2); 
      addChild(image2); 
      image2.x=270; 
      image2.y=-10; 

      var rect2:Rectangle = image2.getBounds(this); 

      var offset2:Matrix = new Matrix; 
      offset2.tx = image2.x - rect2.x; 
      offset2.ty = image2.y - rect2.y;  

      var bitmapData2:BitmapData = new BitmapData(rect2.width, rect2.height, true, 0); 
      bitmapData2.draw(bitmap2, offset2); 

      var point1:Point = new Point(rect1.x, rect1.y); 
      var point2:Point = new Point(rect2.x, rect2.y); 

      //Hit Test 
      if(bitmapData1.hitTest(point1,255,bitmapData2,point2,255)) 
      { 
       image2.color=0x00ff00; 
      } 
     } 
    } 
} 

ответ

Смежные вопросы