我目前正在研究一个新的Flash游戏,这是一个滑动益智游戏。然而,大多数棋子的形状都不像标准的滑动益智游戏中的方形,这使得棋子之间的碰撞检测变得更加困难。我决定尝试编写黄色L和白色L块之间的碰撞检测程序,每个L块都是一个100X100的正方形,角部有一个50X50的正方形。有没有办法给它们提供碰撞检测,这样它们就不会相互重叠?
var YellowLClicked: Boolean=false;
addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
YellowL.addEventListener(MouseEvent.MOUSE_DOWN, dragYellowL);
YellowL.addEventListener(MouseEvent.MOUSE_UP, DoNotdragYellowL);
YellowL.addEventListener(MouseEvent.ROLL_OUT, DoNotdragYellowL);
function dragYellowL(e:MouseEvent): void{
YellowLClicked=true;
}
function DoNotdragYellowL(e:MouseEvent): void{
YellowLClicked=false;
}
function onEnterFrameHandler(e:Event): void{
if(YellowLClicked){
YellowL.startDrag();
} else YellowL.stopDrag();
//Can you help with this code here?
if(YellowL.hitTestObject(WhiteL)) trace("true"); else trace(false);
}发布于 2017-02-08 00:37:18
这是我在俄罗斯方块风格的游戏中使用的碰撞检查。它在我的Main.as文件中。
private function checkCollision(f:GameField):Boolean{
var p:Piece = f.activePiece;
var a:Array = f.blockArray;
for (var j:int = 0; j < p.numChildren; j++){
var b:Block = p.getChildAt(j) as Block;
// check walls, your will probably have four instead of 3
if (b.hitTestObject(f.leftWall) || b.hitTestObject(f.rightWall) || b.row + b.rowLocal >= f._rows){
return true;
}
}
// check all the blocks in f.blockArray
for (var i:int = 0; i < a.length; i++){
// check the moving PIECE against all other BLOCKS
if (p.hitTestObject(a[i])){
// check every child of the PIECE (these will be the blocks)
for (var j:int = 0; j < p.numChildren; j++){
// excluding the BLOCKS in the moving PIECE
if (p.getChildAt(j).parent != a[i].parent){
var b:Block = p.getChildAt(j) as Block;
// finally, BLOCK vs BLOCK collision check
if (b.hitTestObject(a[i])){
return true;
}
}
}
}
}
return false;
}这是我的块类的构造函数:
public function Piece(f:GameField,shape:Array,color:uint)
{
makeGrid();
f.addChild(this);
// the shape array passed to this constructor is shown at the end of my post
_shape = shape;
addBlocks(f,shape,color);
}
private function addBlocks(f:GameField, shape:Array, color:uint):void{
_shape = shape;
for (var i:int = 0; i < shape.length; i++){
var b:Block = new Block(color);
// this uses the Array passed as var "shape" to tell where the block will be in relation to the center of rotation of the Piece instance
b.rowLocal = shape[i].y+2;
b.columnLocal = shape[i].x+2;
// this tells the brick its starting column and row are
b.row = -2;
b.column = 3;
// this is the sum of the local and global coordinates
b.y = (b.rowLocal + b.row) * Main.UNIT_SIZE;
b.x = (b.columnLocal + b.column) * Main.UNIT_SIZE;
// put the block on the display list
addChild(b);
// add the block to the array to use for collision checks in the Main doc
f.blockArray.push(b);
}
}Block类只是扩展了sprite并绘制了一个正方形(我的类有一些不需要在这里应用的好东西,所以我不会发布它)。
最后是对块构造函数的调用,以及传递给它的形状数组的示例。
var newP:Piece = new Piece(f, arr, color);和形状数组:
_shapeArray = [
{shape:new Array(new Point (-2, 0), new Point (-1, 0), new Point (0, 0), new Point (1, 0)),color:_colorArray[0]}, // I
{shape:new Array(new Point (-1, -1), new Point (-1, 0), new Point (0, 0), new Point (0, -1)),color:_colorArray[1]}, // O
{shape:new Array(new Point (-2, -1), new Point (-1, -1), new Point (-1, 0), new Point (0, 0)),color:_colorArray[2]}, // Z
{shape:new Array(new Point (-2, 0), new Point (-1, 0), new Point (-1, -1), new Point (0, -1)),color:_colorArray[3]}, // S
{shape:new Array(new Point (-2, 0), new Point (-1, 0), new Point (0, 0), new Point (-2, -1)),color:_colorArray[4]}, // J
{shape:new Array(new Point (-2, 0), new Point (-1, 0), new Point (0, 0), new Point (0, -1)),color:_colorArray[5]}, // L
{shape:new Array(new Point (-2, 0), new Point (-1, 0), new Point (0, 0), new Point (-1, -1)),color:_colorArray[6]} // T
];总结一下:
编辑
这看起来是可行的。但是,我还有一个问题: checkCollision()返回一个布尔值,但是我该如何处理这个布尔值呢?如果布尔值为true,如何使对象无法交叉?
这就是我如何实现它的。允许对象彼此交叉,然后调用
if (checkCollision(f)) {
// check collision returned true
// so reverse the last move
// before finishing this game loop
}https://stackoverflow.com/questions/42047234
复制相似问题