防止物体移动超过某一点

时间:2016-05-01 05:23:37

标签: ios xcode sprite-kit

我正在制作一个精灵套件中的游戏,需要用户将玩家移过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
        }
    }




}

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作:

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

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