我目前正在开发游戏的一部分,其中涉及移动瓷砖,可以推动其他瓷砖,基本上就像那些拼图板丢失,你需要重建图像。
问题:有没有办法,不使用box2d和碰撞检测来让瓷砖正确移动?
这与我之前提出的问题有关:
Snap squares to a grid algorithm
我问的是因为物理引擎将瓷砖留在了整个地方。
可能会丢失多个部分(与此图片不同,只有一个部分)
答案 0 :(得分:0)
它归结为更大的“董事会”中的递归功能。谢谢你的意见。大多数情况下只需要知道这很容易。
func moveTile(tile:Tile, amount:CGFloat, direction:Direction) -> CGFloat {
var checkLocation:CGPoint = tile.position
var spaceBetween:CGFloat = 0
switch direction {
case .North:
checkLocation.y += amount + (tileSize.height / 2)
case .South :
checkLocation.y -= amount + (tileSize.height / 2)
case .East:
checkLocation.x += amount + (tileSize.width / 2)
case .West:
checkLocation.x -= amount + (tileSize.width / 2)
}
var maxAmount = amount
if !boundingEdgeRect.contains(checkLocation) {
switch direction {
case .North:
maxAmount -= checkLocation.y - boundingEdgeRect.height - boundingEdgeRect.origin.y
case .South :
maxAmount -= boundingEdgeRect.origin.y - checkLocation.y
case .East:
maxAmount -= checkLocation.x - boundingEdgeRect.width - boundingEdgeRect.origin.x
case .West:
maxAmount -= boundingEdgeRect.origin.x - checkLocation.x
}
} else {
let nodes = self.nodesAtPoint(checkLocation)
for node in nodes {
if let nextTile = node as? Tile {
switch direction {
case .North:
spaceBetween = nextTile.position.y - tile.position.y - tileSize.height
case .South :
spaceBetween = tile.position.y - nextTile.position.y - tileSize.height
case .East:
spaceBetween = nextTile.position.x - tile.position.x - tileSize.width
case .West:
spaceBetween = tile.position.x - nextTile.position.x - tileSize.width
}
let tileAmount = moveTile(nextTile, amount:amount - spaceBetween, direction:direction) + spaceBetween
if tileAmount < maxAmount {
maxAmount = tileAmount
}
}
}
}
switch direction {
case .North:
tile.position.y += maxAmount
case .South :
tile.position.y -= maxAmount
case .East:
tile.position.x += maxAmount
case .West:
tile.position.x -= maxAmount
}
return maxAmount
}
瓷砖是中心锚定的,位置相对于板,也是中心锚定。