Neler Yeni

xCode uygulamasından Swift dili ile Oyun Yazarken Oyunda Bug Çıkıyor

Katılım
5 Ocak 2021
Mesajlar
33
Dahası  
Reaksiyon skoru
10
İsim
Kubilay KIRSAY
BUG KODU;
Thread 1: breakpoint 1.1 (1)

Hata Veren Satır;
colorSwitch.physicsBody?.categoryBitMask = PhysicsCategories.switchCategory

Tüm Kodlar;

import SpriteKit



enum PlayColors {

static let colors = [

UIColor(red: 231/255, green: 76/255, blue: 60/255, alpha: 1.0),

UIColor(red: 241/255, green: 196/255, blue: 15/255, alpha: 1.0),

UIColor(red: 46/255, green: 204/255, blue: 113/255, alpha: 1.0),

UIColor(red: 52/255, green: 152/255, blue: 219/255, alpha: 1.0)

]

}



enum SwitchState: Int {

case red, yellow, green, blue

}



class GameScene: SKScene {



var colorSwitch: SKSpriteNode!

var switchState = SwitchState.red

var currentaColorIndex: Int?



let scoreLabel = SKLabelNode(text: "0")

var score = 0



override func didMove(to view: SKView) {

setupPhysics()

layoutScene()

}



func setupPhysics() {

physicsWorld.gravity = CGVector(dx: 0.0, dy: -5.0)

physicsWorld.contactDelegate = self

}



func layoutScene() {

backgroundColor = UIColor(red:44/255, green: 62/255, blue: 80/255, alpha: 1.0)



colorSwitch = SKSpriteNode(imageNamed: "ColorCircle")

colorSwitch.size = CGSize(width: frame.size.width/3, height: frame.size.width/3)

colorSwitch.position = CGPoint(x: frame.midX, y: frame.minY + colorSwitch.size.height)

colorSwitch.zPosition = ZPositions.colorSwitch

colorSwitch.physicsBody = SKPhysicsBody(circleOfRadius: colorSwitch.size.width/2)

colorSwitch.physicsBody?.categoryBitMask = PhysicsCategories.switchCategory

colorSwitch.physicsBody?.isDynamic = false

addChild(colorSwitch)



scoreLabel.fontName = "AvenirNext-Bold"

scoreLabel.fontSize = 60.0

scoreLabel.fontColor = UIColor.white

scoreLabel.position = CGPoint(x: frame.midX, y: frame.midY)

scoreLabel.zPosition = ZPositions.label

addChild(scoreLabel)



spawnBall()

}



func updateScoreLabel() {

scoreLabel.text = "\(score)"

}



func spawnBall() {

currentaColorIndex = Int(arc4randomuniform(UInt32(4)))



let ball = SKSpriteNode(texture:SKTexture(imageNamed: "ball"), color: PlayColors.colors[currentaColorIndex!], size: CGSize(width: 30.0, height: 30.0))

ball.colorBlendFactor = 1.0

ball.name = "Ball"

ball.position = CGPoint(x: frame.midX, y: frame.maxY)

ball.zPosition = ZPositions.ball

ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width/2)

ball.physicsBody?.categoryBitMask = PhysicsCategories.ballCategory

ball.physicsBody?.contactTestBitMask = PhysicsCategories.switchCategory

ball.physicsBody?.collisionBitMask = PhysicsCategories.none

addChild(ball)

}



func turnWheel() {

if let newState = SwitchState(rawValue: switchState.rawValue + 1) {

switchState = newState

} else {

switchState = .red

}



colorSwitch.run(SKAction.rotate(byAngle: .pi/2, duration: 0.25))

}



func gameOver() {

UserDefaults.standard.set(score, forKey: "RecentScore")

if score > UserDefaults.standard.integer(forKey: "Highscore") {

UserDefaults.standard.set(score, forKey: "Highscore")

}



let menuScene = MenuScene(size: view!.bounds.size)

view!.presentScene(menuScene)

}



override func touchesBegan( touches: Set<UITouch>, with event: UIEvent?) {

turnWheel()

}



}



extension GameScene: SKPhysicsContactDelegate {



func didBegin(_ contact: SKPhysicsContact) {

let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask



if contactMask == PhysicsCategories.ballCategory | PhysicsCategories.switchCategory {

if let ball = contact.bodyA.node?.name == "Ball" ? contact.bodyA.node as? SKSpriteNode : contact.bodyB.node as? SKSpriteNode {

if currentaColorIndex == switchState.rawValue {

run(SKAction.playSoundFileNamed("bling", waitForCompletion: false))

score += 1

updateScoreLabel()

ball.run(SKAction.fadeOut(withDuration: 0.25), completion: {

ball.removeFromParent()

self.spawnBall()

})

} else {

gameOver()

}

}

}

}



}
 
Top Bottom