精灵套件:联系不工作

Posted

技术标签:

【中文标题】精灵套件:联系不工作【英文标题】:sprite kit : contact not working 【发布时间】:2015-06-16 12:50:48 【问题描述】:

我正在尝试做一个简单的游戏,这个问题首先发生我的桨可以接触球,但现在它不能这样做,并且 这段代码也应该给我消息或在模拟器中,但它没有显示任何想法?

import SpriteKit

class GameScene: SKScene , SKPhysicsContactDelegate

var istouchingpaddle = false

let ballcatagery:UInt32 = 0 * 1 << 0
let paddlecategary :UInt32 = 0 * 1 << 1


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

    let border = SKPhysicsBody(edgeLoopFromRect: self.frame)

    border.friction = 0
    self.physicsBody = border
    self.physicsWorld.gravity = CGVectorMake(0,-9.8)
    self.physicsWorld.contactDelegate = self
    let ball = childNodeWithName ("ball") as SKSpriteNode
    ball.physicsBody?.applyImpulse(CGVectorMake(30, -30))
    ball.physicsBody?.allowsRotation = false
    ball.physicsBody?.restitution = 1

    ball.physicsBody?.linearDamping = 0
    ball.physicsBody?.angularDamping = 0



    ball.physicsBody!.categoryBitMask = ballcatagery
    let paddle = childNodeWithName("paddle") as SKSpriteNode
    paddle.physicsBody!.categoryBitMask = paddlecategary
    ball.physicsBody?.contactTestBitMask = paddlecategary






func didBeginContact(contact: SKPhysicsContact) 

    if contact.bodyA.categoryBitMask == ballcatagery && contact.bodyB.categoryBitMask == paddlecategary

            println("working ")
    



override func touchesBegan(touches: NSSet, withEvent event: UIEvent) 
    /* Called when a touch begins */
    var touch = touches.anyObject() as UITouch
    var location  = touch.locationInNode(self)
    if let body = self.physicsWorld.bodyAtPoint(location)
    if body.node!.name == "paddle" 

        istouchingpaddle = true
    
    




override func touchesMoved(touches: NSSet, withEvent event: UIEvent) 

    if istouchingpaddle
    var touch = touches.anyObject() as UITouch
    var location  = touch.locationInNode(self)
    var prevlocation = touch.previousLocationInNode(self)
    var paddle = childNodeWithName("paddle") as SKSpriteNode
    var position = paddle.position.x + (location.x - prevlocation.x)
    position = max(position,paddle.size.width/2)
    position = min(position, size.width - paddle.size.width/2)



    paddle.position = CGPoint(x: position, y: paddle.position.y)


            



override func touchesEnded(touches: NSSet, withEvent event: UIEvent) 
    istouchingpaddle = false

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

【问题讨论】:

【参考方案1】:

如果我改变定义类别的方式,我可以用你的代码产生好的结果。尝试像这样设置类别:

let ballcatagery: UInt32 = 0x1 << 0 // Not that you have misspelled word "category", which can lead to errors if you expect somewhere a word "category" instead of your actual "categary". 
let paddlecategary : UInt32 = 0x1 << 1 

我可以将整个代码发布给您,但您应该对此感到满意。

【讨论】:

@DivyPatel 不客气 :) 请注意,0x1 与 0*1(0 乘以 1)不同。 0x1 是 1 的十六进制表示。

以上是关于精灵套件:联系不工作的主要内容,如果未能解决你的问题,请参考以下文章

精灵套件游戏中的计分系统

在 Sprite 套件中未检测到联系人

云套件:如何仅从您的联系人组获取公共数据库订阅通知

使用精灵套件垂直滚动背景的问题

在节点中使用精灵套件问题进行游戏开发?

如何禁用两个 SKSpriteNodes 之间的联系?