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

用SpriteKit 
做一個FB 投籃遊戲

Johnlin
April 12, 2016

用SpriteKit 
做一個FB 投籃遊戲

Johnlin

April 12, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. ఁଌख੎ • SKNode ໵ੋ UIResponder త subclass • ༻ touchesBegan(_:withEvent:)

    ိܭࢉىᴍ • ༻ touchesEnded(_:withEvent:) ိܭࢉऴᴍ • ၷݸ૬ݮबՄҎፙग़ख੎తํ޲
  2. SKPhysicsBody • 㑌ݸSKNode ౎ՄҎ༗Ұݸ physicsBody ိਐߦ෺ ཧ໛ٖɻ • ՄҎࣗݾఆٛܗ㐫ɼ໵ՄҎኺtexture ࣗಈ㗞ੜ

    • ՄҎઃఆ֤छ෺ཧಛੑɼ࣭ྔɼີ౓ɼຎࡲ܎Ꮠɼኧ ੑ܎ᏐɼҠಈ્ྗɼટ᫚્ྗ
  3. 㟚ٿ • ҝ߶߶తLabel Ճ্Ұݸᅵܗత physicsBody physicsBody = SKPhysicsBody(circleOfRadius: 95, center:

    CGPointMake(0, 75)) • ࡏ㟚ٿલ౎ෆཁडॏྗӨڹ physicsBody?.affectedByGravity = false • ሣٿࢪՃҰݸিྔɼฒ׌⬏ಈॏྗ let impulse = CGVectorMake(base * (dx/norm), base * (dy/norm)) physicsBody?.applyImpulse(impulse) physicsBody?.affectedByGravity = true
  4. ឈᐽ䉰಍ • ࠨӈ֤ᙘҰᑍឈᐽ let l = SKPhysicsBody(edgeFromPoint: CGPointMake(0, 2*h), toPoint:

    CGPointMake(3*h, 2*h)) let r = SKPhysicsBody(edgeFromPoint: CGPointMake(rad-3*h, 2*h), toPoint: CGPointMake(rad, 2*h)) • ૊੒Ұݸཟᐽ ring!.physicsBody = SKPhysicsBody(bodies: [l,r])
  5. ্ঋ࣌ෆཁ䉰಍ func ballAboveRing()->Bool { return basket?.position.y < ball?.position.y } override

    func update(currentTime: NSTimeInterval) { if ballAboveRing() { basket?.ringEnabled = true } }
  6. categoryBitMask:UInt32 & collisionBitMask:UInt32 • ᙛၷݸ෺ᱪA,B ᯪۙሣํ࣌ɼ။༻&(bitwise and) ိ ֬ఆၷݸ౦੢။ෆ။䉰಍
 (a.categoryBitMask

    & b.collisionBitMask) && 
 (b.categoryBitMask & a.collisionBitMask) • ஫ҙɿෆੋ௚઀༻ძํతcollisionBitMask ိ൑Ꮧɼ ࣕੋҰํతcategory ሣ্ሣํత collision
  7. ᐽՄҎબᎩཁෆཁ䉰಍ var ringEnabled:Bool { set { ring?.physicsBody?.collisionBitMask = newValue ?

    CollisionMask.Ball : CollisionMask.None ring?.physicsBody?.categoryBitMask = newValue ? CollisionMask.Basket : CollisionMask.None } get { return ring?.physicsBody?.collisionBitMask == CollisionMask.Ball } }
  8. 䉰಍ఁଌ • ࢖༻ contactTestBitMask & categoryBitMask 
 (a.categoryBitMask & b.contactTestBitMask)

    && 
 (b.categoryBitMask & a.contactTestBitMask) • ༗䉰಍ᚙੜ࣌။ݺڣ scene.physicsWorld.contactDelegate త
 didBeginContact(_:) & didEndContact(_:)
  9. መ࡞ܭ෼ఁଌث • ᩋఁଌثෆ။಍౸ٿɼୠੋೳఁଌᢛٿత઀ᨀ phy.collisionBitMask = CollisionMask.None phy.contactTestBitMask = CollisionMask.Ball phy.categoryBitMask

    = CollisionMask.Sensor • Լ్߱தɼ䉰౸बՃҰ෼ func didEndContact(contact: SKPhysicsContact) { guard basket!.ringEnabled else { return } score += 1 }
  10. Q&A