Sprite Kit超过1次碰撞

时间:2023-01-22 23:59:42

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: Sprite Kit超过1次碰撞 so i want when contact only 1 time collision

如果您粘贴此代码并运行,您会看到很多“联系”字符串。我想只有1个联系人:所以我想在联系时只碰一次

I edited my question can anyone help?

我编辑了我的问题可以有人帮忙吗?

1 个解决方案

#1


0  

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

1.从基于SpriteKit的Xcode模板创建“基础游戏”2.将下面的代码添加到GameScene类

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.

重要说明尝试使用didEndContact委托方法而不是didBeginContact。在这种情况下,只有一次调用,而didBeginContact会多次调用。

#1


0  

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

1.从基于SpriteKit的Xcode模板创建“基础游戏”2.将下面的代码添加到GameScene类

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.

重要说明尝试使用didEndContact委托方法而不是didBeginContact。在这种情况下,只有一次调用,而didBeginContact会多次调用。