精灵不停留在屏幕上(左和右)

Sprite not staying on screen (left and right)

我正在制作一个带有在屏幕上移动的 Sprite 的游戏,我使用 self.physicsBody = SKPhysicsBody(edgeLoopF​​romRect: self.frame) 为边缘创建了碰撞 这似乎适用于屏幕的顶部和底部,但 Sprite 的左侧和右侧刚好移出屏幕。我不确定这是为什么,感谢对初学者的任何建议。我包含了我设置的滚动背景的代码。我还有其他敌方精灵在屏幕外生成并进入视野,这可能会影响边界吗?

class GameScene: SKScene, SKPhysicsContactDelegate{

struct PhysicsCategory {
  static let None      : UInt32 = 0
  static let All       : UInt32 = UInt32.max    
  static let Edge   : UInt32 = 0b1
  static let Spaceman: UInt32 = 0b10
  static let Ship: UInt32 = 0b11

}

override func didMoveToView(view: SKView) {
    /* Setup your scene here */



    self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    self.physicsBody?.categoryBitMask = PhysicsCategory.Edge
    physicsWorld.contactDelegate = self


    // Repeating Background
    var bgTexture = SKTexture(imageNamed: "spacebackground")
    var movebg = SKAction.moveByX(-bgTexture.size().width, y: 0, duration: 9)
    var replacebg = SKAction.moveByX(bgTexture.size().width, y: 0, duration: 0)
    var moveForever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))
    for var i:CGFloat=0; i<3; i++ {

        var wave = SKSpriteNode(texture: bgTexture)
        wave.position = CGPoint(x: bgTexture.size().width/2 + bgTexture.size().width * i, y: CGRectGetMidY(self.frame))
        wave.size.height = self.frame.height

        wave.runAction(moveForever)

        self.addChild(wave) 
    }


    Spaceman.texture = (Texture1)
    Spaceman.position = CGPoint(x: self.frame.size.width/2, y: self.frame.size.height/2)
   Spaceman.setScale(0.4)



    Spaceman.physicsBody=SKPhysicsBody(rectangleOfSize: TheBat.size)
    Spaceman.physicsBody?.affectedByGravity = true
    Spaceman.physicsBody?.dynamic = true
    Spaceman.physicsBody?.allowsRotation = false
    Spaceman.physicsBody?.categoryBitMask = PhysicsCategory.Spaceman
    Spaceman.physicsBody?.contactTestBitMask = PhysicsCategory.Ship
    Spaceman.physicsBody!.collisionBitMask = PhysicsCategory.Edge

    self.addChild(Spaceman)

检查以确保您的场景视图设置为 viewcontroller 的边界,这应该在您将场景添加到视图的地方完成,应该是其中之一

/* Set the scale mode to scale to fit the window */
    scene.size = skView.bounds.size
    scene.scaleMode = .ScaleToFit;

/* Set the scale mode to scale to fit the window */
    scene.size = skView.bounds.size
    scene.scaleMode = .AspectFit;

如果是.AspectFill,你会超出屏幕边界。

也可以在您的 SKView 中执行 showPhysics = true 以显示您的碰撞边界。