我正在尝试创建一个像The Tower的游戏,我有一个基础(基础)和一个移动的图块(从-x到+ x),其大小等于该基数。在点击时,将移除活动磁贴,如果活动磁贴的一部分超出了底座的宽度,则该部分将被破坏,并使用新的尺寸和位置创建一个新的磁贴。当我放置第二块活动瓷砖时,其位置和大小与预期的不同。
// tile corner sides values
let left_tile_corner = moving_tile_pos - moving_tile_size * 0.5
let right_tile_corner = moving_tile_pos + moving_tile_size * 0.5
// base corner sides values
let left_base_corner = stationary_tile_pos - stationary_tile_size * 0.5
let right_base_corner = stationary_tile_pos + stationary_tile_size * 0.5
if left_tile_corner <= left_base_corner && right_tile_corner > left_base_corner
如果以上条件为真,则应调用以下代码
var placed_tile_size : CGSize = CGSize.zero
var placed_tile_pos : CGPoint = CGPoint.zero
var destroyed_tile_size : CGSize = CGSize.zero
var destroyed_tile_pos : CGPoint = CGPoint.zero
if tiles_placed.isEmpty
{
//find the closest point to the center of the previous tile
let start_pos = moving_tile.position.x + moving_tile.size.width * 0.5
// new tiles size
placed_tile_size = CGSize(width: base.size.width * 0.5 + start_pos, height: moving_tile.size.height)
destroyed_tile_size = CGSize(width: moving_tile.size.width - placed_tile_size.width, height: moving_tile.size.height)
// new tiles position
placed_tile_pos = CGPoint(x: -(base.size.width * 0.5 - start_pos) * 0.5, y: moving_tile.position.y)
let new_dt_pos = placed_tile_pos.x - start_pos
destroyed_tile_pos = CGPoint(x: moving_tile.position.x + new_dt_pos, y: moving_tile.position.y)
}
else
{
//find the closest point to the center of the previous tile
let start_pos = moving_tile.position.x + moving_tile.size.width * 0.5
// new tiles size
placed_tile_size = CGSize(width: tiles_placed.last!.size.width * 0.5 + start_pos, height: moving_tile.size.height)
destroyed_tile_size = CGSize(width: moving_tile.size.width - placed_tile_size.width, height: moving_tile.size.height)
// new tiles position
placed_tile_pos = CGPoint(x: -(tiles_placed.last!.size.width * 0.5 - start_pos) * 0.5, y: moving_tile.position.y)
let new_dt_pos = placed_tile_pos.x - start_pos
destroyed_tile_pos = CGPoint(x: moving_tile.position.x + new_dt_pos, y: moving_tile.position.y)
}
// tower placed tile
let placed_tile : SKSpriteNode = SKSpriteNode()
placed_tile.texture = SKTexture(imageNamed: "tile_full_1")
placed_tile.size = placed_tile_size
placed_tile.position = placed_tile_pos
placed_tile.alpha = 1.0
placed_tile.zPosition = 1
background.addChild(placed_tile)
// add a new tile to this vector
tiles_placed.append(placed_tile)
// tower destroyed tile
let destroyed_tile : SKSpriteNode = SKSpriteNode()
destroyed_tile.texture = SKTexture(imageNamed: "tile_full_2")
destroyed_tile.size = destroyed_tile_size
destroyed_tile.position = destroyed_tile_pos
destroyed_tile.alpha = 1.0
destroyed_tile.zPosition = 1
background.addChild(destroyed_tile)
这是游戏的屏幕截图,黄色的方块被放置了,绿色的方块被破坏了。 https://imgur.com/bxltp31