gpt4 book ai didi

ios - 如何将碰撞检测设置到我想要的位置?

转载 作者:行者123 更新时间:2023-11-30 11:51:20 26 4
gpt4 key购买 nike

我正在制作一个游戏,其中一个球(人)必须穿过旋转圆圈(圆圈)中的开口并击中目标。我对旋转圆设置了碰撞检测,然后尝试使球(家伙)穿过它而不穿过开口。我知道这与碰撞检测有关。另外,我将circleOfRadius 设置为半径,但我似乎无法为球通过打开一个开口。如果您能提供帮助,我很乐意听取您的意见。这是我的代码,它不是全部,只是重要的部分。

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var circle = SKSpriteNode()
var guy = SKSpriteNode()
let guyCategory :UInt32 = 0x1 << 0
let circleCategory :UInt32 = 0x1 << 1

override func didMove(to view: SKView) {


self.physicsWorld.contactDelegate = self

scene?.anchorPoint = CGPoint(x: 0, y: 0)
backgroundColor = UIColor.lightGray

circle = SKSpriteNode(imageNamed: "Circle1")
circle.position = CGPoint(x: self.frame.size.width / 2, y:
self.frame.size.height / 2)
circle.physicsBody = SKPhysicsBody(circleOfRadius: 298 )
circle.physicsBody?.isDynamic = false
circle.physicsBody?.allowsRotation = true
circle.physicsBody?.affectedByGravity = false
circle.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat.pi * 2.0, duration: 7.0)), withKey: "rotatecircle")
self.addChild(circle)

guy = SKSpriteNode(imageNamed: "Guy")
guy.position = CGPoint(x: self.frame.size.width/2 , y:40)
guy.name = "guy"
guy.physicsBody = SKPhysicsBody(circleOfRadius: guy.size.width/2)
guy.physicsBody?.isDynamic = false
guy.physicsBody?.affectedByGravity = false
self.addChild(guy)

circle.physicsBody?.categoryBitMask = circleCategory
circle.physicsBody?.collisionBitMask = targetCategory
circle.physicsBody?.contactTestBitMask = targetCategory

guy.physicsBody?.categoryBitMask = guyCategory
guy.physicsBody?.collisionBitMask = targetCategory
guy.physicsBody?.contactTestBitMask = targetCategory
}

func didBegin(_ contact: SKPhysicsContact) {
let collision1:UInt32 = contact.bodyA.categoryBitMask

if collision1 == targetCategory {
backgroundColor = UIColor.green
}
else {
backgroundColor = UIColor.red
}
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

self.physicsWorld.gravity = CGVector(dx: 0 ,dy: 10);
guy.physicsBody = SKPhysicsBody(circleOfRadius: guy.size.width / 2)
}
override func update(_ currentTime: TimeInterval) {

}
}

最佳答案

这里的问题是您使用 SKPhysicsBody(circleOfRadius: 298 ) 为整个圆分配一个物理体。这还包括圆圈中的洞,因此任何东西都无法通过。您应该使用 SKPhysicsbody 类的纹理初始化程序来复制圆的形状并保持孔不变。以下内容将适合您:

circle = SKSpriteNode(texture: SKTexture(imageNamed: "Circle1"))
circle.physicsbody = SKPhysicsbody(texture: circle.texture!, size: CGSize(width: circle.frame.width, height: circle.frame.height))

关于ios - 如何将碰撞检测设置到我想要的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48292605/

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