0
votes

my game is not this but like this:

import SpriteKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    var sprite = SKSpriteNode()

    override func didMoveToView(view: SKView)
    {
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody?.categoryBitMask = 1
        self.physicsBody?.contactTestBitMask = 1
        self.physicsWorld.gravity = CGVectorMake(0, -10)
        self.physicsWorld.contactDelegate = self



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

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        let spriteTexture = SKTexture(imageNamed: "Spaceship")
        sprite = SKSpriteNode(texture: spriteTexture)
        sprite.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
        sprite.size = CGSizeMake(80, 80)
        sprite.physicsBody = SKPhysicsBody(texture: spriteTexture, size: CGSizeMake(80, 80))
        sprite.physicsBody?.categoryBitMask = 1
        sprite.physicsBody?.collisionBitMask = 1
        sprite.physicsBody?.contactTestBitMask = 1
        sprite.physicsBody?.linearDamping = 0;

        self.addChild(sprite)

    }
}

if you paste this code and run you see a lot of "contact" string. I want only 1 contact this: enter image description here so i want when contact only 1 time collision

I edited my question can anyone help?

1
then you need to seperate the objects... what do you want? a contact is a contact - you cannot just say you only want contact, that is not how things work. - luk2302
Give some more info, like how contact is happening and what kind of physics bodies you have. Also use square physics body as this kind of contact happens with physics body made using polygonFromPath - Abhi
I edited, sorry my bad English - Nazım
thank you for all answers, I can fix - Nazım

1 Answers

0
votes

1.Create ‘Base Game’ from Xcode template based on SpriteKit
2.Paste code below to GameScene class

class GameScene: SKScene, SKPhysicsContactDelegate {
    var sprite = SKSpriteNode()

    override func didMoveToView(view: SKView)
    {
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody?.categoryBitMask = 1
        self.physicsBody?.collisionBitMask = 1

        self.physicsWorld.gravity = CGVectorMake(0, -10)
        self.physicsWorld.contactDelegate = self

        self.physicsBody?.restitution = 0.0

    }

    func didEndContact(contact: SKPhysicsContact) {
        print("End contact")
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?)
    {
        let spriteTexture = SKTexture(imageNamed: "Spaceship")
        sprite = SKSpriteNode(texture: spriteTexture)
        sprite.physicsBody = SKPhysicsBody(texture: spriteTexture, size: CGSizeMake(80, 80))

        sprite.size = CGSizeMake(80, 80)

        sprite.position = CGPoint(x: CGRectGetMidX(self.frame) , y: CGRectGetMidY(self.frame))
        sprite.physicsBody?.categoryBitMask = 1
        sprite.physicsBody?.collisionBitMask = 1
        sprite.physicsBody?.contactTestBitMask = 1
        sprite.physicsBody?.fieldBitMask = 1
        sprite.physicsBody?.restitution = 0

        self.addChild(sprite)

    }
}

IMPORTANT NOTE Try to use didEndContact delegate method instead of didBeginContact. In this case you'll get only one invoke while didBeginContact invokes several times.