如何防止 Sprite 节点相互推挤?

How to prevent Sprite Nodes from pushing each other?

3 个 Sprite 节点以不同的速度向上移动。你看大猩猩喜欢休息,但是龙虾却在把它慢慢往上推。预期的行为是,当大猩猩再次开始移动时,龙虾也会休息并继续向上移动。使用 .isDynamic = false 不起作用 — Sprite 节点重叠并且龙虾完全忽略了刺猬和大猩猩的物理 body。

录制我的游乐场场景:https://youtu.be/l47kaJiJvkM

视频截图:

代码:

import SpriteKit

public class WalkingScene: SKScene {

public override func didMove(to view: SKView) {
    backgroundColor = .white

    view.showsPhysics = true

    let hedgehog = SKSpriteNode(imageNamed: "hedgehog")
    hedgehog.size = CGSize(width: 64, height: 64)
    hedgehog.position = CGPoint(x: size.width/2, y: 400)
    hedgehog.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 80, height: 80))
    hedgehog.physicsBody?.affectedByGravity = false
    hedgehog.physicsBody?.allowsRotation = false


    let gorilla = SKSpriteNode(imageNamed: "gorilla")
    gorilla.size = CGSize(width: 64, height: 64)
    gorilla.position = CGPoint(x: size.width/2, y: 140)
    gorilla.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 80, height: 80))
    gorilla.physicsBody?.affectedByGravity = false
    gorilla.physicsBody?.allowsRotation = false


    let lobster = SKSpriteNode(imageNamed: "lobster")
    lobster.size = CGSize(width: 64, height: 64)
    lobster.position = CGPoint(x: size.width/2, y: 20)
    lobster.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 80, height: 80))
    lobster.physicsBody?.affectedByGravity = false
    lobster.physicsBody?.allowsRotation = false


    //        hedgehog.physicsBody?.isDynamic = false
    //        gorilla.physicsBody?.isDynamic = false
    //                lobster.physicsBody?.isDynamic = false

    //        hedgehog.physicsBody?.usesPreciseCollisionDetection = true
    //                hedgehog.physicsBody?.collisionBitMask = 0
    //        gorilla.physicsBody?.usesPreciseCollisionDetection = true
    //                gorilla.physicsBody?.collisionBitMask = 0
    //        lobster.physicsBody?.usesPreciseCollisionDetection = true
    //                lobster.physicsBody?.collisionBitMask = 0

    self.addChild(lobster)
    self.addChild(hedgehog)
    self.addChild(gorilla)

    self.moveGorilla(gorilla)
    self.moveLobster(lobster)
    self.moveHedgeHog(hedgehog)
}

func moveGorilla(_ passenger: SKSpriteNode) {

    let moveLeft = CGVector(dx: 0, dy: 200)
    let aYYY = CGVector(dx: 200, dy: 0)
    let moving = SKAction.move(by: moveLeft, duration: 1)
    let turnAYY = SKAction.move(by: aYYY, duration: 1)
    let chill = SKAction.wait(forDuration: 3)

    passenger.run(SKAction.sequence([moving, chill, moving, chill, turnAYY])) {
        print("Gorilla arrived")
    }
}

func moveLobster(_ passenger: SKSpriteNode) {

    let moveLeft = CGVector(dx: 0, dy: 200)
    let moving = SKAction.move(by: moveLeft, duration: 1.2)

    passenger.run(SKAction.sequence([moving, moving, moving, moving, moving, moving, moving, moving, moving])) {
        print("Lobster arrived")
    }
}

func moveHedgeHog(_ passenger: SKSpriteNode) {
    let moveLeft = CGVector(dx: 0, dy: 200)
    let moving = SKAction.move(by: moveLeft, duration: 1)
    let chill = SKAction.wait(forDuration: 2)

    passenger.run(SKAction.sequence([chill, moving, moving, moving])) {
        print("Hedgehog arrived")
    }
}

}

您可以这样设置动态:

     hedgehog.physicsBody?.isDynamic = true
     gorilla.physicsBody?.isDynamic = false
     lobster.physicsBody?.isDynamic = true

通过该策略,您可以将此示例扩展到其他情况。

如果isDynamic = true被当作0,而isDynamic = false,即static,被当作无穷大, 两者之间有一些东西可以由物理物体的质量控制。

    hedgehog.physicsBody?.isDynamic = false. (mass = Infinity)
    gorilla.physicsBody?.isDynamic = true. (mass = big)
    lobster.physicsBody?.isDynamic = true  (mass = 0)

    gorilla.physicsBody?.mass = 1000