简体   繁体   English

Spritekit:当节点沿y轴“跳跃”时,如何使游戏屏幕视图向上移动?

[英]Spritekit: how to make game screen view move upwards as node “jumps” along the y axis?

could someone please post an example code of how i could make my game screen move up as my player node "jumps" up the y axis. 有人可以发布一个示例代码,说明我的播放器节点沿y轴“跳跃”时如何使游戏屏幕向上移动。 the same as doodle jump? 和Doodle Jump一样? at the moment it obviously just jumps straight out of the top of the screen. 目前,它显然只是直接跳出了屏幕顶部。

any help with making my "platform" nodes remove themselves from the parent and then reapear when they go out the bottom of the view as i move up the screen would help aswell. 使我的“平台”节点脱离父级的任何帮助,然后在我向上移动屏幕时离开视图底部时重新出现也会有所帮助。

and one last thing, how do i add more than one node to the initial view. 最后一件事,我如何在初始视图中添加多个节点。 [self add child:node] only makes one. [self add child:node]只做一个。

i know these are probably amateur questions, and thats because I'm an amateur. 我知道这些可能是业余问题,那是因为我是业余爱好者。

您在问一个非常广泛的问题,我建议您看一看raywenderlich教程: 如何制作类似Mega Jump的游戏来入门,并在遇到问题时提出更具体的问题。

The solution is to keep the focus on your player node. 解决方案是将注意力集中在播放器节点上。

For example, you have your background node called _worldNode and your player node called _player. 例如,您有一个名为_worldNode的背景节点,以及一个名为_player的播放器节点。 You then add the all your scenery to your _worldNode which would also include your player. 然后,将所有风景添加到_worldNode中,其中还将包括您的播放器。

As your player moves around you can keep him in the center focus by using this code: 当玩家四处移动时,可以使用以下代码将他保持在中心位置:

- (void)didSimulatePhysics
{
    // keep the center of the screen on the player
    _worldNode.position = CGPointMake(-(_player.position.x-(self.size.width/2)), -(_player.position.y-(self.size.height/2)));
}

I placed the code into the didSimulatePhysics because it ensures your player's new location if he is moved by the physics engine. 我将代码放入didSimulatePhysics因为如果您的播放器被物理引擎移动,它可以确保您的播放器位于新的位置。

Pieter's answer is probably going to be the most help, but I'll try to answer the second and third questions you asked, since sangony covered the first. 彼得的答案可能会是最有帮助的,但是我会尽力回答您所问的第二和第三个问题,因为桑格尼覆盖了第一个。

To remove platforms that have fallen off the screen, use node.name = @"platform"; 要删除掉在屏幕上的平台,请使用node.name = @"platform"; when instantiating the platform nodes, and then check their current positions relative to the worldNode in the update method, like this: 实例化平台节点时,然后在update方法中检查它们相对于worldNode的当前位置,如下所示:

-(void)update:(NSTimeInterval)currentTime
{
    [self enumerateChildNodesWithName:@"platform" usingBlock:^(SKNode *node, BOOL *stop) {
        if(node.position.y < _worldNode.position.y-(self.size.height/2))
            [node removeFromParent];
    }];
}

To add multiple nodes to the scene, you have to use [self addChild:node]; 要将多个节点添加到场景中,必须使用[self addChild:node]; multiple times, on multiple nodes, either instantiating each node manually, or setting them up iteratively. 在多个节点上多次,或者手动实例化每个节点,或者迭代设置它们。 If you want the platforms in fairly uniform patterns, a for loop is probably the right way to get this done. 如果您希望平台采用相当统一的模式,则for循环可能是完成此工作的正确方法。 You could also get this done with fewer total nodes by moving the ones that have fallen off the bottom of the screen up to just off the top of the screen, instead of removing them entirely. 您还可以通过将落在屏幕底部的节点移动到刚好在屏幕顶部的节点,而不用完全删除它们,从而用更少的总节点数来完成此任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM