I have a game (like Doodle Jump) where my player hits objects and bounces off of them to advance and gain points. As the player hits said objects, they should just disappear (.removeFromParent()) as it is called in a function in GameScene and in its own separate class. Can anyone tell me what Im doing wrong?
Here is my code for GameScene:
func createSnowAtPosition(position: CGPoint) -> SKNode {
var sprite: SKSpriteNode!
sprite = SKSpriteNode(imageNamed: "icons_winter_09")
let node = Snowflake()
let position = CGPoint(x: position.x, y: position.y)
node.position = position
node.name = "SNOW"
node.addChild(sprite)
sprite.physicsBody?.affectedByGravity = false
sprite.physicsBody?.categoryBitMask = snowCategory
sprite.physicsBody?.collisionBitMask = playerCategory
sprite.physicsBody = SKPhysicsBody(circleOfRadius: snow.size.width / 7)
sprite.physicsBody?.dynamic = false
return node
}
func didBeginContact(contact: SKPhysicsContact) {
if player.physicsBody?.velocity.dy < 0 {
// 2
player.physicsBody?.velocity = CGVector(dx: player.physicsBody!.velocity.dx, dy: 250.0)
// Play sound
runAction(starSound) {
snow.removeFromParent()
}
if snow.position.y < player.position.y {
let pointsLabel = childNodeWithName("scoreLabel") as! Points
pointsLabel.increment()
}
}
And here is my code for the separate class:
import Foundation
import SpriteKit
let snow = SKSpriteNode(imageNamed: "icons_winter_09")
class Snowflake: SKSpriteNode {
init() {
let size = CGSizeMake(40, 50)
super.init(texture: nil, color: UIColor.clearColor(), size: size)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func loadBodyWithSize(size: CGSize) {
snow.physicsBody?.affectedByGravity = false
snow.physicsBody?.categoryBitMask = snowCategory
snow.physicsBody?.collisionBitMask = playerCategory
snow.physicsBody = SKPhysicsBody(circleOfRadius: snow.size.width / 3)
}
func createSnowsAtPosition() {
snow.name = "snow"
snow.position = CGPointMake(50, 200)
snow.physicsBody?.dynamic = false
snow.physicsBody?.usesPreciseCollisionDetection = true
addChild(snow)
}
}
Define the physics body before you start settings its properties, also don't add the node until the physics body is defined, so:
let node = Snowflake()
let position = CGPoint(x: position.x, y: position.y)
node.position = position
node.name = "SNOW"l
sprite.physicsBody?.affectedByGravity = false
sprite.physicsBody?.categoryBitMask = snowCategory
sprite.physicsBody?.collisionBitMask = playerCategory
sprite.physicsBody = SKPhysicsBody(circleOfRadius: snow.size.width / 7)
sprite.physicsBody?.dynamic = false
addNode(node)
return node
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.