雪碧套件接触问题
Posted
技术标签:
【中文标题】雪碧套件接触问题【英文标题】:Sprite kit contact problems 【发布时间】:2016-03-24 01:43:42 【问题描述】:我正在制作一个需要你躲避坠落物体的游戏。我试图让它在坠落的物体撞击玩家时运行某些代码。但它不起作用,请告诉我我的代码有什么问题。
import SpriteKit
struct physicsCatagory
static let person : UInt32 = 0x1 << 1
static let Ice : UInt32 = 0x1 << 2
class GameScene: SKScene, SKPhysicsContactDelegate
var person = SKSpriteNode(imageNamed: "Person")
override func didMoveToView(view: SKView)
physicsWorld.contactDelegate = self
person.position = CGPointMake(self.size.width/2, self.size.height/12)
person.setScale(0.4)
person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
person.physicsBody?.affectedByGravity = false
person.physicsBody?.categoryBitMask = physicsCatagory.person
person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
person.physicsBody?.dynamic = false
var IceTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnFirstIce"), userInfo: nil, repeats: true)
self.addChild(person)
func didBeginContact(contact: SKPhysicsContact)
let firstBody = contact.bodyA
let secondBody = contact.bodyB
if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.Ice || firstBody.categoryBitMask == physicsCatagory.Ice && secondBody.categoryBitMask == physicsCatagory.person
NSLog ("Test Test")
func spawnFirstIce()
let Ice = SKSpriteNode(imageNamed: "FirstIce")
Ice.setScale(0.5)
Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
Ice.physicsBody?.categoryBitMask = physicsCatagory.Ice
Ice.physicsBody?.contactTestBitMask = physicsCatagory.person
Ice.physicsBody?.affectedByGravity = false
Ice.physicsBody?.dynamic = true
let MinValue = self.size.width / 8
let MaxValue = self.size.width - 20
let SpawnPoint = UInt32(MaxValue - MinValue)
Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
self.addChild(Ice)
let action = SKAction.moveToY(-85, duration: 3.0)
let actionDone = SKAction.removeFromParent()
Ice.runAction(SKAction.sequence([action,actionDone]))
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
for touch in touches
let location = touch.locationInNode(self)
person.position.x = location.x
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?)
for touch in touches
let location = touch.locationInNode(self)
person.position.x = location.x
override func update(currentTime: CFTimeInterval)
/* Called before each frame is rendered */
【问题讨论】:
【参考方案1】:您已经在 didMoveToView
方法中定义了 didBeginContact
:)
将didBeginContact
放在该方法之外,并使其成为 GameScene 类的成员。
【讨论】:
噢噢噢噢。非常感谢。我花了一周时间研究这个愚蠢的东西。你是我的救星。 您是否尝试在函数定义中添加 print("Entering didBegingContact") 以查看它是否被调用?以上是关于雪碧套件接触问题的主要内容,如果未能解决你的问题,请参考以下文章
雪碧套件 iOS 7 bodyWithPolygonFromPath 错误