문제 : 내 플레이어가 동전과 충돌하고 충돌시 동전 라벨에 +1을 추가하는 데 약간 문제가있는 것처럼 보입니다. 플레이어는 동전과 접촉 한 후에 계속 움직여야합니다.PhysicsBodys를 사용하는 기본 Swift SpriteKit 충돌
지금 가지고있는 코드 : 내가 가지고있는 코드로 플레이어는 동전을 통과하지만 충돌은 일어나지 않으며 동전 라벨에는 +1이 추가되지 않습니다.
나는 아직도 빠른 언어를 배우고 있으므로 주어진 도움을 주셔서 감사합니다.
코드 :
struct ColliderType {
static let playerCategory: UInt32 = 0x1 << 0
static let boundary: UInt32 = 0x1 << 1
static let coinCategory: UInt32 = 0x1 << 2
static let bodyA: UInt32 = 0x1 << 4
static let bodyB: UInt32 = 0x1 << 8
}
override func didMoveToView(view: SKView) {
var coinInt = 0
self.physicsWorld.gravity = CGVectorMake(0.0, -7.0)
physicsWorld.contactDelegate = self
player = SKSpriteNode(imageNamed: "player")
player.zPosition = 1
player.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.width/5.12)
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
self.addChild(player)
generateCoins()
coin = SKSpriteNode(imageNamed: "coin")
coin.physicsBody? = SKPhysicsBody(circleOfRadius: coin.size.height/10)
coin.physicsBody?.dynamic = false
coin.physicsBody?.allowsRotation = false
coin.zPosition = 1
self.addChild(coin)
player.physicsBody?.categoryBitMask = ColliderType.playerCategory
player.physicsBody?.contactTestBitMask = ColliderType.boundary
player.physicsBody?.collisionBitMask = ColliderType.coinCategory | ColliderType.boundary
coin.physicsBody?.categoryBitMask = ColliderType.coinCategory
coin.physicsBody?.contactTestBitMask = ColliderType.playerCategory
coin.physicsBody?.collisionBitMask = ColliderType.playerCategory
func didPlayerCollideWithCoin(player: SKSpriteNode, coin: SKSpriteNode) {
self.coin.removeFromParent()
self.coin += 1
coinLabel.text = "\(coinInt)"
}
func generateCoins() {
if(self.actionForKey("spawning") != nil){return}
let coinTimer = SKAction.waitForDuration(7, withRange: 2)
let spawnCoin = SKAction.runBlock {
self.coin = SKSpriteNode(imageNamed: "coin")
self.coin.physicsBody = SKPhysicsBody(circleOfRadius: self.coin.size.height/10)
self.coin.name = "coin"
self.coin.physicsBody?.dynamic = false
self.coin.physicsBody?.allowsRotation = false
var coinPosition = Array<CGPoint>()
coinPosition.append((CGPoint(x:340, y:103)))
coinPosition.append((CGPoint(x:340, y:148)))
coinPosition.append((CGPoint(x:340, y:218)))
coinPosition.append((CGPoint(x: 340, y:343)))
let spawnLocation = coinPosition[Int(arc4random_uniform(UInt32(coinPosition.count)))]
let action = SKAction.repeatActionForever(SKAction.moveToX(+self.xScale, duration: 4.4))
self.coin.runAction(action)
self.coin.position = spawnLocation
self.addChild(self.coin)
print(spawnLocation)
}
let sequence = SKAction.sequence([coinTimer, spawnCoin])
self.runAction(SKAction.repeatActionForever(sequence), withKey: "spawning")
}
func didBeginContact(contact:SKPhysicsContact) {
let bodyA: SKPhysicsBody = contact.bodyA
let bodyB: SKPhysicsBody = contact.bodyB
if ((bodyA.categoryBitMask == ColliderType.playerCategory) && (bodyB.categoryBitMask == ColliderType.coinCategory)){
didPlayerCollideWithCoin(bodyA.node as! SKSpriteNode, coin: bodyB.node as! SKSpriteNode)
}
}
왜'(circleOfRadius : coinOne.size.height/10 자세한 내용은
레이 Wenderlich에서이 자습서를 참조)'(circleOfRadius : coinOne.size.height/2)'가 아니어야합니까? –
"/"뒤에 오는 숫자는 물리학이 얼마나 큰지를 제어한다고 생각했습니다./2,/3을 사용하는 경우와 같이 physicsBody는 더 높은 숫자를 사용하는 것에 비해 더 커집니다. 내가 틀렸을 수도있다. –
반지름 (지름)은 직경/2입니다. 동전의 높이 == 동전의 지름 (아마 그렇게 할 것입니다.)이 값을 2로 나눈 값은 아마도 여러분이 가지고있는 크기가 될 것입니다. –