I am trying to update a sprite's image based on the tilt of the phone. My code compiles, prints the tilt, and says that it is loading the image however, when I run the app the image does not change or update when I move my phone. Any suggestions? I've only added what I think is relevant. When I add a fake image file I get the error (SKTexture: Error loading image resource: "FAKEIMAGE"). This tells me that when I have the correct image name it is updating in the code, just not showing up on my screen?
func createPlayer() -> SKNode {
let playerNode = SKNode()
playerNode.position = CGPoint(x: self.size.width / 2, y: 80.0)
var playerSprite: SKSpriteNode = SKSpriteNode(imageNamed: "alienPink_swim1")
playerNode.addChild(playerSprite)
// 1
playerNode.physicsBody = SKPhysicsBody(circleOfRadius: playerSprite.size.width / 2)
// 2
playerNode.physicsBody?.dynamic = false
// 3
playerNode.physicsBody?.allowsRotation = false
// 4
playerNode.physicsBody?.restitution = 1.0
playerNode.physicsBody?.friction = 0.0
playerNode.physicsBody?.angularDamping = 0.0
playerNode.physicsBody?.linearDamping = 0.0
// 1
playerNode.physicsBody?.usesPreciseCollisionDetection = true
// 2
playerNode.physicsBody?.categoryBitMask = CollisionCategoryBitmask.Player
// 3
playerNode.physicsBody?.collisionBitMask = 0
// 4
playerNode.physicsBody?.contactTestBitMask = CollisionCategoryBitmask.Star | CollisionCategoryBitmask.Platform
return playerNode
}
func changePlayerSprite () {
let playerNode = SKNode()
playerNode.position = CGPoint(x: self.size.width / 2, y: 80.0)
var playerSprite: SKSpriteNode
if self.xAcceleration > 0.3{
playerSprite = SKSpriteNode(imageNamed: "alienPink_swim11")
println("Right")
playerNode.addChild(playerSprite)
}
else if self.xAcceleration < -0.3{
playerSprite = SKSpriteNode(imageNamed: "alienPink_swim1")
println("left")
playerNode.addChild(playerSprite)
}
else {
playerSprite = SKSpriteNode(imageNamed: "p3_front")
println("middle")
playerNode.addChild(playerSprite)
}
}
override func update(currentTime: NSTimeInterval) {
// CoreMotion
// 1
motionManager.accelerometerUpdateInterval = 0.2
// 2
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue(), withHandler: {
(accelerometerData: CMAccelerometerData!, error: NSError!) in
// 3
let acceleration = accelerometerData.acceleration
// 4
self.xAcceleration = (CGFloat(acceleration.x) * 0.75) + (self.xAcceleration * 0.25)
})
changePlayerSprite()
}
A more compact implementation of GameScene...
class GameScene: SKScene {
var node:SKNode!
override func didMoveToView(view: SKView) {
var xAcceleration:CGFloat = 0.0
node = createPlayer()
// CoreMotion
if let playerSprite = node.childNodeWithName("playerSprite") as? SKSpriteNode {
let motionManager = CMMotionManager()
motionManager.accelerometerUpdateInterval = 0.2
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()) {
[weak self] accelerometerData, error in
var acceleration = accelerometerData.acceleration
xAcceleration = (CGFloat(acceleration.x) * 0.75) + (xAcceleration * 0.25)
self!.node.position = CGPoint(x: self!.size.width / 2, y: 80.0)
var texture:SKTexture!
switch (xAcceleration) {
case let x where x > 0.3:
texture = SKTexture(imageNamed: "alienPink_swim11")
case let x where x < -0.3:
texture = SKTexture(imageNamed: "alienPink_swim1")
default:
texture = SKTexture(imageNamed: "p3_front")
}
playerSprite.texture = texture
}
}
}
func createPlayer() -> SKNode {
let playerNode = SKNode()
playerNode.position = CGPoint(x: self.size.width / 2, y: 80.0)
var playerSprite: SKSpriteNode = SKSpriteNode(imageNamed: "alienPink_swim1")
playerSprite.name = "playerSprite"
playerNode.addChild(playerSprite)
// 1
playerNode.physicsBody = SKPhysicsBody(circleOfRadius: playerSprite.size.width / 2)
// 2
playerNode.physicsBody?.dynamic = false
// 3
playerNode.physicsBody?.allowsRotation = false
// 4
playerNode.physicsBody?.restitution = 1.0
playerNode.physicsBody?.friction = 0.0
playerNode.physicsBody?.angularDamping = 0.0
playerNode.physicsBody?.linearDamping = 0.0
// 1
playerNode.physicsBody?.usesPreciseCollisionDetection = true
// 2
playerNode.physicsBody?.categoryBitMask = 1
// 3
playerNode.physicsBody?.collisionBitMask = 0
// 4
playerNode.physicsBody?.contactTestBitMask = 2
return playerNode
}
}
You should use the SKAction methods for change the Textures. Its the cleaner way.
Something like this:
var node: SKSpriteNode = SKSpriteNode(imageNamed: "imageassetname")
func doMagic() {
let texture = SKTexture(imageNamed: "p3_front")
var action:SKAction = SKAction.setTexture(texture)
node.runAction(action)
}
With this way, you can define the actions for your character node at a function. Example:
enum pos {
case top
case bot
case left
case right
}
var node: SKSpriteNode = SKSpriteNode(imageNamed: "imageassetname")
func doMagic(direction: pos) -> SKAction {
var texture: SKTexture = SKTexture()
switch direction {
case .bot:
texture = SKTexture(imageNamed: "p3_front")
case .left: let texture = SKTexture(imageNamed: "leftimage")
default:
texture = SKTexture(imageNamed: "rightimage")
}
var action:SKAction = SKAction.setTexture(texture)
return action
}
func change() {
node.runAction(doMagic(pos.bot))
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.