Theeaxe

Udemy Flappy Bird Issue

Aug 23rd, 2014
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import SpriteKit
  2.  
  3. class GameScene: SKScene {
  4.    
  5.     // Make the Bird Sprite
  6.     var bird = SKSpriteNode()
  7.     // Make the Sky Sprite for Background Color
  8.     var skyColor = SKColor()
  9.    
  10.     override func didMoveToView(view: SKView) {
  11.         /* Setup your scene here */
  12.        
  13.         // Set the Sky Sprite to a color
  14.         skyColor = SKColor(red: 113.0/255.0, green: 197.0/255.0, blue: 207.0/255.0, alpha: 1.0)
  15.         self.backgroundColor = skyColor
  16.  
  17.         // 2 textures for animating the birds wings
  18.         var birdTexture1 = SKTexture(imageNamed:"Bird1")
  19.         birdTexture1.filteringMode = SKTextureFilteringMode.Nearest
  20.         var birdTexture2 = SKTexture(imageNamed:"Bird2")
  21.         birdTexture2.filteringMode = SKTextureFilteringMode.Nearest
  22.        
  23.         // Putting the animation together and running it forever
  24.         var animation = SKAction.animateWithTextures([birdTexture1, birdTexture2], timePerFrame: 0.2)
  25.         var flap = SKAction.repeatActionForever(animation)
  26.        
  27.         // Setting the texture and animation to the bird and the starting position
  28.         bird = SKSpriteNode(texture: birdTexture1)
  29.         bird.position = CGPoint(x: self.frame.size.width / 2.8, y: CGRectGetMidY(self.frame))
  30.         bird.runAction(flap)
  31.        
  32.         // Add the bird to the scene
  33.         self.addChild(bird)
  34.        
  35.         // Set the Ground Texture
  36.         var groundTexture = SKTexture(imageNamed: "Ground")
  37.         groundTexture.filteringMode = SKTextureFilteringMode.Nearest
  38.        
  39.         // Move the Ground
  40.         var moveGroundSprite = SKAction.moveByX(-groundTexture.size().width, y: 0, duration: NSTimeInterval(0.1 * groundTexture.size().width))
  41.         // Reset the Ground
  42.         var resetGroundSprite = SKAction.moveByX(groundTexture.size().width, y: 0, duration: 0.0)
  43.         // Set it to be on loop and last forever (put it in the for loop below
  44.         var moveGroundSpriteForever = SKAction.repeatActionForever(SKAction.sequence([moveGroundSprite, resetGroundSprite]))
  45.        
  46.        
  47.        
  48.        
  49.         // Set a for loop so that the ground keeps going as far as the bird/player goes
  50.         for var i:CGFloat = 0; i<2 + self.frame.size.width / (groundTexture.size().width); ++i {
  51.             var sprite = SKSpriteNode(texture: groundTexture)
  52.             sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2)
  53.             sprite.runAction(moveGroundSpriteForever)
  54.             self.addChild(sprite)
  55.         }
  56.        
  57.         // Set the Skyline Texture
  58.         var skylineTexture = SKTexture(imageNamed: "Skyline")
  59.         skylineTexture.filteringMode = SKTextureFilteringMode.Nearest
  60.        
  61.         var moveSkylineSprite = SKAction.moveByX(-skylineTexture.size().width, y: 0, duration: NSTimeInterval(0.1 * skylineTexture.size().width))
  62.         var resetSkylineSprite = SKAction.moveByX(skylineTexture.size().width, y: 0, duration: 0.0)
  63.         var moveSkylineSpriteForever = SKAction.repeatActionForever(SKAction.sequence([moveSkylineSprite, resetSkylineSprite]))
  64.        
  65.         // Make it continue like the Ground Texture
  66.         for var i:CGFloat = 0; i<2.0 + self.frame.size.width / (skylineTexture.size().width); ++i {
  67.             var sprite = SKSpriteNode(texture: skylineTexture)
  68.             sprite.zPosition = -20;
  69.             sprite.position = CGPointMake(i * sprite.size.width, sprite.size.height / 2 + groundTexture.size().height)
  70.             sprite.runAction(moveSkylineSpriteForever)
  71.             self.addChild(sprite)
  72.  
  73.         }
  74.        
  75.     }
  76.    
  77.     override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
  78.         /* Called when a touch begins */
  79.    
  80.     }
  81.    
  82.     override func update(currentTime: CFTimeInterval) {
  83.         /* Called before each frame is rendered */
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment