我試圖做一個簡單的遊戲,當球落入某個塊時,你贏了。AS3:如何檢測碰撞?
機制:球通過幾個障礙物落下,最後有兩個方塊,如果球碰到你贏的左方塊,下一個關卡將包含更多的塊和更少的空間。
測試影片(點擊屏幕上砸球): http://gabrielmeono.com/downloads/Lucky_Hit_Alpha.swf
這些是主要的變量:
var winBox:QuickObject;//You win
var looseBox:QuickObject;//You loose
var gameBall:QuickObject;//Ball dropped
問:
- 如何檢測交流電如果球擊中winBox,ollision併發揮作用? (贏消息/下一等級)*
謝謝你,這裏是全碼:
package {
import flash.display.MovieClip;
import com.actionsnippet.qbox.*;
import flash.events.MouseEvent;
[SWF(width = 600, height = 600, frameRate = 60)]
public class LuckyHit extends MovieClip {
public var sim:QuickBox2D;
var winBox:QuickObject;
var looseBox:QuickObject;
var gameBall:QuickObject;
/**
* Constructor
*/
public function LuckyHit()
{
sim = new QuickBox2D(this);
//sim.createStageWalls();
winBox = sim.addBox({x:5,y:600/30, width:300/30, height:10/30, density:0});
looseBox = sim.addBox({x:15,y:600/30, width:300/30, height:10/30, density:0});
// make obstacles
for (var i:int = 0; i<(stage.stageWidth/50); i++){
//End
sim.addCircle({x:1 + i * 1.5, y:16, radius:0.1, density:0});
sim.addCircle({x:2 + i * 1.5, y:15, radius:0.1, density:0});
//Mid End
sim.addCircle({x:0 + i * 2, y:14, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:13, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:12, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:11, radius:0.1, density:0});
sim.addCircle({x:0 + i * 2, y:10, radius:0.1, density:0});
//Middle Start
sim.addCircle({x:0 + i * 1.5, y:09, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:08, radius:0.1, density:0});
sim.addCircle({x:0 + i * 1.5, y:07, radius:0.1, density:0});
sim.addCircle({x:1 + i * 1.5, y:06, radius:0.1, density:0});
}
sim.start();
stage.addEventListener(MouseEvent.CLICK, _clicked);
}
/**
* ..
* @param e MouseEvent.CLICK
*/
private function _clicked(e:MouseEvent)
{
gameBall = sim.addCircle({x:(mouseX/30), y:(1), radius:0.25, density:5});
}
}
}
除非我誤讀,因爲我不熟悉box2d,所以應該使用[hitTestObject](http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#hitTestObject ()),當它返回true時,調用你想要的任何函數。 – ToddBFisher 2011-12-15 18:51:10