UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

SOLVED: Dive Into Sprite Kit -> sprite shows up on simulator but not on device

Forums > iOS

Hello,

I'm working on Project one from the Dive Into Sprite Kit book. I'm doing the racer version and just added the player (motorbike). Everything works fine on the simulator but when I try it on my iPad (Air 2, same version as simulator) the player doesn't show up. The background and the particles work fine but I can't see the player. I have retraced my steps to make sure I'm writing the code as the book says. I have googled the problem but still can't find an answer. Anyone have any suggestions?

This is the code I have now:

import SpriteKit
import CoreMotion

@objcMembers
class GameScene: SKScene {
  let player = SKSpriteNode(imageNamed: "player-motorbike.png")

  let motionManager = CMMotionManager()
  var gameTimer: Timer?

  override func didMove(to view: SKView) {
    // this method is called when your game scene is ready to run
    let background = SKSpriteNode(imageNamed: "road.jpg")
    background.zPosition = -1
    addChild(background)

    if let particles = SKEmitterNode(fileNamed: "Mud") {
        // Creates 10 s worth of particles as soon as the game starts
        particles.advanceSimulationTime(10)
        // start emiting at edge of iPad
        particles.position.x = 512
        // add to scene
        addChild(particles)

        player.position.x = -400
        player.zPosition = 1
        addChild(player)

        motionManager.startAccelerometerUpdates()

    }
  }

  override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
      // this method is called when the user touches the screen
  }

  override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
      // this method is called when the user stops touching the screen
  }

  override func update(_ currentTime: TimeInterval) {
    // this method is called before each frame is rendered
    if let accelerometerData = motionManager.accelerometerData {
        let changeX = CGFloat(accelerometerData.acceleration.y) * 100
        let changeY = CGFloat(accelerometerData.acceleration.x) * 100
        player.position.x -= changeX
        player.position.y += changeY
    }
  }
}

Thank you

3      

As I understand it, the player starts off the screen and the movement is controlled by the accellerometer, right? So first off, are you sure you're turning the device in the right direction? You might be sending the player further off the screen. Maybe try centering it on the screen to see where it is going first (just to see if that is or isn't the problem).

4      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.