HomeiOS Developmentios - Why aren't contacts working in SpriteView

ios – Why aren’t contacts working in SpriteView


I am attempting to get to know when two sprites contact one another however I am not having the ability to get it to work.

The code under reproduces the issue. I anticipated the contact delegate strategies to fireplace once I faucet the purple sq. and the yellow sq. reaches it. Nonetheless, nothing occurs. The code looks as if rather a lot however is definitely fairly easy. I’ve added feedback to make it simpler to learn:

class GameScene: SKScene, SKPhysicsContactDelegate {
    non-public var node: SKSpriteNode!
    
    override func didMove(to view: SKView) {
        // Set contact delegate to this class
        physicsWorld.contactDelegate = self

        // Have the Sport Scene be the identical measurement because the View
        measurement = view.body.measurement

        // Create a yellow sq. with a quantity based mostly physics physique that is not dynamic
        node = SKSpriteNode(shade: .yellow, measurement: CGSizeMake(50, 50))
        node.place = CGPointMake(100, 100)
        node.physicsBody = SKPhysicsBody(rectangleOf: CGSizeMake(50, 50))
        node.physicsBody!.isDynamic = false

        // Set it is contact bit masks to any worth (default class bitmask of 
        // SKSpriteNode is 0xFFFFFFFF so any worth over right here would do)
        node.physicsBody!.contactTestBitMask = 1

        // Add it to the Scene
        addChild(node)
        
        // Create a purple sq. with a quantity based mostly physics physique that is not dynamic
        let otherNode = SKSpriteNode(shade: .purple, measurement: CGSizeMake(50, 50))
        otherNode.physicsBody = SKPhysicsBody(rectangleOf: CGSizeMake(50, 50))
        otherNode.physicsBody!.isDynamic = false

        // Set the place to be 100 pts to the appropriate of the yellow sq.
        otherNode.place = CGPointMake(200, 100)

        // Add it to the Scene
        addChild(otherNode)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with occasion: UIEvent?) {
        // Strikes yellow sq. to the place you tapped on the display
        guard let finger = touches.first?.location(in: self) else { return }
        node.run(.transfer(to: finger, period: 1))
    }

    func didBegin(_ contact: SKPhysicsContact) {
        print("did start contact")
    }
}
struct ContentView: View {
    var physique: some View {
        VStack {
            Textual content("Sport")
            SpriteView(scene: GameScene(), debugOptions: [.showsPhysics, .showsNodeCount])
        }
        .padding()
    }
}

Can anybody inform me what I am doing flawed?

Thanks prematurely

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments