2016-05-01 61 views
0

我正在制作一个精灵套件中的游戏,要求用户将玩家移动到X轴上以捕捉事物。我试图让玩家不可能离开屏幕,因为那看起来很糟糕。我试图用一个if语句来做这件事,说如果它移过某个特定点将它传回正确的点,但不知道如何做到这一点。这可能是正确的做法,但我不确定。请告诉/告诉我该怎么做才能完成这项工作。我将发布下面的代码。让对象远离特定点

import SpriteKit 



struct physicsCatagory { 
    static let person : UInt32 = 0x1 << 1 
    static let Ice : UInt32 = 0x1 << 2 
    static let IceTwo : UInt32 = 0x1 << 3 
    static let IceThree : UInt32 = 0x1 << 4 
    static let Score : UInt32 = 0x1 << 5 
} 


class GameScene: SKScene, SKPhysicsContactDelegate { 



    var timeOfLastSpawn: CFTimeInterval = 0.0 
    var timePerSpawn: CFTimeInterval = 1.2 
    var scorenumber = Int() 
    var lifenumber = Int() 
    var SpeedNumber : Double = 0.5 
    var person = SKSpriteNode(imageNamed: "Person1") 
    let Score = SKSpriteNode() 
    var ScoreLable = SKLabelNode() 
    let BackGround = SKSpriteNode (imageNamed: "BackGround") 


    override func didMoveToView(view: SKView) { 

     self.scene?.backgroundColor = UIColor.blueColor() 

     physicsWorld.contactDelegate = self 

     self.scene?.size = CGSize(width: 640, height: 1136) 

     lifenumber = 0 
     SpeedNumber = 1 

     BackGround.size = CGSize(width: self.frame.width, height: self.frame.height) 
     BackGround.position = CGPointMake(self.size.width/2, self.size.height/2) 
     BackGround.zPosition = -5 
     self.addChild(BackGround) 


     Score.size = CGSize(width: 648, height: 1) 
     Score.position = CGPoint(x: 320, y: -90) 
     Score.physicsBody = SKPhysicsBody(rectangleOfSize: Score.size) 
     Score.physicsBody?.affectedByGravity = false 
     Score.physicsBody?.dynamic = false 
     Score.physicsBody?.categoryBitMask = physicsCatagory.Score 
     Score.physicsBody?.collisionBitMask = 0 
     Score.physicsBody?.contactTestBitMask = physicsCatagory.IceThree 
     Score.color = SKColor.blueColor() 
     self.addChild(Score) 


     person.zPosition = 1 
     person.position = CGPointMake(self.size.width/2, self.size.height/10) 
     person.setScale(0.32) 
     person.physicsBody = SKPhysicsBody (rectangleOfSize: CGSize(width: 40, height: 50)) 
     person.physicsBody?.affectedByGravity = false 
     person.physicsBody?.categoryBitMask = physicsCatagory.person 
     person.physicsBody?.contactTestBitMask = physicsCatagory.Ice 
     person.physicsBody?.collisionBitMask = physicsCatagory.Ice 
     person.physicsBody?.dynamic = false 




     ScoreLable = SKLabelNode(fontNamed: "Zapfino") 
     ScoreLable.position = CGPoint(x: self.frame.width/2, y: 1000) 
     ScoreLable.text = "\(scorenumber)" 
     ScoreLable.fontColor = UIColor.yellowColor() 
     ScoreLable.fontSize = 100 
     ScoreLable.fontName = "Zapfino " 
     self.addChild(ScoreLable) 





     self.addChild(person) 





    } 




    func didBeginContact(contact: SKPhysicsContact) { 
      let firstBody = contact.bodyA 
      let secondBody = contact.bodyB 


     if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.IceThree || firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.person{ 

      scorenumber++ 

      if scorenumber == 20 { 

       timePerSpawn = 1.0 

      } 

      if scorenumber == 40{ 
       timePerSpawn = 0.89 

      } 
      if scorenumber == 60{ 

       timePerSpawn = 0.6 
      } 
      if scorenumber == 80{ 

       timePerSpawn = 0.5 
      } 

      if scorenumber == 100{ 

       timePerSpawn = 0.4 
      } 

      if scorenumber == 120{ 

       timePerSpawn = 0.3 
      } 



      ScoreLable.text = "\(scorenumber)" 
      CollisionWithPerson(firstBody.node as! SKSpriteNode, Person: secondBody.node as! SKSpriteNode) 


     } 

     if firstBody.categoryBitMask == physicsCatagory.Score && secondBody.categoryBitMask == physicsCatagory.IceThree || 
      firstBody.categoryBitMask == physicsCatagory.IceThree && secondBody.categoryBitMask == physicsCatagory.Score{ 
       lifenumber++ 

       if lifenumber == 1{ 
        //person.texture 
        person.texture = SKTexture (imageNamed: "Flower#2") 
       } 

       if lifenumber == 2{ 
      person.texture = SKTexture (imageNamed: "Flower #3") 

     } 
       if lifenumber == 3{ 
        self.view?.presentScene(EndScene()) 

       } 


     } 
    } 



    func CollisionWithPerson (Ice: SKSpriteNode, Person: SKSpriteNode){ 

     Person.removeFromParent() 

    } 



    func spawnThirdIce(){ 

     var Ice = SKSpriteNode(imageNamed: "Ice") 
     Ice.zPosition = 2 
     Ice.setScale(0.9) 
     Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size) 
     Ice.physicsBody?.categoryBitMask = physicsCatagory.IceThree 
     Ice.physicsBody?.contactTestBitMask = physicsCatagory.person | physicsCatagory.Score 
     Ice.physicsBody?.affectedByGravity = false 
     Ice.physicsBody?.dynamic = true 
     let MinValue = self.size.width/8 
     let MaxValue = self.size.width - 20 
     let SpawnPoint = UInt32(MaxValue - MinValue) 
     Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height) 
     self.addChild(Ice) 

     let action = SKAction.moveToY(-85, duration: 2.0) 
     let actionDone = SKAction.removeFromParent() 
     Ice.runAction(SKAction.sequence([action,actionDone])) 


    } 



    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 

      } 
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 

     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      let previousTouch = touch.previousLocationInNode(self) 
      let ammountDragged = location.x - previousTouch.x 


      person.position.x += ammountDragged 











     } 
    } 




    override func update(currentTime: CFTimeInterval) { 
     /* Called before each frame is rendered */ 
     if (currentTime - timeOfLastSpawn > timePerSpawn) { 
      spawnThirdIce() 
      self.timeOfLastSpawn = currentTime 
     } 
    } 




} 
+0

可能有助于http://stackoverflow.com/questions/21948028/how-to-give-screen-edge-a-physics-body – Shubhank

+0

你可以查看SKConstraint。创建一个'+ positionX:'约束,并将其分配给一个SKNode的约束属性。 – CloakedEddy

回答

0

你可以这样做:

if node.position.x > scene.width { 
     node.position.x = scene.width 
    } 

    if node.position.x < 0 { 
     node.position.x = 0 
}