swift - 当我运行它时,精灵在模拟器中出现在屏幕外。这段代码来自我从 raywenderlich 在线找到的教程。任何帮助深表感谢
问题描述
func addMonster() {
// Create sprite
let monster = SKSpriteNode(imageNamed: "monster")
monster.physicsBody = SKPhysicsBody(rectangleOf: monster.size) // 1
monster.physicsBody?.isDynamic = true // 2
monster.physicsBody?.categoryBitMask = PhysicsCategory.monster // 3
monster.physicsBody?.contactTestBitMask = PhysicsCategory.projectile // 4
monster.physicsBody?.collisionBitMask = PhysicsCategory.none // 5
// Determine where to spawn the monster along the Y axis
let actualY = random(min: monster.size.height/2, max: size.height - monster.size.height/2)
// Position the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = CGPoint(x: size.width + monster.size.width/2, y: actualY)
// Add the monster to the scene
addChild(monster)
// Determine speed of the monster
let actualDuration = random(min: CGFloat(2.0), max: CGFloat(4.0))
// Create the actions
let actionMove = SKAction.move(to: CGPoint(x: -monster.size.width/2, y: actualY), duration: TimeInterval(actualDuration))
let actionMoveDone = SKAction.removeFromParent()
let loseAction = SKAction.run() { [weak self] in
guard let `self` = self else { return }
let reveal = SKTransition.flipHorizontal(withDuration: 0.5)
let gameOverScene = GameOverScene(size: self.size, won: false)
self.view?.presentScene(gameOverScene, transition: reveal)
}
monster.run(SKAction.sequence([actionMove, loseAction, actionMoveDone]))
}
我认为y坐标有问题。同样在原始教程中,他们没有实际的场景文件,如果有意义的话,您可以将精灵拖放到其中。谢谢你的帮助。
解决方案
推荐阅读
- python - 如何编写一个函数,仅计算类数组对象的前 n 项并返回它们的总和?
- r - RcppTN .cpp 脚本在来源时有效,但在库中编译时无效
- android - RecyclerView item element(imageview)click
- javascript - 如何在 swiper 滑块“.swiper-wrapper”类上添加固定的过渡持续时间值?
- rest - REST API 设计:当 api 必须返回一个字段的相等和不相等列表时,如何处理这种情况?
- c# - 尝试在锚标记助手中使用自定义路由时出错
- r - 如何在 RMarkdown 中合并和打印多个代码块?
- c++ - 在 C++ 中将变量 wstring 分配给 Char 数组
- prometheus - 在普罗米修斯的一项工作中注册多个黑盒出口商
- ansible - 如何在ansible中以不同的参数方式应用条件