gpt4 book ai didi

Swift didBeginContact() 未被调用

转载 作者:行者123 更新时间:2023-11-28 13:17:09 26 4
gpt4 key购买 nike

当我将 println() 放入我的 didBeginContact 时,没有任何打印。因此,我假设我的 didBeginContact 不起作用。为什么?我还有一个枚举(对撞机类型),其中列出了 Cup 和 Star 的原始值。

这是我的 GameScene 文件。

import Foundation
import SpriteKit

class PlayScene: SKScene, SKPhysicsContactDelegate {


let cup = SKSpriteNode(imageNamed: "cup")

override func didMoveToView(view: SKView) {
/* Setup your scene here */

self.physicsWorld.contactDelegate = self


self.backgroundColor = UIColor(red: 192, green: 192, blue: 192, alpha: 1)

cup.setScale(0.5)
cup.position = CGPointMake(self.size.width + cup.size.width / 2, self.size.height/2)

self.addChild(cup)

let actualDuration = 2.0

let actualY = size.height / 2

let actionMove = SKAction.moveByX(-550, y: 0, duration: actualDuration)

let reverseActionMove = actionMove.reversedAction()

let sequence = SKAction.sequence([actionMove, reverseActionMove])

let endlessAction = SKAction.repeatActionForever(sequence)

self.cup.physicsBody = SKPhysicsBody(rectangleOfSize: self.cup.size)
self.cup.physicsBody?.dynamic = false
self.cup.physicsBody?.categoryBitMask = ColliderType.Cup.rawValue
self.cup.physicsBody?.collisionBitMask = ColliderType.Star.rawValue



//Code to keep stars in screen
//let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
//borderBody.friction = 0
//self.physicsBody = borderBody

cup.runAction(endlessAction)

}




override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch: AnyObject in touches {
let star = StarNode.star(touch.locationInNode(self))
star.setScale(0.3)
self.addChild(star)
}
}

func didBeginContact(contact: SKPhysicsContact) {
println("test")
let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask
switch contactMask {
case ColliderType.Star.rawValue | ColliderType.Cup.rawValue:
println("star with da cup")
default:
return

}
}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}

}

这是我的 StarNode 文件。

import Foundation
import UIKit
import SpriteKit

class StarNode: SKSpriteNode, SKPhysicsContactDelegate {
class func star(location: CGPoint) -> StarNode {
let sprite = StarNode(imageNamed:"star")

sprite.xScale = 0.075
sprite.yScale = 0.075
sprite.position = location

sprite.physicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "star"), size: sprite.size)
sprite.physicsBody?.dynamic = true
sprite.physicsBody?.categoryBitMask = ColliderType.Star.rawValue
sprite.physicsBody?.collisionBitMask = ColliderType.Cup.rawValue

if let physics = sprite.physicsBody {
physics.affectedByGravity = true
physics.friction = 0.12
physics.allowsRotation = true
physics.dynamic = true
physics.linearDamping = 0.75
physics.angularDamping = 0.75
sprite.name = "star"
}
return sprite
}
}

最佳答案

您需要将 contactTestBitMask 设置为您的 SKSpriteNodes。 collisionBitMask 仅说明哪些类别的物体可以与节点发生碰撞,但不调用 didBeginContact 方法。

self.cup.physicsBody?.contactTestBitMask = ColliderType.Star.rawValue

//Star
sprite.physicsBody?.contactTestBitMask = ColliderType.Cup.rawValue

contactTestBitMask文档说:

A mask that defines which categories of bodies cause intersection notifications with this physics body.

关于Swift didBeginContact() 未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28572175/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com