gpt4 book ai didi

ios - 分数不会随着接触而增加

转载 作者:可可西里 更新时间:2023-11-01 01:39:04 26 4
gpt4 key购买 nike

当一个物体经过另一个物体时,我很难提高分数。

我已经尝试更改 contactTestBitMask 并得到混合结果有人知道我的代码有什么问题吗?

我已经删除了大部分不需要的代码,如果有人想玩的话。

import SpriteKit

var contact = SKSpriteNode()
var birdTexture1 = SKTexture()
var scoreTexture1 = SKTexture()
var birds = SKNode()
var moveAndRemoveBirds = SKAction()
var moving = SKNode()

let scoreCategory: UInt32 = 1 << 3
let contact2Category: UInt32 = 1 << 2
let crowCategory: UInt32 = 1 << 2

var scoreLabelNode = SKLabelNode()
var score = NSInteger()
var highScoreLabelNode = SKLabelNode()
var highScore = NSInteger()


class GameScene: SKScene {
override func didMoveToView(view: SKView) {


self.addChild(moving)
moving.addChild(birds)

birdTexture1 = SKTexture(imageNamed: "crow")
birdTexture1.filteringMode = SKTextureFilteringMode.Nearest

var distanceToMoveBird = CGFloat(self.frame.size.width + 2 * birdTexture1.size().width);
var moveBirds = SKAction.moveByX(-distanceToMoveBird, y:0, duration:NSTimeInterval(0.0040 * distanceToMoveBird));
var removeBirds = SKAction.removeFromParent();
moveAndRemoveBirds = SKAction.sequence([moveBirds, removeBirds]);

var spawnBirds = SKAction.runBlock({() in self.spawnBird()})
var delayBirds = SKAction.waitForDuration(NSTimeInterval(4.0))
var spawnThenDelayBirds = SKAction.sequence([spawnBirds, delayBirds])
var spawnThenDelayForeverBirds = SKAction.repeatActionForever(spawnThenDelayBirds)
self.runAction(spawnThenDelayForeverBirds)

var scoreTexture1 = SKTexture(imageNamed: "contactNode")
scoreTexture1.filteringMode = SKTextureFilteringMode.Nearest

contact = SKSpriteNode(texture: scoreTexture1)
contact.position = CGPoint(x: self.frame.size.width / 3.3, y: self.frame.size.height / 2.2 )
contact.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(1, 800))
contact.physicsBody?.categoryBitMask = scoreCategory
contact.physicsBody?.contactTestBitMask = crowCategory
contact.physicsBody?.collisionBitMask = 0
contact.physicsBody!.dynamic = false
contact.physicsBody!.allowsRotation = false
self.addChild(contact)

score = 0
scoreLabelNode.fontName = "Helvetica-Bold"
scoreLabelNode.position = CGPoint(x: self.frame.size.width / 2.6, y: self.frame.size.height / 1.2 )
scoreLabelNode.fontSize = 40
scoreLabelNode.alpha = 0.7
scoreLabelNode.fontColor = SKColor.redColor()
scoreLabelNode.zPosition = -30
scoreLabelNode.text = "Score \(score)"
self.addChild(scoreLabelNode)

}

func spawnBird() {
var bird = SKSpriteNode()
bird.position = CGPointMake( self.frame.size.width + birdTexture1.size().width * 2, 0 );
var height = UInt32( self.frame.size.height / 1 )
var height_max = UInt32( 500 )
var height_min = UInt32( 300 )
var y = arc4random_uniform(height_max - height_min + 1) + height_min;
var bird1 = SKSpriteNode(texture: birdTexture1)
bird1.position = CGPointMake(0.0, CGFloat(y))
bird1.physicsBody = SKPhysicsBody(rectangleOfSize: bird1.size)
bird1.physicsBody?.dynamic = false
bird1.physicsBody?.categoryBitMask = crowCategory
bird1.physicsBody?.collisionBitMask = scoreCategory
bird1.physicsBody?.contactTestBitMask = scoreCategory
bird.addChild(bird1)

bird.runAction(moveAndRemoveBirds)

birds.addChild(bird)

}

func didBeginContact(contact: SKPhysicsContact) {

if( moving.speed > 0 ) {

if contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory {

score++
scoreLabelNode.text = "Score \(score)"


}else {

//code removed
}

}

}


override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {

}

override func update(currentTime: CFTimeInterval) {

}
}

最佳答案

我不是 100% 确定,但你应该改变这两件事:

  • “contact.physicsBody?.collisionBitMask = 0”到“contact.physicsBody?.collisionBitMask = crowCategory”
  • 并使您的物理体动态化。

“动态属性控制基于体积的物体是否会受到重力、摩擦、与其他物体的碰撞以及您直接施加到物体上的力或冲量的影响。”

编辑:另外,你想改变:

if contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory {

if (contact.bodyA.categoryBitMask == crowCategory && contact.bodyB.categoryBitMask == scoreCategory) || 
(contact.bodyA.categoryBitMask == scoreCategory && contact.bodyB.categoryBitMask == crowCategory)

那是因为您无法确定 bodyA 始终是 crowCategory。这样你就可以确保如果 crowCategory 的一个对象和 scoreCategory 的一个对象发生碰撞,分数就会增加。

祝你好运

关于ios - 分数不会随着接触而增加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32353540/

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