gpt4 book ai didi

swift - SKPhysicsContactDelegate,碰撞无法正常工作

转载 作者:行者123 更新时间:2023-11-28 07:20:10 25 4
gpt4 key购买 nike

我想知道如何让联系人代理工作。我确实有接触,但我希望个别砖 block 在被球击中时消失。

我已经用这个碰壁太久了。一个接一个的教程似乎对我毫无帮助。我可能需要采取一些小步骤,但我不确定如何开始。

我已经尝试过 Youtube 上的许多教程以及我可以在网上找到的所有教程。

enum CollisionType: UInt32 {

case ball = 1
case paddle = 2
case walls = 4
case brick = 8

}

class Brick: SKSpriteNode { }


class GameScene: SKScene, SKPhysicsContactDelegate {

let bricks = ["brickPurple","brickGreen", "brickRed"]
let brick = SKSpriteNode(imageNamed: "brickPurple")

let ball = SKSpriteNode(imageNamed: "ball")
let paddle = SKSpriteNode(imageNamed: "paddle")



override func didMove(to view: SKView) {

physicsWorld.contactDelegate = self


physicsBody = SKPhysicsBody(edgeLoopFrom: frame.inset(by: UIEdgeInsets(top: 0, left: 0 , bottom: 0 , right: 0 )))


ball.name = "ball"
ball.size = CGSize(width: 20, height: 20)
ball.position.x = frame.maxX/2
ball.position.y = 70
addChild(ball)


ball.physicsBody = SKPhysicsBody (circleOfRadius: ball.size.width/2)

ball.physicsBody?.restitution = 1
ball.physicsBody?.linearDamping = 0
ball.physicsBody?.friction = 0
ball.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 20))
ball.physicsBody?.contactTestBitMask = ball.physicsBody!.collisionBitMask


paddle.name = "paddle"
paddle.size = CGSize(width: 100, height: 20)
paddle.position.x = frame.maxX/2
paddle.position.y = 50
paddle.physicsBody = SKPhysicsBody(rectangleOf: paddle.size)
paddle.physicsBody?.isDynamic = false



let brickWidth = brick.frame.width
let brickHeight = brick.frame.height

for i in stride(from: view.bounds.minX + brickWidth, to: view.bounds.maxX - brickWidth, by: brickWidth ) {

for j in stride(from: view.bounds.minX + brickHeight + 375, to: view.bounds.maxX + 200, by: brick.frame.height + 0) {

let brickType = bricks.randomElement()!

let brick = Brick(imageNamed: brickType)

brick.position = CGPoint(x: i, y: j)

brick.physicsBody = SKPhysicsBody (rectangleOf: brick.size)


brick.physicsBody?.isDynamic = false

brick.name = "brick"

addChild(brick)


}


}

}}

我之前有一些代码用于 func didBegin(_ contact: SKPhysicsContact),但我刚刚决定重新开始。

最佳答案

这不是您问题的具体答案,但如果您按照以下答案中的步骤(尤其是第一个),我认为您的代码将正常工作:

我的碰撞和接触分步指南: https://stackoverflow.com/a/51041474/1430420

以及碰撞和接触测试位掩码指南: https://stackoverflow.com/a/40596890/1430420

操纵位掩码以关闭和打开单个碰撞和接触。 https://stackoverflow.com/a/46495864/1430420

关于swift - SKPhysicsContactDelegate,碰撞无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58468569/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com