在Sprite Kit中实现生活和得分计数的最佳方法(静态,IoC等) [英] The best way to implement lives and score count in Sprite Kit (static, IoC, other?)

查看:98
本文介绍了在Sprite Kit中实现生活和得分计数的最佳方法(静态,IoC等)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Java的背景,但是多年没有编码了.最近,我开始对提高编码技能感兴趣,并选择在Swift中为我的孩子创建学习应用.我使用带有GameviewController和多个场景的Sprite Kit创建了基本的Swift游戏.但是,我遇到了一个基本问题,该问题与将场景中的点和生命计数等基本数据传递到gameviewcontroller有关.

I have background in Java but havn't been coding in years. Lately I've taken interest to warm my coding skills again and have chosen to create learning apps for my kids in Swift. I've created basic Swift game utilizing the Sprite Kit with gameviewcontroller and multiple scenes. However I run into a basic question which is related to passing basic data such as points and lives counts from scenes to gameviewcontroller.

在过去,我会通过创建一个静态成员来完成此工作的,该成员将保留剩余的生命并计分,但是今天在Swift中情况如何?我认为IoC与静态成员的关系更现代,但是Swift和这种情况又如何呢?

Back in the day, I would have done it by creating a static member that would hold the lives left and score count but how is it today in Swift? I undertand that IoC would be the more modern equilevant to static members but howabout Swift and howabout this case?

是否存在针对Swift或StackOverFlow用户提出的解决方案概念的IoC框架?

Are there IoC frameworks for Swift or StackOverFlow users' proposals for solution concept?

推荐答案

要在多个类之间传递数据,您的变量必须是全局变量.您必须在类之外声明变量.

To pass data between several classes your variable has to be global. You have to declare your variable out of classes.

例如:

var score:Int = 0

class GameScene {
   //Your game here
}

像这样,您将在所有其他类(如GameViewController)中调用score

Like this your will call score in all others class like your GameViewController

这篇关于在Sprite Kit中实现生活和得分计数的最佳方法(静态,IoC等)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆