2016-12-14 1 views
2

Je fais un jeu d'oiseau flappy. J'ai l'impression d'avoir un problème quand mon oiseau passe chaque mur/recueille une pièce de monnaie. Il y a 2 problèmes. 1 le jeu est en retard d'une milliseconde après la collecte. 2 Mon oiseau semble avoir 2 ou même 3 collisions à chaque fois créant un score de 2 ou 3, je ne peux pas obtenir ma tête autour de cela! Mon oiseau est une animation de texture 5, avec un corps de physique qui entoure sa forme complexe avec la texture: bird.texture! type de code.Collision détectée plusieurs fois.

J'ai essayé de comprendre cela pendant 4 jours maintenant il a mis les pauses sur mon application grand moment! S'il vous plaît Helpp !!!

func createScene(){ 

let bird1 = SKTexture(imageNamed: "1") 
let bird2 = SKTexture(imageNamed: "2") 
let bird3 = SKTexture(imageNamed: "3") 
let bird4 = SKTexture(imageNamed: "4") 
let bird5 = SKTexture(imageNamed: "5") 



let birdAnimation = SKAction.repeatForever(SKAction.animate(with: [bird1, bird2, bird3, bird4, bird5], timePerFrame: 0.1)) 
let flyForever = SKAction.repeatForever(birdAnimation) 

bird = SKSpriteNode(texture: bird1) 
bird = CGSize(width: 65, height: 65) 
bird = CGPoint(x: self.frame.width/1.5 - bird, y: self.frame.height/2) 
bird(flyForever, withKey: "birdFly") 

bird = SKPhysicsBody(texture: bird.texture!, size: CGSize(width: bird.size.width, height: bird.size.height)) 


bird.physicsBody?.categoryBitMask = physicsCategory.bird 
bird.physicsBody?.collisionBitMask = physicsCategory.ground | physicsCategory.wall 
bird.physicsBody?.contactTestBitMask = physicsCategory.ground | physicsCategory.wall | physicsCategory.score 
bird?.affectedByGravity = false 
bird.physicsBody?.isDynamic = true 


self.addChild(bird) 


func didBegin(_ contact: SKPhysicsContact) { 
let firstBody = contact.bodyA 
let secondBody = contact.bodyB 

if firstBody.categoryBitMask == physicsCategory.score && secondBody.categoryBitMask == physicsCategory.bird{ 



    score += 1 
    scoreLabel.text = "\(score)" 
    firstBody.node?.removeFromParent() 

    run(SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false)) 

    if score > UserDefaults().integer(forKey: "HIGHSCORE") { 
     saveHighScore() 


    } 

} 
else if firstBody.categoryBitMask == physicsCategory.bird && secondBody.categoryBitMask == physicsCategory.score{ 

    score += 1 
    scoreLabel.text = "\(score)" 
    secondBody.node?.removeFromParent() 

    run(SKAction.playSoundFileNamed("tap.caf", waitForCompletion: false)) 

    if score > UserDefaults().integer(forKey: "HIGHSCORE") { 
     saveHighScore() 


    } 

} 


func createWalls(){ 



let scoreNode = SKSpriteNode(imageNamed: "bird") 
scoreNode.size = CGSize(width: 40, height: 40) 
scoreNode.position = CGPoint(x: self.frame.width + 25, y: self.frame.height/2) 
scoreNode.physicsBody = SKPhysicsBody(rectangleOf: scoreNode.size) 
scoreNode.physicsBody?.affectedByGravity = false 
scoreNode.physicsBody?.isDynamic = false 
scoreNode.physicsBody?.categoryBitMask = physicsCategory.score 
scoreNode.physicsBody?.collisionBitMask = 0 
scoreNode.physicsBody?.contactTestBitMask = physicsCategory.bird 

Répondre

0

Je trouve en quelque sorte la réponse à cette question, mais il est pas parfait ... la physicsBody de mon oiseau a été changé de

bird = SKPhysicsBody(texture: bird.texture!, size: CGSize(width: bird.size.width, height: bird.size.height)) 

à

let path = CGMutablePath() 
    path.addLines(between: [CGPoint(x: -8, y: -28), 
        CGPoint(x: -30, y: 9), CGPoint(x:-11, y: 14), CGPoint(x: -10, y: 27), 
        CGPoint(x: 26, y: 22), CGPoint(x: 32, y: 20), 
        CGPoint(x: 30, y: 14), CGPoint(x: 23, y: -17), CGPoint(x: 15, y: -31)]) 
    path.closeSubpath() 
    bird.physicsBody = SKPhysicsBody(polygonFrom: path) 

Le nouveau chemin J'ai créé était un chemin beaucoup plus simple qui avait essentiellement un front carré. Donc, il est impossible que la forme entre en collision avec mon scoreNode deux fois. Cela peut ne pas être des instructions très claires, mais si quelqu'un a les mêmes problèmes, n'hésitez pas à demander! Cela a été le plus grand retour en arrière de faire mon application d'oiseau flappy!