菜鸟教程小白 发表于 2022-12-12 12:30:36

ios - Sprite-kit 如何将整数值传递给下一个场景


                                            <p><p>我有 2 个场景 <code>GameScene</code> 和 <code>GameLostScene</code>,在 <code>GameLostScene</code> 我创建了 highScore 但我不知道如何从 <code>游戏场景</code>。然后我尝试用 <code>NSLog</code> 打印分数我可以看到哪个值不正确,我应该怎么做才能使它正确?</p>

<p><code>GameScene</code> 方法</p>

<pre><code> -(void) gameLost {

    SKScene *nextScene = [ initWithSize:self.size ];
    SKTransition *doors = [ SKTransition fadeWithDuration:1.0];
    NSLog(@&#34;%ld&#34;, self.currentScore);
    ;
}
</code></pre>

<p>游戏迷失场景</p>

<pre><code>GameScene *gameScene;
long score = gameScene.currentScore;
NSLog(@&#34;%ld&#34;, score);
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果您没有发现您的分数数据重要或敏感,请使用 <code>NSUserDefaults</code></p>

<p><strong>第一个场景:</strong></p>

<pre><code>NSUserDefaults *defaults = ;
;
</code></pre>

<p><strong>第二幕:</strong></p>

<pre><code>NSUserDefaults *defaults = ;
yourScore =
</code></pre>

<p>这也将存储您的分数。 </p>

<p><strong>或者你可以只通过场景传递数据。</strong></p>

<p>在第一场景
在您的 .m 文件中声明:</p>

<pre><code>static NSInteger staticScore;
</code></pre>

<p>创建类方法并返回这个值:</p>

<pre><code>+ (NSInteger) passScore {
   return staticScore;
}
</code></pre>

<p>在另一个场景中<code>#import</code>这个类并得到这样的值:</p>

<pre><code>;
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - Sprite-kit 如何将整数值传递给下一个场景,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/26688547/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/26688547/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - Sprite-kit 如何将整数值传递给下一个场景