繁体   English   中英

SpriteKit:精灵定位

[英]SpriteKit : sprite positioning

我最近找到了一个有关如何使用SpriteKit制作类似“飞扬的小鸟”的游戏的教程 我没有使用敲击机制,而是使用了设备加速度计来左右移动鸟。 现在,我的问题是生成管道。 本教程中使用的方法在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];
 }

我的想法是生成从“天空”掉下的管道,而鸟必须在管道之间移动以保持生命。 我希望我的问题的描述很清楚:)谢谢

说明 :管道确实从“天空”掉下,但是问题出在管道在屏幕上的位置。 当我运行项目时,右管道或左管道之间没有缝隙。 我只看到一根巨大的管道在垂直方向上掉落,填充屏幕的很大一部分。

感谢您链接教程!

如果您希望管道掉下,那就让它们掉下! 这些管道连接有物理物体,因此应根据其垂直位置从天上掉下来。 然后,无需使用SKAction来移动管道。 但是,您需要将其dynamic标志更改为YES以便施加重力。 请参阅Xcode参考:

一个布尔值,指示物理模拟是否移动了物理主体。 默认值为是。 如果该值为NO,则物理主体将忽略所有施加到其上的力和脉冲。 基于边缘的实体将忽略此属性; 它们是自动静态的。

我终于找到了解决方案。 通过如下更改管道位置的值,我设法使它们在屏幕上正确显示。

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 );

结果==> 图片

现在最大的挑战是找到一种方法,以某种方式使管道的位置随机。 :)

暂无
暂无

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

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