如何设置它以便只有我的播放器和物体具有物理体?

时间:2022-09-25 12:45:37

Creating a game like Doodle Jump, except for bouncing off platforms, my hero bounces off objects to collect points and progress in the game. As my hero "bounces off" the object, I set it to removeFromParent and it is working like it should. However, if my hero misses the object and accidentally hits the wall or ground, the object disappears as well which is odd because my ground doesn't have a physics body.

创建像Doodle Jump这样的游戏,除了从平台上弹跳之外,我的英雄反弹物体以收集点数并在游戏中取得进展。当我的英雄“反弹”对象时,我将其设置为removeFromParent并且它的工作方式应该如此。然而,如果我的英雄错过了物体并意外撞到了墙壁或地面,那么物体也会消失,这很奇怪,因为我的地面没有物理体。

Here is my code for my GameScene:

这是我的GameScene的代码:

 import SpriteKit

 var player: Player!
 var ground: Ground!
 var snowflake: Snowflake!
 var isFingerOnPlayer = false
 var gameOver = false
 var playerTouched = false
 var touching: Bool = false
 var lastTouch: CGPoint? = nil
 let xPlayerForce: CGFloat = 40
 let yPlayerForce: CGFloat = 50
 var touchPoint: CGPoint = CGPoint()

 struct Collision {
    static let player: UInt32 = 1
    static let snowflake: UInt32 = 2
 }

 class GameScene: SKScene, SKPhysicsContactDelegate {
    override func didMoveToView(view: SKView) {
        player = Player()
        player.position = CGPointMake(20, 150)
        addChild(player)

        ground = Ground()
        ground.position = CGPointMake(10, -37)
        addChild(ground)

        player.physicsBody?.linearDamping = 0.0
        player.physicsBody?.angularDamping = 500.0
        player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
        player.physicsBody?.allowsRotation = false
        player.physicsBody?.mass = 0
        player.physicsBody?.friction = 1
        player.physicsBody?.dynamic = true
        physicsWorld.contactDelegate = self
        physicsWorld.gravity = CGVector(dx: 0.0, dy: -3.0)

        self.backgroundColor = SKColor(red: 0.15, green: 0.15, blue: 0.3, alpha: 1.0)
        snowflake = Snowflake()
        addChild(snowflake)
        self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
    }

    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let touchLocation = touch.locationInNode(self)
            lastTouch = touchLocation

            player.physicsBody?.categoryBitMask = snowCategory
            player.physicsBody?.contactTestBitMask = playerCategory
            player.physicsBody?.affectedByGravity = true
            player.physicsBody?.collisionBitMask = playerCategory
            player.physicsBody?.usesPreciseCollisionDetection = true
        }
    }

    func touchedPlayer() {
        playerTouched = true
        snowflake.removeFromParent()
        player.bounceOff()
    }

    override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
        lastTouch = nil
    }

    override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
        lastTouch = nil
    }

    override func update(currentTime: NSTimeInterval) {
        if let touch = lastTouch {
            var xForce: CGFloat = 0.0
            var yForce: CGFloat = 0.0
            let xTouchOffset = (touch.x - player.position.x)
            let yTouchOffset = (touch.y - player.position.y)

            if xTouchOffset > 0.0 {
                xForce = xPlayerForce
            } else if xTouchOffset < 0.0 {
                xForce = -xPlayerForce
            } // else we do nothing

            if yTouchOffset > 0.0 {
                yForce = yPlayerForce
            } else if xTouchOffset > 0.0 {
                yForce = -yPlayerForce
            }
            // here you can choose whether you want it to push
            // the player node down, using similar code from the
            // above if statement

            let impulseVector = CGVector(dx: xForce, dy: yForce)
            player.physicsBody?.applyImpulse(impulseVector)
        }
    }

    func didBeginContact(contact: SKPhysicsContact) {
        touchedPlayer()
    }
}

1 个解决方案

#1


0  

In spite of the fact that it's not clear what is snow flake entity.

尽管事实上还不清楚什么是雪片实体。

There is a code:

有一个代码:

 self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

You have an edge around your frame.

你的框架周围有一个边缘。

From the description:

从描述:

Creates an edge loop from a CGRect. Edges have no volume and are intended to be used to create static environments. Edges can collide with body's of volume, but not with each other.

从CGRect创建边循环。边缘没有卷,旨在用于创建静态环境。边缘可以与体积相互碰撞,但不会相互碰撞。

In the method func didBeginContact(contact: SKPhysicsContact) use contact.contactBodyA.categoryBitMask to ignore this contact.(Should be zero).

在方法func中,didBeginContact(contact:SKPhysicsContact)使用contact.contactBodyA.categoryBitMask忽略此联系。(应为零)。

#1


0  

In spite of the fact that it's not clear what is snow flake entity.

尽管事实上还不清楚什么是雪片实体。

There is a code:

有一个代码:

 self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)

You have an edge around your frame.

你的框架周围有一个边缘。

From the description:

从描述:

Creates an edge loop from a CGRect. Edges have no volume and are intended to be used to create static environments. Edges can collide with body's of volume, but not with each other.

从CGRect创建边循环。边缘没有卷,旨在用于创建静态环境。边缘可以与体积相互碰撞,但不会相互碰撞。

In the method func didBeginContact(contact: SKPhysicsContact) use contact.contactBodyA.categoryBitMask to ignore this contact.(Should be zero).

在方法func中,didBeginContact(contact:SKPhysicsContact)使用contact.contactBodyA.categoryBitMask忽略此联系。(应为零)。