简体   繁体   中英

Sprite-kit collison

All this code in my project works fine, but I am having some problems. When the plane touches the border of the screen it starts to spin after contact. I can't figure out how to make it so it doesn't spin when it hits the border of the screen. It only has this problem when I use:

plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)

Other forms of definition I have no problem, but I need it to be the size of the texture.

plane = SKSpriteNode(imageNamed: sett.store() as! String)
plane.size = CGSize(width: 80, height: 80)
plane.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 4)
// scorenode collison off, makes images spin on any contact
plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)
plane.physicsBody?.categoryBitMask = Physics.Plane
plane.physicsBody?.collisionBitMask = Physics.Bird
plane.physicsBody?.contactTestBitMask = Physics.Bird | Physics.Score | Physics.Coin
plane.physicsBody?.affectedByGravity = true
plane.physicsBody?.dynamic = true
plane.zPosition = 2
self.addChild(plane)

// screen boarder so cant leave screen
let boredBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
boredBody.friction = 0
self.physicsBody = boredBody

My other problem is again with the the plane

plane.physicsBody = SKPhysicsBody(texture: plane.texture!, size: plane.size)

but as well I need this so it's the size of the texture. When it hits my score node on the screen it should only add 1 point but it varies 1-about 60 points when it crosses the scoreNode. How would i fix this?

let scoreNode = SKSpriteNode()
scoreNode.size = CGSize(width: 800, height: 1)
scoreNode.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 350)
scoreNode.physicsBody = SKPhysicsBody(rectangleOfSize: scoreNode.size)
scoreNode.physicsBody?.affectedByGravity = false
scoreNode.physicsBody?.dynamic = false
scoreNode.physicsBody?.categoryBitMask = Physics.Score
scoreNode.physicsBody?.collisionBitMask = 0
scoreNode.physicsBody?.contactTestBitMask = Physics.Plane
scoreNode.color = SKColor.whiteColor()

// when scoreNode and plane touch && initates score and highScore
if firstBody.categoryBitMask == Physics.Score && secondBody.categoryBitMask == Physics.Plane || firstBody.categoryBitMask == Physics.Plane && secondBody.categoryBitMask == Physics.Score {
    score += 1
    if score >= highScore{
        highScore = score
        scoreLabel.text = "SCORE: \(score)"
        highScoreLabel.text = "HIGH SCORE: \(highScore)"
        let highScoreDefault = NSUserDefaults.standardUserDefaults()
        highScoreDefault.setValue(highScore, forKey: "highScore")
        highScoreDefault.synchronize()
    } else {
        scoreLabel.text = "SCORE: \(score)"
        highScoreLabel.text = "HIGH SCORE: \(highScore)"
    }
}

It's spining because the collision has imparted angular velocity on the physics body. This is as it should be. If you really never want an object to have angular velocity you can do that by setting the node physics body

allowsRotation property to false.

in this case it would be something like:

scoreNode.physicsBody?.allowsRotation = false

https://developer.apple.com/library/ios/documentation/SpriteKit/Reference/SKPhysicsBody_Ref/index.html#//apple_ref/occ/instp/SKPhysicsBody/allowsRotation

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM