Question

I've found lately a tutorial on how to build a game like "flappy bird" using 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.

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

Was it helpful?

Solution 2

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. :)

OTHER TIPS

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. However, you need to change their dynamic flag to YES so that gravitational force is applied. See Xcode reference:

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. This property is ignored on edge-based bodies; they are automatically static.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top