-1

有一个球和一个桨,球应该在桨上并接触它,但球从不接触桨并在到达桨之前停止,有人能帮我吗?

在此处输入图像描述

import SpriteKit

class GameScene: SKScene ,SKPhysicsContactDelegate {

    let ballCategory:UInt32 = 0x1 << 0
    let bottomCategory:UInt32 = 0x1 << 1
    let paddleCategory:UInt32 = 0x1 << 2

    override init(size: CGSize){
        super.init(size: size)

        physicsWorld.contactDelegate = self


        let ball = SKSpriteNode(imageNamed: "ball")
        ball.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        addChild(ball)

        ball.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.size.width / 2)
        ball.physicsBody?.dynamic = true
        ball.physicsBody?.allowsRotation = false
        ball.physicsBody?.friction = 0
       // ball.physicsBody?.applyImpulse(CGVectorMake(2,-2))
        ball.physicsBody?.categoryBitMask = ballCategory
        ball.physicsBody?.contactTestBitMask = paddleCategory
        ball.physicsBody?.collisionBitMask = paddleCategory
        ball.physicsBody?.affectedByGravity = true

        let paddle = SKSpriteNode(imageNamed: "paddle")
        paddle.position = CGPointMake(CGRectGetMidX(self.frame), paddle.frame.size.height * 2)
        addChild(paddle)

        paddle.physicsBody = SKPhysicsBody(rectangleOfSize: paddle.frame.size)
        paddle.physicsBody?.affectedByGravity = false
        paddle.physicsBody?.dynamic = false
        paddle.physicsBody?.categoryBitMask = paddleCategory

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    }

    func didBeginContact(contact: SKPhysicsContact) {
            print("touched")

    }

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

2 回答 2

0

确定节点在哪里接触的最佳方法是打开物理。

打开GameViewController.swift并输入以下代码:

skView.showsPhysics =  true
于 2016-03-01T14:45:37.060 回答
0

由于这条线,您遇到了这个问题:

 ball.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.size.width / 2)

self是这里的一个场景。

你需要这个:

 ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.frame.size.width / 2)

在 GameViewController 中打开物理视觉表示,如下所示:

skView.showsPhysics =  true

你会看到发生了什么。

于 2016-03-01T11:06:56.340 回答