我正在制作一款游戏而且我一直坚持创造一种成功的物理接触方法。
func bulletCollisionWithEnemy1(enemy1: SKSpriteNode, bullet1: SKSpriteNode){ // my enemy1 and bullet are defined in my game scene
score += 1
scoreLabel.text = "\(score)"
bullet1.removeFromParent ()
hitCount++
}
func enemy1Hit (enemy1:Enemy1) { //my enemy1 is defined in my game scene and my Enemy1 is a separate SKSNode class i have created containing physics properties and vars ect.
if (enemy1.hit() == true) {
//enemy hit animation
}else{
//for animations
}
}
这里是我定义接触物理的地方
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory.bullet1) &&
(secondBody.categoryBitMask == PhysicsCategory.enemy1) ||
(firstBody.categoryBitMask == PhysicsCategory.enemy1) &&
(secondBody.categoryBitMask == PhysicsCategory.bullet1)) {
if let firstNode = firstBody.node as? SKSpriteNode, secondNode = secondBody .node as? SKSpriteNode {
bulletCollisionWithEnemy1((firstNode), bullet1: (secondNode))
}
我的问题是我无法找到一个叫做我的enemy1hit()函数。我的hit()函数在我的Enemy1类中定义,因此我必须在参数中调用我的Enemy1以及在与bullet类的碰撞中调用我的命中类。
答案 0 :(得分:0)
基于Enemy1
类是SKSpriteNode
的子类的假设,我认为你想要的是:
class Enemy1: SKSpriteNode {
func bulletCollision(bullet1: SKSpriteNode){ // my enemy1 and bullet are defined in my game scene
score += 1
scoreLabel.text = "\(score)"
bullet1.removeFromParent ()
hitCount++
}
func hit() { //my enemy1 is defined in my game
// scene and my Enemy1 is a separate SKSNode class i have created
//containing physics properties and vars ect.
if (enemy1.hit() == true) {
//enemy hit animation
} else {
//for animations
}
}
}
您的代表在哪里:
func didBeginContact(contact: SKPhysicsContact) {
let firstBody : SKPhysicsBody = contact.bodyA
let secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory.bullet1) &&
(secondBody.categoryBitMask == PhysicsCategory.enemy1) ||
(firstBody.categoryBitMask == PhysicsCategory.enemy1) &&
(secondBody.categoryBitMask == PhysicsCategory.bullet1)) {
if let firstNode = firstBody.node as? Enemy1, secondNode = secondBody .node as? SKSpriteNode {
firstNode.bulletCollision(secondNode)
firstNode.hit()
}
}
}
请记住,bulletCollision
和hit
方法是Enemy1
对象的成员,因此可以在对象上调用,而不必将对象传递给它们。