为什么我的联系人识别器不起作用

时间:2018-02-23 17:01:17

标签: swift sprite-kit

我使用物理主体,类别位掩码和SKPhysics联系人制作了联系人识别器。不幸的是它不起作用。你能以某种方式帮我找错吗? 此代码不打印语句Touched。

是因为一个主体是SKSpriteNode还是第二个SKShapeNode?我们可以识别SpriteNode和ShapeNode之间的联系吗?

我做了所有的改进,比如@giorashc用SKPhysics身体说,但没有奏效。

import Foundation
import SpriteKit
import GameplayKit
import UIKit

var TouchLevel3BadgeChecker = 0

enum BodyType:UInt32{
    case TheCircle = 1
    case TheEightBack = 2
}


class TouchLevel3SceneClass: SKScene, SKPhysicsContactDelegate{

    var Eight = SKSpriteNode(imageNamed: "8.png")
    var EightBack = SKSpriteNode(imageNamed: "8Back.png")


    var counter : Int = 0;

    override func didMove(to view: SKView) {



        self.physicsWorld.contactDelegate = self



        Eight.position = CGPoint(x: 0, y: -50)
        Eight.name = "Eight"

        addChild(Eight)


        EightBack.position = CGPoint(x: 0, y: -50)
        EightBack.name = "EightBack"
        EightBack.zPosition = 2
        EightBack.physicsBody? = SKPhysicsBody(texture: EightBack.texture!,
                                                size: EightBack.texture!.size())
        EightBack.physicsBody?.usesPreciseCollisionDetection = true
        EightBack.physicsBody?.isDynamic = true
        EightBack.physicsBody?.affectedByGravity = false
        EightBack.physicsBody?.categoryBitMask = BodyType.TheEightBack.rawValue
        addChild(EightBack)



        Circle.position = CGPoint(x: 0, y: -25)
        Circle.zPosition = 2;
        Circle.fillColor = SKColor.black;
        Circle.name = "Circle"
        Circle.physicsBody? = SKPhysicsBody(circleOfRadius: 30)
        Circle.physicsBody?.usesPreciseCollisionDetection = true
        Circle.physicsBody?.isDynamic = true
        Circle.physicsBody?.affectedByGravity = false
        Circle.physicsBody?.categoryBitMask = BodyType.TheCircle.rawValue
        Circle.physicsBody?.collisionBitMask = BodyType.TheEightBack.rawValue
        Circle.physicsBody?.contactTestBitMask = BodyType.TheEightBack.rawValue
        addChild(Circle)
   }

    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches{
            let location = t.location(in: self);


            if atPoint(location).name == "Circle"{
                Circle.position.x = location.x
                Circle.position.y = location.y
            }
        }
    }


        func didBegin(_ contact: SKPhysicsContact) {
            if contact.bodyA.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyB.categoryBitMask == BodyType.TheEightBack.rawValue{
                print("Touched")
            } else if contact.bodyB.categoryBitMask == BodyType.TheCircle.rawValue && contact.bodyA.categoryBitMask == BodyType.TheEightBack.rawValue{
                print("Touched")
        }
        }
    }

1 个答案:

答案 0 :(得分:0)

您没有创建physicsBody属性。由于其可选和零设置,其属性不会做任何事情。

使用:

创建physicsBody属性
EightBack.physicsBody = SKPhysicsBody(...);
Circle.physicsBody = SKPhysicsBody(...);

查看此链接中的示例:https://developer.apple.com/documentation/spritekit/skphysicsbody