gpt4 book ai didi

swift - didBeginContact 在 Swift 2 + SpriteKit 中不起作用

转载 作者:搜寻专家 更新时间:2023-11-01 07:28:18 25 4
gpt4 key购买 nike

我正在开发一款游戏,我正在使用 spritekit 和 Swift。

这是我的代码:

import SpriteKit

struct collision {
static let arrow:UInt32 = 0x1 << 1
static let runner:UInt32 = 0x1 << 2
static let target:UInt32 = 0x1 << 3
static let targetCenter:UInt32 = 0x1 << 4
}

class GameScene: SKScene, SKPhysicsContactDelegate {

var person = SKSpriteNode()
var box = SKSpriteNode()

var screenSize:CGSize!
var gameScreenSize:CGSize!

var gameStarted:Bool = false

var moveAndRemove = SKAction()

var boxVelocity:NSTimeInterval = 5.5

override func didMoveToView(view: SKView) {

self.physicsWorld.gravity = CGVectorMake(0, -1.0)
self.physicsWorld.contactDelegate = self

screenSize = self.frame.size
gameScreenSize = view.frame.size

createPerson()
}

func createPerson() -> Void {
person.texture = SKTexture(imageNamed:"person")
person.setScale(1.0)
person.size = CGSize(width: 80, height: 80)
person.position = CGPoint(x: screenSize.width / 2, y: 150)

person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
person.physicsBody?.affectedByGravity = false
person.physicsBody?.dynamic = false

self.addChild(person)
}

func createTarget() -> Void {
box = SKSpriteNode()

box.size = CGSize(width: 70, height: 100)
box.setScale(1.0)

box.position = CGPoint(x: (screenSize.width / 3) * 2, y: screenSize.height + box.size.height)
box.texture = SKTexture(imageNamed: "box")

box.physicsBody? = SKPhysicsBody(rectangleOfSize: box.size)
box.physicsBody?.categoryBitMask = collision.target
box.physicsBody?.collisionBitMask = collision.arrow
box.physicsBody?.contactTestBitMask = collision.targetCenter
box.physicsBody?.affectedByGravity = false
box.physicsBody?.dynamic = true
box.physicsBody?.usesPreciseCollisionDetection = true

self.addChild(box)

let distance = CGFloat(self.frame.height - box.frame.height)
let moveTargets = SKAction.moveToY(-distance, duration: boxVelocity)
let removeTargets = SKAction.removeFromParent()

moveAndRemove = SKAction.sequence([moveTargets,removeTargets])
box.runAction(moveAndRemove)
}

func createBall() ->Void {
let ball = SKSpriteNode()
ball.size = CGSize(width: 20, height: 22)
ball.zPosition = 5

let moveToXY = CGPoint(x: self.size.width, y: self.size.height)

ball.texture = SKTexture(imageNamed: "ball")
ball.position = CGPointMake(person.position.x + ball.size.width, person.position.y + ball.size.height)

ball.physicsBody? = SKPhysicsBody(rectangleOfSize: ball.size)
ball.physicsBody?.categoryBitMask = collision.arrow
ball.physicsBody?.collisionBitMask = collision.target
ball.physicsBody?.affectedByGravity = false
ball.physicsBody?.dynamic = true
ball.physicsBody?.usesPreciseCollisionDetection = true

let action = SKAction.moveTo(moveToXY, duration: 1.5)
let delay = SKAction.waitForDuration(1.5)
ball.runAction(SKAction.sequence([action,delay]))

self.addChild(ball)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
if gameStarted == false {
gameStarted = true

let spawn = SKAction.runBlock { () in
self.createTarget()
}
let delay = SKAction.waitForDuration(1.5)
let spawnDelay = SKAction.sequence([spawn, delay])
let spanDelayForever = SKAction.repeatActionForever(spawnDelay)
self.runAction(spanDelayForever)
} else {
createBall()
boxVelocity -= 0.1
}
}

func didBeginContact(contact: SKPhysicsContact) {
print("Detect")
}

func didEndContact(contact: SKPhysicsContact) {
print("end detect")
}

override func update(currentTime: CFTimeInterval) {

}
}

但是当我运行游戏时,物体之间的碰撞没有。我试图解决一段时间,但一无所获。有人可以帮助我吗?

Project files .

最佳答案

尝试对这些进行修改:

 box.physicsBody = SKPhysicsBody(rectangleOfSize: box.size)
box.physicsBody?.categoryBitMask = collision.target
box.physicsBody?.collisionBitMask = collision.arrow
box.physicsBody?.contactTestBitMask = collision.targetCenter

ball.physicsBody = SKPhysicsBody(rectangleOfSize: ball.size)
ball.physicsBody?.categoryBitMask = collision.arrow
ball.physicsBody?.collisionBitMask = collision.target
ball.physicsBody?.contactTestBitMask = collision.target

注意没有“?”当您初始化 physicsBody 和新的 contactTestBitMask

关于swift - didBeginContact 在 Swift 2 + SpriteKit 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34461559/

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