微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当我运行模拟器时,精灵出现在屏幕外此代码来自我从 raywenderlich 在线找到的教程任何帮助深表感谢

如何解决当我运行模拟器时,精灵出现在屏幕外此代码来自我从 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 坐标有问题。同样在原始教程中,他们没有实际的场景文件,您可以将精灵拖放到其中(如果有意义的话)。感谢您的帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。