gpt4 book ai didi

swift - 如何使用 SpriteKit 与已创建的 SKSpriteNode 创建碰撞?

转载 作者:行者123 更新时间:2023-11-30 12:33:21 24 4
gpt4 key购买 nike

我希望 pacman 在与正在移动的眨眼碰撞时从其原始位置重新启动。

考虑到我已经声明了它们,我怎样才能使它们发生碰撞?

你移动吃 bean 人,但眨眼独自移动。我希望它像吃 bean 人游戏一样工作。

  public class PacmanScene: SKScene {

let playerSpeed: CGFloat = 40.0
var pacman: SKSpriteNode?
var playerTextures: [SKTexture] = []
var lastTouch: CGPoint? = nil
var blinky: SKSpriteNode?
var clyde: SKSpriteNode?
var inky: SKSpriteNode?
var pinky: SKSpriteNode?
override public init(size: CGSize) {
let pacmanTexture = SKTexture(imageNamed: "pacman01.png")
pacman = SKSpriteNode(texture: pacmanTexture)
pacman?.name = "pacman"

pacman?.position = CGPoint(x:30, y:30)
pacman?.zPosition = 1.0
pacman?.physicsBody = SKPhysicsBody(texture: pacmanTexture, size: CGSize(width: (pacman?.size.width)!, height: (pacman?.size.height)!))
pacman?.physicsBody?.allowsRotation = true
pacman?.physicsBody?.affectedByGravity = false
pacman?.physicsBody?.mass = 2
let blinkyTexture = SKTexture(imageNamed: "blinky.png")
blinky = SKSpriteNode(texture: blinkyTexture)
blinky?.name = "blinky"

blinky?.position = CGPoint(x: 15, y: 60)
blinky?.zPosition = 1.0
blinky?.physicsBody = SKPhysicsBody(texture: pacmanTexture, size: CGSize(width: (blinky?.size.width)!, height: (blinky?.size.height)!))
blinky?.physicsBody?.allowsRotation = false
blinky?.physicsBody?.affectedByGravity = false
blinky?.physicsBody?.mass = 1000

super.init(size: size)
addChild(pacman!)
addChild(blinky!)

override public func didMove(to view: SKView) {

let bmoveUp = SKAction.moveBy(x: 0, y: 450, duration: 4.0)

let bmoveRight = SKAction.moveBy(x:20, y:0, duration: 1.0)

let bmoveDown = SKAction.moveBy(x:0, y: -450, duration: 4.0)

let bmoveLeft = SKAction.moveBy(x:-20, y:0, duration: 1.0)


let bsequence = SKAction.sequence([bmoveUp, bmoveRight, bmoveDown, bmoveLeft])

let bendlessAction = SKAction.repeatForever(bsequence)
blinky?.run(bendlessAction)
}

最佳答案

如果 iv 做对了,你希望你的“blinky”跟随你的“pacman”来做到这一点,那么你必须计算出 pacman 的位置,然后向你的眨眼添加一个 SKAction 以移动到该位置。

尝试这样的事情

//Speed blinky moves
let blinkySpeed = 100

override func update(_ currentTime: TimeInterval) {
// Called before each frame is rendered
updateBlinky()
}

func updateBlinky() {
//Set the point that blinky moves to
let point = CGPoint(x: pacman.position.x, y: pacman.position.y)
//Get the distance its got to travel
let distance = distanceBetweenPoints(first: pacman.position, second: blinky.position)
//Get the time is got to take from the speed and distance
let time = distance / blinkySpeed
//Create and run the action
let action = SKAction.move(to: point, duration: TimeInterval(time))
blinky.run(action)
}

//work out the distance between the sprites
func distanceBetweenPoints(first: CGPoint, second: CGPoint) -> Int {
return Int(hypot(second.x - first.x, second.y - first.y))
}

最终的结果是这样的

The end result would be something like this

编辑:

好吧,我认为从你的问题来看,你已经有“闪烁”的移动,你只是想检测碰撞。

首先,您需要将 SKPhysicsContactDelegate 添加到您的类中

public class PacmanScene: SKScene, SKPhysicsContactDelegate {

然后您需要为两个 Sprite 添加类别位掩码,然后在 didBegin(_ contact: SKPhysicsContact) 方法中处理碰撞。

我会做什么

//Create Physics category struct
struct PhysicsCategory {
static let pacman : UInt32 = 0x1 << 1
static var blinky : UInt32 = 0x1 << 2
}

然后在设置 pacman 和眨眼的地方设置类别位掩码

//Set the category bit mask for pacman
pacman?.physicsBody?.categoryBitMask = PhysicsCategory.pacman
//Set what categories you want to test contact
pacman?.physicsBody?.contactTestBitMask = PhysicsCategory.blinky
//Set what categories you want to collide with each other
pacman?.physicsBody?.collisionBitMask = PhysicsCategory.blinky

//Set the category bit mask for blinky
blinky?.physicsBody?.categoryBitMask = PhysicsCategory.blinky
//Set what categories you want to test contact
blinky?.physicsBody?.contactTestBitMask = PhysicsCategory.pacman
//Set what categories you want to collide with each other
blinky?.physicsBody?.collisionBitMask = PhysicsCategory.pacman

然后您需要实现 didBegin(_ contact: SKPhysicsContact) 方法来处理碰撞

func didBegin(_ contact: SKPhysicsContact) {
//Work out which contact was pacman
let other = contact.bodyA.categoryBitMask == PhysicsCategory.pacman ? contact.bodyB : contact.bodyA
//Test what it hit
switch other.categoryBitMask {

case PhysicsCategory.blinky:
print("pacman hit blinky")
//Move pacman
pacman?.position = CGPoint(x:30, y:30)

default:
break

}
}

希望这有帮助

关于swift - 如何使用 SpriteKit 与已创建的 SKSpriteNode 创建碰撞?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43173549/

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