【问题标题】:Sprite not staying on screen (left and right)精灵不停留在屏幕上(左右)
【发布时间】:2015-04-14 19:07:13
【问题描述】:

我正在制作一个带有在屏幕上移动的 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)

【问题讨论】:

    标签: ios swift sprite-kit edge-detection


    【解决方案1】:

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

    /* 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 以显示碰撞边界。

    【讨论】:

    • 谢谢。我将它从 .AspectFill 更改为 Aspect.Fit 并且游戏被缩小了,我可以看到边缘,但这不是我想要的样子。是不是因为我的背景图太大,对此感到困惑。
    • 此外,Spite 现在会击中边缘,然后“穿过”边缘并消失
    • 我假设您正在制作一个带有故事板的通用应用程序?你从来没有在场景物理体上设置碰撞掩码,我不确定它是否默认为 0xFFFF。您是否显示了碰撞边界以确保它们位于正确的位置?
    • 嗨,我正在制作一个 iPhone 应用程序,我希望它仅是纵向模式,我在部署信息选项下选择了它,所以我不确定为什么左右边距延伸到屏幕之外这么远边缘。目前我只是在编辑主要的游戏场景,我还没有研究故事板,但会对其进行一些研究。还设置了如下的碰撞位掩码,精灵从边缘碰撞,然后通过 - self.physicsBody?.categoryBitMask = PhysicsCategory.Edge physicalWorld.contactDelegate = self
    • 在分配时需要一个碰撞位掩码。它的工作原理是您使用 categoryBitMask 来识别您的精灵,然后使用 collisionBitMask 告诉精灵它可以与什么发生碰撞,最后使用 contactBitMask 来确定它应该接触哪些其他精灵,但不被视为碰撞。如果您使用默认设置制作游戏场景,那么您的游戏场景环境类似于 600x600。现在请记住,从 IOS 8(640x960,640,1136、750x1334 和 1242×2208)开始,Iphone 共有 4 个有效分辨率和 1 个弃用分辨率(弃用 320x480)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多