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

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

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.

回答1:

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.



回答2:

You're asking a very broad question, I suggest you take a look at one of the raywenderlich tutorials: How to make a game like Mega Jump to get you started, and ask more specific questions as you run into problems.



回答3:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!