简体   繁体   English

SpriteKit:精灵定位

[英]SpriteKit : sprite positioning

I've found lately a tutorial on how to build a game like "flappy bird" using SpriteKit. 我最近找到了一个有关如何使用SpriteKit制作类似“飞扬的小鸟”的游戏的教程 Instead of implementing the tap-mechanism, I've used the device accelerometer to move the bird, right and left. 我没有使用敲击机制,而是使用了设备加速度计来左右移动鸟。 My problem, now, is with generating pipes. 现在,我的问题是生成管道。 The method used in the tutorial creates pipes on the x axe and not the y axe, which I want to do. 本教程中使用的方法在x轴上创建管道,而不是在y轴上创建管道,这是我想要做的。

-(void)createPipes
{
   SKTexture* _pipeTexture1 = [SKTexture textureWithImageNamed:@"Pipe1"];
   _pipeTexture1.filteringMode = SKTextureFilteringNearest;
   SKTexture* _pipeTexture2 = [SKTexture textureWithImageNamed:@"Pipe2"];
   _pipeTexture2.filteringMode = SKTextureFilteringNearest;

   SKNode* pipePair = [SKNode node];
   pipePair.position = CGPointMake( self.frame.size.width + _pipeTexture1.size.width * 2,   0 );
   pipePair.zPosition = -10;

   CGFloat y = arc4random() % (NSInteger)( self.frame.size.height / 3 );

   SKSpriteNode* pipe1 = [SKSpriteNode spriteNodeWithTexture:_pipeTexture1];
   [pipe1 setScale:2];
   pipe1.position = CGPointMake( self.frame.size.width/2 -100, self.frame.size.height+250 );
   pipe1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipe1.size];
   pipe1.physicsBody.dynamic = NO;
   [pipePair addChild:pipe1];

   SKSpriteNode* pipe2 = [SKSpriteNode spriteNodeWithTexture:_pipeTexture2];
   [pipe2 setScale:2];
   pipe2.position = CGPointMake( self.frame.size.width/2 +100, self.frame.size.height+250 );
   pipe2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:pipe2.size];
   pipe2.physicsBody.dynamic = NO;
   [pipePair addChild:pipe2];

   SKAction* movePipes = [SKAction repeatActionForever:[SKAction moveByX:0 y:-2 duration:0.02]];
   [pipePair runAction:movePipes];

   [self addChild:pipePair];
 }

My idea is to generate pipes that fall from the "sky" and the bird has to move between the pipes to keep living. 我的想法是生成从“天空”掉下的管道,而鸟必须在管道之间移动以保持生命。 I hope that the description of my problem was very clear :) Thanks 我希望我的问题的描述很清楚:)谢谢

Clarification : Pipes do fall from the "sky" but the problem lies with their positioning on the screen. 说明 :管道确实从“天空”掉下,但是问题出在管道在屏幕上的位置。 When I run the project, there's no gap between the right pipe or the left one. 当我运行项目时,右管道或左管道之间没有缝隙。 I only see a giant pipe falling, filling, vertically, a good proportion of the screen. 我只看到一根巨大的管道在垂直方向上掉落,填充屏幕的很大一部分。

Thanks for linking the tutorial! 感谢您链接教程!

If you want the pipes to fall, then just let them fall! 如果您希望管道掉下,那就让它们掉下! The pipes have physics bodies attached so they should fall down from the sky depending on their vertical position. 这些管道连接有物理物体,因此应根据其垂直位置从天上掉下来。 There's no need to use the SKAction for moving the pipes then. 然后,无需使用SKAction来移动管道。 However, you need to change their dynamic flag to YES so that gravitational force is applied. 但是,您需要将其dynamic标志更改为YES以便施加重力。 See Xcode reference: 请参阅Xcode参考:

A Boolean value that indicates whether the physics body is moved by the physics simulation. 一个布尔值,指示物理模拟是否移动了物理主体。 The default value is YES. 默认值为是。 If the value is NO, then the physics body ignores all forces and impulses applied to it. 如果该值为NO,则物理主体将忽略所有施加到其上的力和脉冲。 This property is ignored on edge-based bodies; 基于边缘的实体将忽略此属性; they are automatically static. 它们是自动静态的。

I have finally found a solution. 我终于找到了解决方案。 By changing the values of pipes position as follow, I've managed to make them appear properly on the screen. 通过如下更改管道位置的值,我设法使它们在屏幕上正确显示。

SKSpriteNode* pipe1 = [SKSpriteNode spriteNodeWithTexture:_pipe1];
pipe1.position = CGPointMake( (-0.39*pipe1.size.width), 0 );
SKSpriteNode* pipe2 = [SKSpriteNode spriteNodeWithTexture:_pipe2];
pipe2.position = CGPointMake(self.frame.size.width  *1.8, 0 );

The result ==> picture 结果==> 图片

The big challenge now is find a way to make the position of the pipes somehow random. 现在最大的挑战是找到一种方法,以某种方式使管道的位置随机。 :) :)

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

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