- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经尝试了几天来实现一个单例方法来更新我正在制作的游戏的每个级别的分数。我无法找出实现它的正确方法。我在构建和运行项目时没有错误,但在运行游戏时不会显示分数标签。我不知道为什么,但我知道这与单例方法没有正确实现有关。任何意见将不胜感激。下面的代码是我的第一个也是开头的场景,目前尚未显示分数标签或分数。
这是游戏的第一关:
import SpriteKit
class Singleton {
static let sharedInstance = Singleton()
var ScoreLabel = UILabel()
var Score : Int = 0
}
struct PhysicsCategory {
static let Enemy : UInt32 = 1
static let Bullet : UInt32 = 2
static let Player : UInt32 = 3
}
class GameScene: SKScene, SKPhysicsContactDelegate {
var HighScore = Int()
var Player = SKSpriteNode(imageNamed: "GoodGuy.png")
var Level1Label = UILabel()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var timer = NSTimer.scheduledTimerWithTimeInterval(5.0, target: self, selector: "update", userInfo: nil, repeats: true)
var HighScoreDefault = NSUserDefaults.standardUserDefaults()
if (HighScoreDefault.valueForKey("HighScore") != nil){
HighScore = HighScoreDefault.valueForKey("HighScore") as! NSInteger
}
else{
HighScore = 0
}
physicsWorld.contactDelegate = self
self.scene?.backgroundColor = UIColor.blackColor()
self.scene?.size = CGSize(width:640, height: 1136)
self.addChild(SKEmitterNode(fileNamed: "MagicParticle")!)
Player.position = CGPointMake(self.size.width/2, self.size.height/8)
Player.physicsBody = SKPhysicsBody (rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCategory.Player
Player.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
Player.physicsBody?.dynamic = false
Level1Label = UILabel (frame: CGRect(x:0, y:0, width: view.frame.size.width/3, height: 30))
Level1Label.center = CGPoint(x: view.frame.size.width/1.675 , y: view.frame.size.height/1.05)
Level1Label.textColor = UIColor.whiteColor()
Level1Label.text = "Level 1"
self.view?.addSubview(Level1Label)
Singleton.sharedInstance.ScoreLabel.text = "\(Singleton.sharedInstance.Score)"
Singleton.sharedInstance.ScoreLabel = UILabel(frame: CGRect(x:0, y:0, width:100, height:20))
Singleton.sharedInstance.ScoreLabel.backgroundColor = UIColor.clearColor()
Singleton.sharedInstance.ScoreLabel.textColor = UIColor.whiteColor()
self.view?.addSubview(Singleton.sharedInstance.ScoreLabel)
var Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)
var Enemytimer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: Selector("SpawnEnemies"), userInfo: nil, repeats: true)
self.addChild(Player)
}
func update() {
self.view?.presentScene(GameScene2())
Level1Label.removeFromSuperview()
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.Bullet) ||
(firstBody.categoryBitMask == PhysicsCategory.Bullet) && (secondBody.categoryBitMask == PhysicsCategory.Enemy)){
CollisionWithBullet(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == PhysicsCategory.Enemy) && (secondBody.categoryBitMask == PhysicsCategory.Player) ||
(firstBody.categoryBitMask == PhysicsCategory.Player) && (secondBody.categoryBitMask == PhysicsCategory.Enemy)){
CollisionWithPlayer(firstBody.node as! SKSpriteNode, Player: secondBody.node as! SKSpriteNode)
}
}
func CollisionWithBullet(Enemy: SKSpriteNode, Bullet: SKSpriteNode){
Enemy.removeFromParent()
Bullet.removeFromParent()
Singleton.sharedInstance.Score++
Singleton.sharedInstance.ScoreLabel.text = "\(Singleton.sharedInstance.Score)"
}
func CollisionWithPlayer(Enemy: SKSpriteNode, Player: SKSpriteNode){
var ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Singleton.sharedInstance.Score, forKey: "Score")
ScoreDefault.synchronize()
if (Singleton.sharedInstance.Score > HighScore){
var HighScoreDefault = NSUserDefaults.standardUserDefaults()
HighScoreDefault.setValue(Singleton.sharedInstance.Score, forKey: "HighScore")
}
Enemy.removeFromParent()
Player.removeFromParent()
self.view?.presentScene(EndScene())
Level1Label.removeFromSuperview()
}
func SpawnBullets(){
var Bullet = SKSpriteNode(imageNamed: "Bullet.png")
Bullet.zPosition = -5
Bullet.position = CGPointMake(Player.position.x, Player.position.y)
let action = SKAction.moveToY(self.size.height + 30, duration: 1.0)
let actionDone = SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action, actionDone]))
Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategory.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCategory.Enemy
Bullet.physicsBody?.affectedByGravity = false
Bullet.physicsBody?.dynamic = false
self.addChild(Bullet)
}
func SpawnEnemies(){
var Enemy = SKSpriteNode(imageNamed: "BadGuy.png")
var MinValue = self.size.width/8
var MaxValue = self.size.width - 150
let SpawnPoint = UInt32(MaxValue - MinValue)
Enemy.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
Enemy.physicsBody = SKPhysicsBody(rectangleOfSize: Enemy.size)
Enemy.physicsBody?.categoryBitMask = PhysicsCategory.Enemy
Enemy.physicsBody?.contactTestBitMask = PhysicsCategory.Bullet
Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.dynamic = true
let action = SKAction.moveToY(-70, duration: 3.0)
let actionDone = SKAction.removeFromParent()
Enemy.runAction(SKAction.sequence([action, actionDone]))
Enemy.runAction(SKAction.repeatActionForever(action))
self.addChild(Enemy)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
这是游戏的第二关:
导入SpriteKit
struct PhysicsCategory2 {
static let Enemy : UInt32 = 1//00000000000000000000000000000001
static let Bullet : UInt32 = 2//00000000000000000000000000000010
static let Player : UInt32 = 3//00000000000000000000000000000100
}
class GameScene2: SKScene, SKPhysicsContactDelegate {
var HighScore = Int()
var Player = SKSpriteNode(imageNamed: "GoodGuy.png")
var Level2Label = UILabel()
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var HighScoreDefault = NSUserDefaults.standardUserDefaults()
if (HighScoreDefault.valueForKey("HighScore") != nil){
HighScore = HighScoreDefault.valueForKey("HighScore") as! NSInteger
}
else{
HighScore = 0
}
physicsWorld.contactDelegate = self
self.scene?.backgroundColor = UIColor.blackColor()
self.scene?.size = CGSize(width:640, height: 1136)
self.addChild(SKEmitterNode(fileNamed: "MagicParticle")!)
Player.position = CGPointMake(self.size.width/2, self.size.height/8)
Player.physicsBody = SKPhysicsBody (rectangleOfSize: Player.size)
Player.physicsBody?.affectedByGravity = false
Player.physicsBody?.categoryBitMask = PhysicsCategory2.Player
Player.physicsBody?.contactTestBitMask = PhysicsCategory2.Enemy
Player.physicsBody?.dynamic = false
Level2Label = UILabel (frame: CGRect(x:0, y:0, width: view.frame.size.width/3, height: 30))
Level2Label.center = CGPoint(x: view.frame.size.width/1.675 , y: view.frame.size.height/1.05)
Level2Label.textColor = UIColor.whiteColor()
Level2Label.text = "Level 2"
self.view?.addSubview(Level2Label)
var Timer = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: Selector("SpawnBullets"), userInfo: nil, repeats: true)
var Enemytimer = NSTimer.scheduledTimerWithTimeInterval(1.5, target: self, selector: Selector("SpawnEnemies"), userInfo: nil, repeats: true)
self.addChild(Player)
Singleton.sharedInstance.ScoreLabel.text = "\(Singleton.sharedInstance.Score)"
Singleton.sharedInstance.ScoreLabel = UILabel(frame: CGRect(x:0, y:0, width:100, height:20))
Singleton.sharedInstance.ScoreLabel.backgroundColor = UIColor.clearColor()
Singleton.sharedInstance.ScoreLabel.textColor = UIColor.whiteColor()
}
func didBeginContact(contact: SKPhysicsContact) {
var firstBody : SKPhysicsBody = contact.bodyA
var secondBody : SKPhysicsBody = contact.bodyB
if ((firstBody.categoryBitMask == PhysicsCategory2.Enemy) && (secondBody.categoryBitMask == PhysicsCategory2.Bullet) ||
(firstBody.categoryBitMask == PhysicsCategory2.Bullet) && (secondBody.categoryBitMask == PhysicsCategory2.Enemy)){
CollisionWithBullet(firstBody.node as! SKSpriteNode, Bullet: secondBody.node as! SKSpriteNode)
}
else if ((firstBody.categoryBitMask == PhysicsCategory2.Enemy) && (secondBody.categoryBitMask == PhysicsCategory2.Player) ||
(firstBody.categoryBitMask == PhysicsCategory2.Player) && (secondBody.categoryBitMask == PhysicsCategory2.Enemy)){
CollisionWithPlayer(firstBody.node as! SKSpriteNode, Player: secondBody.node as! SKSpriteNode)
}
}
func CollisionWithBullet(Enemy: SKSpriteNode, Bullet: SKSpriteNode){
Enemy.removeFromParent()
Bullet.removeFromParent()
Singleton.sharedInstance.Score++
Singleton.sharedInstance.ScoreLabel.text = "\(Singleton.sharedInstance.Score)"
}
func CollisionWithPlayer(Enemy: SKSpriteNode, Player: SKSpriteNode){
var ScoreDefault = NSUserDefaults.standardUserDefaults()
ScoreDefault.setValue(Singleton.sharedInstance.Score, forKey: "Score")
ScoreDefault.synchronize()
if (Singleton.sharedInstance.Score > HighScore){
var HighScoreDefault = NSUserDefaults.standardUserDefaults()
HighScoreDefault.setValue(Singleton.sharedInstance.Score, forKey: "HighScore")
}
Enemy.removeFromParent()
Player.removeFromParent()
self.view?.presentScene(EndScene())
Level2Label.removeFromSuperview()
Singleton.sharedInstance.ScoreLabel.removeFromSuperview()
}
func SpawnBullets(){
var Bullet = SKSpriteNode(imageNamed: "Bullet.png")
Bullet.zPosition = -5
Bullet.position = CGPointMake(Player.position.x, Player.position.y)
let action = SKAction.moveToY(self.size.height + 30, duration: 1.0)
let actionDone = SKAction.removeFromParent()
Bullet.runAction(SKAction.sequence([action, actionDone]))
Bullet.physicsBody = SKPhysicsBody(rectangleOfSize: Bullet.size)
Bullet.physicsBody?.categoryBitMask = PhysicsCategory2.Bullet
Bullet.physicsBody?.contactTestBitMask = PhysicsCategory2.Enemy
Bullet.physicsBody?.affectedByGravity = false
Bullet.physicsBody?.dynamic = false
self.addChild(Bullet)
}
func SpawnEnemies(){
var Enemy = SKSpriteNode(imageNamed: "BadGuy.png")
var MinValue = self.size.width/8
var MaxValue = self.size.width - 150
let SpawnPoint = UInt32(MaxValue - MinValue)
Enemy.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
Enemy.physicsBody = SKPhysicsBody(rectangleOfSize: Enemy.size)
Enemy.physicsBody?.categoryBitMask = PhysicsCategory2.Enemy
Enemy.physicsBody?.contactTestBitMask = PhysicsCategory2.Bullet
Enemy.physicsBody?.affectedByGravity = false
Enemy.physicsBody?.dynamic = true
let action = SKAction.moveToY(-70, duration: 3.0)
let actionDone = SKAction.removeFromParent()
Enemy.runAction(SKAction.sequence([action, actionDone]))
Enemy.runAction(SKAction.repeatActionForever(action))
self.addChild(Enemy)
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
/* Called when a touch begins */
for touch in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
Player.position.x = location.x
}
}
override func update(currentTime: CFTimeInterval) {
/* Called before each frame is rendered */
}
}
这是游戏的Game Over页面:
import Foundation
import SpriteKit
class EndScene: SKScene {
var RestartButton : UIButton!
var HighScore : Int!
var HighScoreLabel : UILabel!
var GameOverLabel : UILabel!
override func didMoveToView(view: SKView) {
self.scene?.backgroundColor = UIColor.blackColor()
self.scene?.size = CGSize(width:640, height: 1136)
self.addChild(SKEmitterNode(fileNamed: "MagicParticle")!)
RestartButton = UIButton(frame: CGRect(x:0, y:0, width: view.frame.size.width/3
, height: 30))
RestartButton.titleLabel?.adjustsFontSizeToFitWidth = true
RestartButton.center = CGPoint(x: view.frame.size.width/2 , y: view.frame.size.height/1.5)
RestartButton.setTitle("Restart", forState: UIControlState.Normal)
RestartButton.showsTouchWhenHighlighted = true
RestartButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
RestartButton.addTarget(self, action: Selector("Restart"), forControlEvents: UIControlEvents.TouchUpInside)
self.view?.addSubview(RestartButton)
var ScoreDefault = NSUserDefaults.standardUserDefaults()
var Score = ScoreDefault.valueForKey("Score") as! NSInteger
var HighScoreDefault = NSUserDefaults.standardUserDefaults()
HighScore = HighScoreDefault.valueForKey("HighScore") as! NSInteger
Singleton.sharedInstance.ScoreLabel = UILabel (frame: CGRect(x:0, y:0, width: view.frame.size.width/3, height: 30))
Singleton.sharedInstance.ScoreLabel.center = CGPoint(x: view.frame.size.width/1.6 , y: view.frame.size.height/2.5)
Singleton.sharedInstance.ScoreLabel.text = "\(Singleton.sharedInstance.Score)"
Singleton.sharedInstance.ScoreLabel.textColor = UIColor.whiteColor()
self.view?.addSubview(Singleton.sharedInstance.ScoreLabel)
HighScoreLabel = UILabel (frame: CGRect(x:0, y:0, width: view.frame.size.width/3, height: 30))
HighScoreLabel.center = CGPoint(x: view.frame.size.width/1.6 , y: view.frame.size.height/2)
HighScoreLabel.textColor = UIColor.whiteColor()
HighScoreLabel.text = "\(HighScore)"
self.view?.addSubview(HighScoreLabel)
GameOverLabel = UILabel (frame: CGRect(x:0, y:0, width: view.frame.size.width/3.8, height: 30))
GameOverLabel.center = CGPoint(x: view.frame.size.width/2 , y: view.frame.size.height/10)
GameOverLabel.textColor = UIColor.whiteColor()
GameOverLabel.text = "Game Over"
self.view?.addSubview(GameOverLabel)
}
func Restart(){
self.view?.presentScene(GameScene(), transition: SKTransition.crossFadeWithDuration(0.3))
RestartButton.removeFromSuperview()
HighScoreLabel.removeFromSuperview()
Singleton.sharedInstance.ScoreLabel.removeFromSuperview()
GameOverLabel.removeFromSuperview()
}
}
我想一个新问题是,除了更改变量名称之外,我是否还需要做其他事情才能使单例方法在场景之间工作
最佳答案
你在哪里调用 DisplayGameScore() ? (你没有)
除此之外,这不太好
static let ScoreLabel = Singleton()
static let Score = Singleton()
您创建了两个 Singleton 实例,这是错误的
然后你有同名 ScoreLabel 的实例变量,这也不太好
关于ios - 如何使用 Singleton 在快速 Sprite Kit 游戏中显示分数标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169334/
直接从 Python 代码运行 pylint 时,我似乎无法获得任何返回值。从命令行运行它会生成一个漂亮的报告,在底部有一个总结分数。 我已经尝试将“Run”的返回值放入一个变量中,并获取它的“rep
我是 Python 新手,正在尝试学习单词检测。我有一个带有单词的数据框 sharina['transcript'] Out[25]: 0 thank you for calling my
http://jsfiddle.net/q8P7Y/ 我在最后显示最终分数时遇到问题,有很多方法可以做到这一点,但我不确定什么是最好的。 正如你所看到的,下一个按钮只是 div 的显示/隐藏,而不是页
我使用滑动 slider 并有计数器分数。它计数很好,但我需要计数 =(所有幻灯片 - 1)。例如,如果我有 20 张幻灯片,我想显示总数 19。有什么办法可以做到这一点吗?我使用他们网站上的常规 j
我使用滑动 slider 并有计数器分数。它计数很好,但我需要计数 =(所有幻灯片 - 1)。例如,如果我有 20 张幻灯片,我想显示总数 19。有什么办法可以做到这一点吗?我使用他们网站上的常规 j
我试图在按下按钮时添加分数,分数显示在 JTextField 中,但是当按下按钮时,分数会添加,它显示为 0。我有一个存储分数的整数字段 private int score=0; yesButton
我可以在选项(单选按钮)随机播放之前计算分数/分数,如下面的代码所示。在Collection.shuffle()之前,选项是固定的,因为 CorrectChoice将始终分配给c2单选按钮。那么我可以
我在这里的代码只能得到87%的代码,因为“带有非正参数的加法参数什么也没做。我该如何解决呢?我尝试了更多的方法,但是我什至无法解决此错误在同学的帮助下 说明是: 对于此分配,您将创建一个存储分数的类。
昨天,我尝试以一种方式执行此操作...今天我尝试另一种方式,但仍然卡住了。我必须找到一种使用整数除法和取模来做到这一点的方法。这是我的代码,后面是错误消息。 public int evaluateFr
我这里有一些特殊字符: http://209.141.56.244/test/char.php 但是当我在这里通过 ajax 抓取这个文件时,它们显示为 back ?标记: http://209.14
我得到了一张图表 G与 n顶点,标记自 1至 n (2 a_1 -> a_2 -> ... a_k -> n A然后将占据 1 的所有“子节点”节点, a_1 , ... a_x (其中 x = ce
我有一个看起来像这样的 mongodb 集合: db.scores.insert({"name": "Bob", value: 96.3, timeStamp:'2010-9-27 9:32:00'}
我试图更好地了解 lucene 如何对我的搜索进行评分,以便我可以对我的搜索配置或文档内容进行必要的调整。 以下是分数明细的一部分。 产品: 0.34472802 = queryWeight,
在我网站上用户生成的帖子下,我有一个类似亚马逊的评级系统: Was this review helpful to you: Yes | No 如果有投票,我会在该行上方显示结果,如下所示:
对于我的项目,我需要找出哪些搜索结果被视为“良好”匹配。目前,分数因查询而异,因此需要以某种方式对它们进行标准化。标准化分数将允许选择高于给定阈值的结果。 我为 Lucene 找到了几个解决方案: h
我有一个由 57 个变量组成的数据文件。由于测量水平不均匀,我想将其中的大约 12 个转换为 z 分数。我查找了互联网资源和帮助文件。一个互联网资源建议我需要 Rbasic 包(不存在)。我使用了 s
我对 SOLR 核心运行查询并使用过滤器限制结果例如 fq: {!frange l=0.7 }query($q)。我知道 SOLR 分数不有绝对意义,但是0.7(只是一个例子)是计算出来的基于用户输入
我想找到不同的方法来解决我遇到的现实生活问题:想象一下进行一场比赛或一场游戏,在此期间用户收集积分。您必须构建一个查询来显示具有最佳“n”分数的用户列表。 我举一个例子来澄清。假设这是用户表,其中包含
我有很多 wiki 页面,我想训练一个分类器,看看是否可以通过一些特征(包括段落的位置和段落的 lucene 分数)来确定重点搜索的位置。我尝试将每个段落视为一个文档,这使我能够获得每个段落的 luc
我是 R 编程新手,在使用一些基本代码时遇到问题。 我有一个包含以下列的数据框:条件(因子)、用户(因子)和灵敏度(int)。对于每个用户有 20 个敏感项。我需要为每个用户创建一个具有标准化敏感度分
我是一名优秀的程序员,十分优秀!