gpt4 book ai didi

ios - 使用 Sprite Kit 和 Swift 在游戏中显示当前分数

转载 作者:行者123 更新时间:2023-11-28 07:12:32 25 4
gpt4 key购买 nike

这几天我一直在使用 Sprite Kit 开发 Flappy Bird 类型的游戏。我是编程新手,所以我一直在尝试通过各种在线视频和教程自学制作游戏。我已经到了需要创建一个评分系统的地步,该评分系统将在屏幕上实时显示当前分数。我一直在寻找一个很好的教程,但没有找到一个。我已经在下面的代码中设置了一个分数标签。我在下面包含了我的整个场景。我只需要知道每次鸟儿飞过管道时如何更新分数标签文本。任何意见或建议将不胜感激,谢谢。

//
// ArcheryScene.swift
// FlappyBird (swift)
//
// Created by Brandon Ballard on 1/6/15.
// Copyright (c) 2015 Brandon Ballard. All rights reserved.
//

import UIKit
import SpriteKit

class ArcheryScene: SKScene, SKPhysicsContactDelegate {

var bird = SKSpriteNode()
var pipeUpTexture = SKTexture()
var pipeDownTexture = SKTexture()
var pipesMoveAndRemove = SKAction()
var impulse = 1
var count = 0
var scoreLabel = SKLabelNode()
let scoreLabelName = "scoreLabel"

let pipeGap = 150.0

enum ColliderType:UInt32 {
case BIRD = 1
case PIPE = 2
}

override func didMoveToView(view: SKView) {
/* Setup your scene here */

backgroundColor = SKColor.cyanColor()

//physics
self.physicsWorld.gravity = CGVectorMake(0.0, -15.0);
self.physicsWorld.contactDelegate = self

//Bird
var birdTexture = SKTexture(imageNamed:"Bird")
birdTexture.filteringMode = SKTextureFilteringMode.Nearest

bird = SKSpriteNode(texture: birdTexture)
bird.setScale(0.6)
bird.position = CGPoint(x: self.frame.width * 0.35 + 20, y: self.frame.size.height * 0.95)

bird.physicsBody = SKPhysicsBody(circleOfRadius: bird.size.height / 2.0)
bird.physicsBody?.dynamic = true
bird.physicsBody?.allowsRotation = true
bird.physicsBody?.affectedByGravity = true
bird.physicsBody!.collisionBitMask = ColliderType.BIRD.rawValue
bird.physicsBody!.contactTestBitMask = ColliderType.PIPE.rawValue
self.addChild(bird)

//Ground
var groundTexture = SKTexture(imageNamed: "Ground")

var sprite = SKSpriteNode(texture: groundTexture)
sprite.setScale(2.0)
sprite.position = CGPointMake(self.size.width / 2, sprite.size.height / 2.0)

self.addChild(sprite)

var ground = SKNode()

ground.position = CGPointMake(0, groundTexture.size().height + 0)
ground.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundTexture.size().height * 2.0))
ground.physicsBody?.dynamic = false
self.addChild(ground)

//Score Label

scoreLabel = SKLabelNode(fontNamed: "ScoreLabel")
scoreLabel.name = scoreLabelName
scoreLabel.fontSize = 125
scoreLabel.fontColor = SKColor.whiteColor()
scoreLabel.text = "\(count)"
println(size.height)
scoreLabel.position = CGPointMake(frame.size.width / 2, frame.size.height / 14)
self.addChild(scoreLabel)

//Pipes

//Create the Pipes

pipeUpTexture = SKTexture(imageNamed: "PipeUp")
pipeDownTexture = SKTexture(imageNamed: "PipeDown")

//Movement of Pipes

let distanceToMove = CGFloat(self.frame.size.width + 2.0 * pipeUpTexture.size().width)
let movePipes = SKAction.moveByX(-distanceToMove, y: 0.0, duration: NSTimeInterval(0.01 * distanceToMove))
let removePipes = SKAction.removeFromParent()

pipesMoveAndRemove = SKAction.sequence([movePipes,removePipes])

//Spawn Pipes

let spawn = SKAction.runBlock({() in self.spawnPipes()})
let delay = SKAction.waitForDuration(NSTimeInterval(2.0))
let spawnThenDelay = SKAction.sequence([spawn,delay])
let spawnThenDelayForever = SKAction.repeatActionForever(spawnThenDelay)

self.runAction(spawnThenDelayForever)

}

func spawnPipes() {

let pipePair = SKNode()
pipePair.position = CGPointMake(self.frame.size.width + pipeUpTexture.size().width * 2, 0)
pipePair.zPosition = -10

let height = UInt32(self.frame.size.height / 4)
let y = arc4random() % height + height

var pipeDown = SKSpriteNode(texture: pipeDownTexture)
pipeDown.setScale(2.0)////////
pipeDown.position = CGPointMake(3.0, CGFloat(y) + pipeDown.size.height + CGFloat(pipeGap) )

pipeDown.physicsBody = SKPhysicsBody(rectangleOfSize: pipeDown.size)
pipeDown.physicsBody?.dynamic = false
pipeDown.physicsBody!.affectedByGravity = false
pipeDown.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
pipePair.addChild(pipeDown)

var pipeUp = SKSpriteNode(texture: pipeUpTexture)
pipeUp.setScale(2.0)
pipeUp.position = CGPointMake(0.0, CGFloat(y))

pipeUp.physicsBody = SKPhysicsBody(rectangleOfSize: pipeUp.size )
pipeUp.physicsBody?.dynamic = false
pipeUp.physicsBody!.affectedByGravity = false
pipeUp.physicsBody!.collisionBitMask = ColliderType.PIPE.rawValue
pipePair.addChild(pipeUp)

pipePair.runAction(pipesMoveAndRemove)
self.addChild(pipePair)

}

func didBeginContact(contact: SKPhysicsContactDelegate) {

impulse = 0

let fadeOut = SKAction.sequence([SKAction.waitForDuration(3.0)])

let welcomeReturn = SKAction.runBlock({
let Transition = SKTransition.revealWithDirection(SKTransitionDirection.Down, duration: 1.0)
let welcomeScene = GameScene(fileNamed: "GameScene")
welcomeScene.scaleMode = .AspectFill
self.scene!.view?.presentScene(welcomeScene, transition: Transition)
})

let sequence = SKAction.sequence([fadeOut, welcomeReturn])
self.runAction(sequence)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */

for touch: AnyObject in touches {

let location = touch.locationInNode(self)

if bird.position.y > (self.frame.size.height * 0.999){
impulse = 0
}

if impulse == 1 {
bird.physicsBody?.velocity = CGVectorMake( 0, 0 )
bird.physicsBody?.applyImpulse(CGVectorMake(0,25))
}

}
}

override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}

最佳答案

只需在您的管道之间设置一个不可见的 Sprite 。当您的鸟儿经过它时,您可以检测到它并增加您的分数。

你可以像这样制作一个隐形 Sprite

let scoreSprite = SKSpriteNode(color: SKColor.clearColor(), size: theSize)

向其添加物理体,一切顺利。

关于ios - 使用 Sprite Kit 和 Swift 在游戏中显示当前分数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826762/

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