我目前正在开发一款新的Flash游戏,这是一款滑动益智游戏。然而,大多数棋子的形状不像标准滑动益智游戏中的方形,这使得棋子之间的碰撞检测更加困难。我决定尝试编制黄色L和白色L片之间的碰撞检测,每个片都是100X100正方形,角落切掉50X50平方。反正有没有给他们碰撞检测,以便他们不能相互重叠?
java.security.*
答案 0 :(得分:0)
这是我在俄罗斯方块式游戏中使用的碰撞检查。它在我的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;
}
这是我的Piece类的构造函数:
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类只是扩展精灵并绘制一个正方形(我的一些好东西不需要在这里应用,所以我不会发布它。)
最后调用Piece构造函数以及传递给它的shape数组的示例。
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
}