2011-02-02 192 views
1

我正在嘗試將movieclip球沿着動畫片的地面邊界移動。您可以使用箭頭鍵移動球體,這會增加和減小其x值,但y值將始終等於Ground動畫片段的頂部邊緣。AS3 - 獲取邊緣座標

要做到這一點,我需要找到電影剪輯地面邊緣點的y值。這怎麼可能? Ground MovieClip

這就是Ground MovieClip的樣子。這是一個不規則的形狀。我想要的是任何x點都指向這個形狀的頂部邊緣。

+0

是地面上的矩形? – Mikushi 2011-02-02 22:13:20

+0

請提供一個屏幕截圖,以便我們可以看到「地面」的形狀(提示,問題編輯器的圖像按鈕提供了上傳。) – ocodo 2011-02-03 00:46:38

回答

5

如果這是你想要做的,它應該通過你的位圖數據的列併爲每個商店很容易環路Ÿ值以後可以參照得到ŸX。 這裏有一個基本的例子:

//get values 
var bitmapData:BitmapData = new Terrain(480,320);//Terrain is a reference to your image as BitmapData 
var yValues:Vector.<Number> = new Vector.<Number>(bitmapData.width,true); 

for(var i:int = 0 ; i < bitmapData.width ; i++){ 
    for(var j:int = 0 ; j < bitmapData.height; j++){ 
     //this conditions might change based on the content of your image, black if fine for now 
     if(bitmapData.getPixel(i,j) == 0) { 
      yValues[i] = j; 
      j = bitmapData.height; 
     }else yValues[i] = bitmapData.height; 
    } 
} 
//test values 
var bitmap:Bitmap = new Bitmap(bitmapData); 
var ball:Sprite = new Sprite();ball.graphics.lineStyle(5,0x009900);ball.graphics.drawCircle(-5,-5,5); 
addChild(bitmap); 
addChild(ball); 

addEventListener(Event.ENTER_FRAME, update); 

function update(event:Event):void { 
    ball.x = mouseX; 
    if(mouseX > 0 && mouseX <= bitmap.width) ball.y = yValues[mouseX]; 
} 

terrain

如果不是「緩存值」你想它在運行時滾動的地形,可以通過像素(的BitmapData與寬度列循環1和高度等於你的地形位圖)並獲得價值。

您的hitTestPoint()建議也不錯。請記住,BitmapData類也有hitTest方法。查看Mike Chamber's posts就可以了。

此外,您可能想在Corey's Collision Detection Kit

HTH

2

可以使用getbounds()得到一個矩形

你可以得到一個矩形的頂部和左側對象

1

我找到了解決辦法。使用一個for循環來hitTestPoint(x_val,y_val,true),每個點從ground.y直到ground.y + ground.height。當發現一擊時,這就是我的觀點。

4

喬治的技術來一看是好的(&可能是最快的)一個靜態的地形。

如果地形剪輯移動,計算起來可能會非常昂貴:2個嵌套循環來獲取查找表= ouch。

在這種情況下,我們可以使用BitmapData的getcolorBoundsRect()(!= getBounds():))屬性。

與一個BitmapData被稱作景物,它會看到這樣的:

scene.fillRect(scene.rect, 0);//clear bitmapdata 
scene.draw(mc);//draw the clip 

var rect:Rectangle = new Rectangle(mouseX, 0, 1, scene.height);//create a 1px wide rect at the desired X position 
var slice:BitmapData = new BitmapData(1, scene.height, true, 0);// create a slice bitmapdata 
slice.copyPixels(scene, rect, new Point);//draw the slice rect into the slice bitmapdata 
return new Point(rect.x, slice.getColorBoundsRect(0xff000000, 0, false).topLeft.y);//retrieve the top left corner of the getColorBoundsRect 

切片的getColorBoundsRect的左上角()是最上層的非透明像素。這在計算方面是一個相當便宜的測試,所以它可以在循環中使用。

建議不要每次都創建'slice'bitmapData。

我做了一個快速的動畫例如:

如果您取消註釋該//提取輪廓塊,你會得到恢復在這種情況下斜坡