Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SKFun

 SKFun

Building Sprite Kit games with RubyMotion. Presented at the RubyMotion #inspect 2014 conference in San Francisco.

willrax

May 29, 2014
Tweet

More Decks by willrax

Other Decks in Programming

Transcript

  1. one

  2. two

  3. class ! ! ! ! ! ! ! ! end

    def didMoveToView(view) super ! ! ! ! ! end add_skyline add_ground add_pipes add_bird
  4. def flap ! ! ! ! ! ! ! !

    ! ! end one = SKTexture.textureWithImageNamed(“1.png") two = SKTexture.textureWithImageNamed(“2.png") three = SKTexture.textureWithImageNamed(“3.png”) textures = [one, two, three] animation = SKAction.animateWithTextures(textures, timePerFrame: 0.15) bird.repeatActionForever(animation)
  5. def add_skyline txt = SKTexture.textureWithImageNamed(“s.png“) ! mid_x = CGRectGetMidX(self.frame) mid_y

    = CGRectGetMidY(self.frame) ! ! ! ! ! ! ! ! ! ! ! end position = mid_x + (i * mid_x * 2) ! sky = SKSpriteNode.spriteNodeWithTexture(txt) sky.position = CGPointMake(position, mid_y) sky.runAction scroll_action(mid_x, 0.1) ! scene.addChild(sky) 2.times do |i| ! ! ! ! ! ! ! end
  6. def scroll_action(x, time) ! ! ! ! ! ! !

    ! ! end width = (x * 2) dur = time * width ! move = SKAction.moveByX(-width, y: 0, duration: dur) reset = SKAction.moveByX(width, y: 0, duration: 0.0) sequence = SKAction.sequence([move, reset]) ! SKAction.repeatActionForever(sequence)
  7. def ! mid_x = CGRectGetMidX(self.frame) mid_y = CGRectGetMidY(self.frame) ! !

    ! ! ! ! ! ! ! ! ! end position ! sky sky.position sky.runAction scroll_action(mid_x, ! scene.addChild(sky 2 ! ! ! ! ! ! ! end txt = SKTexture.textureWithImageNamed(“s.png“)
  8. six

  9. def init super self.addChild(top_pipe) self.addChild(bottom_pipe) self end ! def top

    pipe = SKSpriteNode.spriteNodeWithImageNamed(“t.png”) pipe.position = CGPointMake(0, y + 450) pipe end ! def bottom pipe = SKSpriteNode.spriteNodeWithImageNamed(“b.png”) pipe.position = CGPointMake(0, y) pipe end
  10. def ! ! ! ! ! end earth = CGVectorMake(0,-5.0)

    ! physicsWorld.speed = 1.0 physicsWorld.gravity = earth
  11. - touchesBegan(touches, withEvent: event) ! - touchesMoved(touches, withEvent: event) !

    - touchesEnded(touches, withEvent: event) ! - touchesCancelled(touches, withEvent: event)
  12. def touchesBegan(touches, withEvent: event) node = childNodeWithName "bird" ! node.physicsBody.velocity

    = CGVectorMake(0, 0) node.physicsBody.applyImpulse CGVectorMake(0, 8) end
  13. ten

  14. BIRD = 0x1 < 1 GROUND = 0x1 < 2

    PIPE = 0x1 < 3 ! pipe.physicsBody.categoryBitMask = WORLD ground.physicsBody.categoryBitMask = WORLD bird.physicsBody.categoryBitMask = BIRD bird.physicsBody.contactTestBitMask = WORLD WORLD = GROUND | PIPE
  15. def super ! ! ! ! end ! physicsWorld.gravity !

    setup_scene class ! ! ! ! ! ! ! end physicsWorld.contactDelegate = self
  16. def check_controller bird = childNodeWithName("bird") ! controllers = GCController.controllers controller

    = controllers.first.extendedGamepad ! if controller.buttonA.isPressed? bird.physicsBody.velocity = CGVectorMake(0, 0) bird.physicsBody.applyImpulse CGVectorMake(0, 8) end end