当我运行模拟器时,精灵出现在屏幕外此代码来自我从 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 (将#修改为@)