SKSpriteNode 替换为 SKTexture
SKSpriteNode replace with SKTexture
我的问题是,当我玩我的游戏时,即使敌人没有碰到我的角色,但离他很近,我也会失去生命。我明白问题是我角色的透明space和敌人的透明space.
有接触
我想按照SKPhysicsBody documentation中的建议去做。我如何使用我当前的代码实现它?
func madEnemy() {
let randomNumber = Int.random(in: 1..<6)
let enemy = SKSpriteNode(imageNamed: "enemy\(randomNumber)")
enemy.position = CGPoint(
x: cameraRect.maxX + enemy.size.width/2,
y: CGFloat.random(
min: cameraRect.minY + enemy.size.height/2,
max: cameraRect.maxY - enemy.size.height/2))
enemy.zPosition = 50
enemy.name = "enemy"
addChild(enemy)
let actionMove =
SKAction.moveBy(x: -(size.width + enemy.size.width), y: 0, duration: 2.0)
let actionRemove = SKAction.removeFromParent()
enemy.run(SKAction.sequence([actionMove, actionRemove]))
}
您只需要先从您的图像创建一个 SKTexture
对象,然后使用该纹理创建一个 SKSpriteNode
以便使用该纹理调整 SKPhysicsBody
或您的节点的大小.
func madEnemy() {
let randomNumber = Int.random(in: 1..<6)
let enemyTexture = SKTexture(imageNamed: "enemy\(randomNumber)")
let enemy = SKSpriteNode(texture: enemyTexture)
enemy.physicsBody = SKPhysicsBody(texture: enemyTexture, size: CGSize(width: enemy.size.width, height: enemy.size.height))
enemy.position = CGPoint(
x: cameraRect.maxX + enemy.size.width/2,
y: CGFloat.random(
min: cameraRect.minY + enemy.size.height/2,
max: cameraRect.maxY - enemy.size.height/2))
enemy.zPosition = 50
enemy.name = "enemy"
addChild(enemy)
let actionMove =
SKAction.moveBy(x: -(size.width + enemy.size.width), y: 0, duration: 2.0)
let actionRemove = SKAction.removeFromParent()
enemy.run(SKAction.sequence([actionMove, actionRemove]))
}
我的问题是,当我玩我的游戏时,即使敌人没有碰到我的角色,但离他很近,我也会失去生命。我明白问题是我角色的透明space和敌人的透明space.
有接触我想按照SKPhysicsBody documentation中的建议去做。我如何使用我当前的代码实现它?
func madEnemy() {
let randomNumber = Int.random(in: 1..<6)
let enemy = SKSpriteNode(imageNamed: "enemy\(randomNumber)")
enemy.position = CGPoint(
x: cameraRect.maxX + enemy.size.width/2,
y: CGFloat.random(
min: cameraRect.minY + enemy.size.height/2,
max: cameraRect.maxY - enemy.size.height/2))
enemy.zPosition = 50
enemy.name = "enemy"
addChild(enemy)
let actionMove =
SKAction.moveBy(x: -(size.width + enemy.size.width), y: 0, duration: 2.0)
let actionRemove = SKAction.removeFromParent()
enemy.run(SKAction.sequence([actionMove, actionRemove]))
}
您只需要先从您的图像创建一个 SKTexture
对象,然后使用该纹理创建一个 SKSpriteNode
以便使用该纹理调整 SKPhysicsBody
或您的节点的大小.
func madEnemy() {
let randomNumber = Int.random(in: 1..<6)
let enemyTexture = SKTexture(imageNamed: "enemy\(randomNumber)")
let enemy = SKSpriteNode(texture: enemyTexture)
enemy.physicsBody = SKPhysicsBody(texture: enemyTexture, size: CGSize(width: enemy.size.width, height: enemy.size.height))
enemy.position = CGPoint(
x: cameraRect.maxX + enemy.size.width/2,
y: CGFloat.random(
min: cameraRect.minY + enemy.size.height/2,
max: cameraRect.maxY - enemy.size.height/2))
enemy.zPosition = 50
enemy.name = "enemy"
addChild(enemy)
let actionMove =
SKAction.moveBy(x: -(size.width + enemy.size.width), y: 0, duration: 2.0)
let actionRemove = SKAction.removeFromParent()
enemy.run(SKAction.sequence([actionMove, actionRemove]))
}