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

标签: swiftxcodesprite-kit

解决方案


推荐阅读