Search code examples
iosxcodeswiftcore-motion

node leave left side screen enter opposite side of screen


oke so i have the following code that makes my skspritenode move on the x-axis see the code below:

manager.startAccelerometerUpdates()
    manager.accelerometerUpdateInterval = 0.1
    manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
        (data, error) in

        let currentX = self.Player.position.x

        if data!.acceleration.x < 0 {
            self.destX = currentX + CGFloat(data!.acceleration.x * 1000)
        }

        else if data!.acceleration.x > 0 {
            self.destX = currentX + CGFloat(data!.acceleration.x * 1000)
        }

        print(currentX)
    }

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */

    let action = SKAction.moveToX(destX, duration: 0.1)
    self.Player.runAction(action)
}

now when the skspritenode leaves the left or right of the screen i want it to reappear on the other side of the screen. i tried adding the following line code:

else if currentX >= self.size.width-5 {
            self.destX = 5
        }

but this sadly didnt work. could someone explain me how to do this?

oke so this is how the update function looks now:

override func update(currentTime: CFTimeInterval) {
    /* Called before each frame is rendered */


    let action = SKAction.moveToX(destX, duration: 0.1)
    self.Player.runAction(action)

    if self.Player.position.x < CGRectGetMinX(self.frame) - (Player.size.width / 2) {

        self.player.position.x = CGRectGetMaxX(self.frame) - (self.Player.size.width / 2)

    }

    else if self.Player.position.x > CGRectGetMaxX(self.frame) + (Player.size.width / 2) {

        self.player.position.x = CGRectGetMinX(self.frame) + (self.Player.size.width / 2)
    }
}

Solution

  • In regards to the player and the screen why dont you add something like this into the update method

     if self.Player.position.x < CGRectGetMinX(self.frame) - (Player.size.width / 2) {
    
        self.player.position.x = CGRectGetMaxX(self.frame) - (self.Player.size.width / 2)
    
    } 
    
    else if self.Player.position.x > CGRectGetMaxX(self.frame) + (Player.size.width / 2) {
    
        self.player.position.x = CGRectGetMinX(self.frame) + (self.Player.size.width / 2)
    }
    

    I tried to get it to work with your moving code but I couldn't, think its the SKAction in the updateMethod.

    Try this instead.

    Create this above your GameScene class underneath the import SpriteKit line

     struct PhysicsCategory {
         static let player: UInt32 = 0x1 << 0
         static let enemy: UInt32 = 0x1 << 1
     }
    

    to create your physics categories. If you are unsure about this read some tutorials about SKPhysicsBodies, there are plenty on the web. Sooner or later you will have to anyway.

    Now give you player a physics body. Go to where you are creating your player and add this code just before adding it to scene.

        player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
        player.physicsBody?.categoryBitMask = PhysicsCategory.player
        player.physicsBody?.collisionBitMask = 0
        player.physicsBody?.dynamic = true
        player.physicsBody?.affectedByGravity = false
    
        addChild(player)
    

    and change your motion code to this

     manager.startAccelerometerUpdates()
    manager.accelerometerUpdateInterval = 0.1
    manager.startAccelerometerUpdatesToQueue(NSOperationQueue.mainQueue()){
        (data, error) in
    
         /// use this to limit the max speed of player in both directions
         guard player.physicsBody!.velocity.dx > -500 && node.physicsBody!.velocity.dx < 500 else { return }
    
        /// if your game is in landscape
        if UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft {
                self.player.physicsBody!.applyForce(CGVectorMake(-500 * CGFloat(data!.acceleration.y), 0))
            } else {
               self.player.physicsBody!.applyForce(CGVectorMake(500 * CGFloat(data!.acceleration.y), 0))
            }
    
    
        /// if your game is in portrait
            self.player.physicsBody!.applyForce(CGVectorMake(500 * CGFloat(data!.acceleration.y), 0))
    }
    

    Comment out the code that you dont need depending on your orientation. Play around with the 500 value to get your desired speed.

    Hope this helps