简体   繁体   中英

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. the same as 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.

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. You then add the all your scenery to your _worldNode which would also include your player.

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.

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"; when instantiating the platform nodes, and then check their current positions relative to the worldNode in the update method, like this:

-(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]; 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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