2015-04-14 41 views
0

我正在與在屏幕上移動的雪碧一場比賽,我已經建立了使用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) 

回答

1

檢查以確保您的sceneview設置到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,你會走出屏幕界限。

還做showPhysics =真在你SKView顯示你的碰撞邊界是什麼。

+0

謝謝。我將它從.AspectFill改爲Aspect.Fit,遊戲縮小了,我可以看到邊緣,但它不是我想要的樣子。這是因爲我的背景圖片太大,對此感到困惑。 – JMD

+0

也趁勢現在碰到斷過它們的邊緣,但隨後「通行證」,並消失 – JMD

+0

我假設你正在用故事板一個通用的應用程序?你從不在場景物理體上設置collisionMask,我不確定它是否默認爲0xFFFF。你有沒有顯示你的碰撞界限,以確保他們在正確的地方? – Knight0fDragon