gpt4 book ai didi

ios - SKSpriteNode 替换为 SKTexture

转载 作者:行者123 更新时间:2023-11-30 11:06:00 27 4
gpt4 key购买 nike

我的问题是,当我玩游戏时,即使敌人没有触及我的角色,而是飞得离他很近,我也会失去生命。我了解到问题是我角色的透明空间与敌人的透明空间接触了。

我想按照 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]))
}

关于ios - SKSpriteNode 替换为 SKTexture,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52722174/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com