从其他SKShapeNodes正确反弹一个SKShapeNode

时间:2018-01-31 12:34:20

标签: ios swift sprite-kit physics

我正在创建一个小游戏并学习Spritekit物理。我有一个SKScene,我在screne的边缘放置了4个边框(左,右,上,下。制作正方形)。 Beetwin他们我有一个圆圈SKShapeNode,我用速度移动它(dx:200,dy:200)。我希望我的shapenode永远地从现实世界的边界反弹。因此,如果它以速度(dx:200,dy:200)移动到右边界,则由X轴反弹,从上边界移动Y轴。 我想如何做到这一点,我以为我得到了它。我创建了一些代码,但在一系列反弹后它无法正常工作。

public var redLaserTexVert = SKTexture()

public var redCircle = SKShapeNode()
public var redCircleObject = SKNode()

public var redLaserTex = SKTexture()

public var redLaser = SKSpriteNode()
public var redLaserObject = SKNode()

public var redLaser2 = SKSpriteNode()
public var redLaserObject2 = SKNode()

public var redLaser3 = SKSpriteNode()
public var redLaserObject3 = SKNode()

public var redLaser4 = SKSpriteNode()
public var redLaserObject4 = SKNode()

public var greenCircle = SKShapeNode()
public var greenCircleObject = SKNode()

public var dx = Int()
public var dy = Int()

public var plusX = 200
public var minusX = -200

public var plusY = 200
public var minusY = -200

struct collision {
    static let circleBody: UInt32 = 0x1 << 0
    static let redLaser1: UInt32 = 0x1 << 1
    static let redLaser2: UInt32 = 0x1 << 2
    static let redLaser3: UInt32 = 0x1 << 3
    static let redLaser4: UInt32 = 0x1 << 4
    static let greenCircle: UInt32 = 0x1 << 5
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    override func didMove(to view: SKView) {

        self.physicsWorld.contactDelegate = self
        self.backgroundColor = UIColor.white

        greenCircle = SKShapeNode.init(circleOfRadius: self.frame.width/9)
        greenCircle.position = CGPoint(x: 100, y: 100)
        greenCircle.fillColor = UIColor.green
        greenCircle.physicsBody = SKPhysicsBody(circleOfRadius: self.frame.width/9)
        greenCircle.physicsBody?.affectedByGravity = false
        greenCircle.physicsBody?.isDynamic = true
        greenCircleObject.physicsBody?.categoryBitMask = collision.greenCircle
        greenCircle.physicsBody?.contactTestBitMask = collision.redLaser1 | collision.redLaser2 | collision.redLaser3 | collision.redLaser4
        greenCircle.physicsBody?.collisionBitMask = collision.redLaser1 | collision.redLaser2 | collision.redLaser3 | collision.redLaser4
        greenCircleObject.addChild(greenCircle)
        self.addChild(greenCircleObject)
        greenCircle.name = "greenCircle"

        dx = plusX
        dy = plusY

        greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)

        redLaserTex = SKTexture(imageNamed: "redLaserRay")
        redLaserTexVert = SKTexture(imageNamed: "redLaserRayVert")

        redLaser = SKSpriteNode(texture: redLaserTex)
        redLaser.size = CGSize(width: self.frame.width, height: self.frame.height/12)
        redLaser.position = CGPoint(x: self.frame.midX, y: self.frame.minY+10)
        redLaser.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.width, height: 10))
        redLaser.physicsBody?.affectedByGravity = false
        redLaser.physicsBody?.isDynamic = false
        redLaser.physicsBody?.categoryBitMask = collision.redLaser1
        redLaser.physicsBody?.contactTestBitMask = collision.greenCircle
        redLaser.physicsBody?.collisionBitMask = collision.greenCircle
        redLaserObject.addChild(redLaser)
        self.addChild(redLaserObject)
        redLaser.name = "leftLr"

        redLaser2 = SKSpriteNode(texture: redLaserTex)
        redLaser2.size = CGSize(width: self.frame.width, height: self.frame.height/12)
        redLaser2.position = CGPoint(x: self.frame.midX, y: self.frame.maxY-10)
        redLaser2.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.frame.width, height: 10))
        redLaser2.physicsBody?.affectedByGravity = false
        redLaser2.physicsBody?.isDynamic = false
        redLaser2.physicsBody?.categoryBitMask = collision.redLaser2
        redLaser2.physicsBody?.contactTestBitMask = collision.greenCircle
        redLaser2.physicsBody?.collisionBitMask = collision.greenCircle
        redLaserObject2.addChild(redLaser2)
        redLaser2.name = "upLr"
        self.addChild(redLaserObject2)

        redLaser3 = SKSpriteNode(texture: redLaserTexVert)
        redLaser3.size = CGSize(width: self.frame.width/6, height: self.frame.height)
        redLaser3.position = CGPoint(x: self.frame.minX+10, y: self.frame.midY)
        redLaser3.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 10, height: self.frame.height))
        redLaser3.physicsBody?.affectedByGravity = false
        redLaser3.physicsBody?.isDynamic = false
        redLaser3.physicsBody?.categoryBitMask = collision.redLaser3
        redLaser3.physicsBody?.contactTestBitMask = collision.greenCircle
        redLaser3.physicsBody?.collisionBitMask = collision.greenCircle
        redLaserObject3.addChild(redLaser3)
        redLaser3.name = "bottomLr"
        self.addChild(redLaserObject3)

        redLaser4 = SKSpriteNode(texture: redLaserTexVert)
        redLaser4.size = CGSize(width: self.frame.width/6, height: self.frame.height)
        redLaser4.position = CGPoint(x: self.frame.maxX-10, y: self.frame.midY)
        redLaser4.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 10, height: self.frame.height))
        redLaser4.physicsBody?.affectedByGravity = false
        redLaser4.physicsBody?.isDynamic = false
        redLaser4.physicsBody?.categoryBitMask = collision.redLaser4
        redLaser4.physicsBody?.contactTestBitMask = collision.greenCircle
        redLaser4.physicsBody?.collisionBitMask = collision.greenCircle
        redLaserObject4.addChild(redLaser4)
        redLaser4.name = "rightLr"
        self.addChild(redLaserObject4)  
    }

    func didBegin(_ contact: SKPhysicsContact) {
        var firstBody = SKPhysicsBody()
        var secondBody = SKPhysicsBody()

        if contact.bodyA.node?.name == "greenCircle" {

            firstBody = contact.bodyA
            secondBody = contact.bodyB }
        else {

            firstBody = contact.bodyB
            secondBody = contact.bodyA }

        if firstBody.node?.name == "greenCircle" && secondBody.node?.name == "rightLr" {

            if dx == plusX && dy == plusY {
                dx = minusX
                dy = plusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }

            if dx == plusX && dy == minusY {
                dx = minusX
                dy = minusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }

        }

        if firstBody.node?.name == "greenCircle" && secondBody.node?.name == "upLr" {
            if dx == minusX && dy == plusY {
                dx = minusX
                dy = minusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }

            if dx == plusX && dy == plusY {
                dx = plusX
                dy = minusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }
        }
        if firstBody.node?.name == "greenCircle" && secondBody.node?.name == "bottomLr" {
            if dx == plusX && dy == minusY {
                dx = plusX
                dy = plusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }

            if dx == minusX && dy == minusY {
                dx = minusX
                dy = plusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }
        }

        if firstBody.node?.name == "greenCircle" && secondBody.node?.name == "leftLr" {
            if dx == minusX && dy == minusY {
                dx = plusX
                dy = minusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }

            if dx == minusX && dy == plusY {
                dx = plusX
                dy = plusY
                greenCircle.physicsBody?.velocity = CGVector(dx: dx, dy: dy)
            }
        }
    }

    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        for t in touches { self.touchUp(atPoint: t.location(in: self)) }
    }

    override func update(_ currentTime: TimeInterval) {
    //   greenCircle.physicsBody?.velocity = CGVector(dx: 200, dy: 200)

        }
    }

0 个答案:

没有答案