簡體   English   中英

精靈不在螢幕上(左右)

[英]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)

檢查以確保您的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,則將超出屏幕范圍。

還要在SKView中執行showPhysics = true來顯示碰撞范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM