gpt4 book ai didi

ios - Swift:重新加载关卡后不会调用 didBeginContact()

转载 作者:行者123 更新时间:2023-11-30 14:05:27 29 4
gpt4 key购买 nike

我是 swift 语言的新手,遇到了一些问题。我猜这是一个简单的错误,但无论如何我似乎找不到问题。我正在制作一个游戏。当你输了之后,我会在屏幕上创建一个重播按钮。单击后,我开始重置游戏。

我基本上重置了所有值,删除所有子项,然后重新初始化。它重新初始化得很好 - 所有背景和 Sprite 都正确移动,但如果我触摸尖峰(游戏的失败机制),游戏不会结束;不会调用 didBeginContact 函数来再次执行代码的丢失部分。我猜它与碰撞位掩码和类别位掩码有关。

玩家和尖峰之间的接触,以及玩家和平台之间的接触(以记录分数)都不起作用。

注意:我的代码目前很困惑。我希望在开始大修之前让它工作起来,使其变得漂亮和优化。

func didBeginContact(contact: SKPhysicsContact) {
//...
if contact.bodyA.categoryBitMask == spikeGroup && contact.bodyB.categoryBitMask == playerGroup {
gameOver = true
movingObjectsNode.speed = 0
//...

rplay.frame = CGRectMake(100, 100, 100, 50)
rplay.backgroundColor = UIColor.greenColor()
rplay.setTitle("Replay", forState: UIControlState.Normal)
rplay.addTarget(self, action: "rplayAction", forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(rplay)
} else if contact.bodyA.categoryBitMask == playerGroup && contact.bodyB.categoryBitMask == platformGroup {
var contactedPlatform = contact.bodyB.node
contactHistory.append(contactedPlatform!)
if contactHistory.count >= 3 {
contactHistory.removeAtIndex(0)
}
if contactHistory.count > 1 {
if contactHistory[0].name != contactHistory[1].name {
score = score + 1
scoreLabel.text = String(score)
}
} else {
score = score + 1
scoreLabel.text = String(score)
}
}
func rplayAction() {
rplay.removeFromSuperview()
self.removeAllChildren()

bgOnArray.removeAll(keepCapacity: false)
platformHistArray.removeAll(keepCapacity: false)
platformOnScreenArrayB.removeAll(keepCapacity: false)
platformOnScreenArrayW.removeAll(keepCapacity: false)
movingObjectsNode.removeAllChildren()
gameOverTabHolderNode.removeAllChildren()
pW.removeAll(keepCapacity: false)
pB.removeAll(keepCapacity: false)
pWMask.removeAll(keepCapacity: false)
pBMask.removeAll(keepCapacity: false)
contactHistory.removeAll(keepCapacity: false)

state = true
gstate = false
allowContact = true
gameOver = false
score = 0
count = 0
scoreLabel.text = "0"
scoreLabelArray.removeAll(keepCapacity: false)
movingObjectsNode.speed = 1

self.physicsWorld.contactDelegate = self
self.physicsWorld.gravity = CGVectorMake(0, -20)
self.addChild(movingObjectsNode)

var bgWTexture = SKTexture(imageNamed: "img/bgW")
var bgBTexture = SKTexture(imageNamed: "img/bgB")
var playerBTexture = SKTexture(imageNamed: "img/playerB")
var playerWTexture = SKTexture(imageNamed: "img/playerW")

var movebg = SKAction.moveByX(-bgWTexture.size().width, y: 0, duration: 10.0) //4.48 = 20 pixels a sec?
var replacebg = SKAction.moveByX(bgWTexture.size().width, y: 0, duration: 0)
var movebgForever = SKAction.repeatActionForever(SKAction.sequence([movebg, replacebg]))

makeBackground()

//note: for some reason spikes take forever to load (because of the tempFloatPoint was condensed into one line)
for var i:CGFloat=0; i<36; i++ {
var floorSpikes = SKShapeNode()
var tempInt = Int(i)
var tempString = String(tempInt)
var tempFloatPoint = CGFloat()
var path = CGPathCreateMutable()

CGPathMoveToPoint(path, nil, 0, 0)
CGPathAddLineToPoint(path, nil, bg.size.width/14 , 0)
CGPathAddLineToPoint(path, nil, bg.size.width/28, bg.size.height/9)
CGPathCloseSubpath(path)
floorSpikes.path = path
floorSpikes.lineWidth = 0;
floorSpikes.name = "2 " + tempString
tempFloatPoint = CGRectGetMidX(self.frame)
tempFloatPoint = tempFloatPoint - (bg.size.width/7)
tempFloatPoint = tempFloatPoint + (bg.size.width/14 * i)

if i <= 7 {
floorSpikes.position = CGPoint(x: tempFloatPoint, y: 0)
} else if i > 7 && i <= 20 {
floorSpikes.position = CGPoint(x: tempFloatPoint - 5, y: 0)
} else if i > 20 && i <= 36 {
floorSpikes.position = CGPoint(x: tempFloatPoint - 10, y: 0)
}

floorSpikes.runAction(movebgForever)
floorSpikes.zPosition = 100
floorSpikes.physicsBody = SKPhysicsBody(polygonFromPath: path)
floorSpikes.physicsBody!.dynamic = false
floorSpikes.physicsBody!.categoryBitMask = spikeGroup
movingObjectsNode.addChild(floorSpikes)
}

for var i:CGFloat=0; i<36; i++ {
var ceilingSpikes = SKShapeNode()
var tempInt = Int(i)
var tempString = String(tempInt)
var tempFloatPoint = CGFloat()
var path = CGPathCreateMutable()

CGPathMoveToPoint(path, nil, 0, 0)
CGPathAddLineToPoint(path, nil, bg.size.width/14 , 0)
CGPathAddLineToPoint(path, nil, bg.size.width/28, -bg.size.height/9)
CGPathCloseSubpath(path)
ceilingSpikes.path = path
ceilingSpikes.lineWidth = 0;
ceilingSpikes.name = "3 " + tempString
tempFloatPoint = CGRectGetMidX(self.frame)
tempFloatPoint = tempFloatPoint - (bg.size.width/7)
tempFloatPoint = tempFloatPoint + (bg.size.width/14 * i)

if i <= 7 {
ceilingSpikes.position = CGPoint(x: tempFloatPoint, y: CGRectGetMaxY(self.frame))
} else if i > 7 && i <= 20 {
ceilingSpikes.position = CGPoint(x: tempFloatPoint - 5, y: CGRectGetMaxY(self.frame))
} else if i > 20 && i <= 36 {
ceilingSpikes.position = CGPoint(x: tempFloatPoint - 10, y: CGRectGetMaxY(self.frame))
}

ceilingSpikes.runAction(movebgForever)
ceilingSpikes.zPosition = 100
ceilingSpikes.physicsBody = SKPhysicsBody(polygonFromPath: path)
ceilingSpikes.physicsBody!.dynamic = false
ceilingSpikes.physicsBody!.categoryBitMask = spikeGroup
movingObjectsNode.addChild(ceilingSpikes)
}

//player stuff
player = SKSpriteNode(texture: playerBTexture)

player.xScale = 0.1
player.yScale = 0.1
player.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame) + self.frame.size.height/8)

player.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(player.size.width, player.size.height))
player.physicsBody!.restitution = 0.0
player.physicsBody!.allowsRotation = false
player.physicsBody!.angularDamping = 0.0
player.physicsBody!.angularVelocity = 0.0
player.physicsBody!.dynamic = true
player.physicsBody!.categoryBitMask = playerGroup
player.physicsBody!.collisionBitMask = spikeGroup
player.physicsBody!.contactTestBitMask = spikeGroup
player.physicsBody!.collisionBitMask = platformGroup
player.physicsBody!.contactTestBitMask = platformGroup

self.addChild(player)

var screen = CGRectMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame), UIScreen.mainScreen().bounds.width, self.frame.height)

scoreLabel.fontName = "GKFont"
scoreLabel.fontSize = 60
scoreLabel.fontColor = UIColor.blackColor()
scoreLabel.text = "0"
scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame) + screen.width/2 - screen.width/10, self.frame.height - self.frame.height/4)
self.addChild(scoreLabel)
scoreLabelArray.append(scoreLabel)

makeStarterPlatforms()
}

我认为问题出在这两个函数之一上。预先感谢您愿意提供的任何帮助!

最佳答案

终于我明白了。我不知道为什么,但在重玩游戏并重新加载所有内容后,碰撞发生的顺序颠倒了。对于每个碰撞声明,我都必须更改......

contact.bodyA.categoryBitMask == playerGroup && contact.bodyB.categoryBitMask == platformGroup

到....

contact.bodyA.categoryBitMask == playerGroup && contact.bodyB.categoryBitMask == platformGroup || contact.bodyA.categoryBitMask == platformGroup && contact.bodyB.categoryBitMask == playerGroup

如果有人可以澄清为什么需要实现此更改,请随时在下面发表评论。

关于ios - Swift:重新加载关卡后不会调用 didBeginContact(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32488418/

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