分数不会随着接触而增加
Score not increasing with contact
当一个物体经过另一个物体时,我很难提高分数。
我已经尝试更改 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"
- 并使您的物理体动态化。
"The dynamic property controls whether a volume-based body is affected by gravity, friction, collisions with other objects, and forces or impulses you directly apply to the object. "
编辑:
另外,您想更改:
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 的一个对象发生碰撞,分数就会增加。
祝你好运
当一个物体经过另一个物体时,我很难提高分数。
我已经尝试更改 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"
- 并使您的物理体动态化。
"The dynamic property controls whether a volume-based body is affected by gravity, friction, collisions with other objects, and forces or impulses you directly apply to the object. "
编辑: 另外,您想更改:
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 的一个对象发生碰撞,分数就会增加。
祝你好运