Last active
August 29, 2015 14:22
-
-
Save syou007/8cdad5cce401cebca656 to your computer and use it in GitHub Desktop.
Cocos2d v3 気球を重ならないようにするのとリファクタリング ref: http://qiita.com/syou007/items/b480c5e82a1658da3ee4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 気球表示ノード | |
class BalloonNode: CCNode { | |
// 気球本体のSprite | |
weak var _balloonSprite:CCSprite! | |
// 気球生成処理 | |
func didLoadFromCCB() { | |
// 大きさをランダムにします。 | |
self.scale = Float(arc4random_uniform(25) + 25) * 0.01 | |
} | |
// 画面サイズを受け取って気球を生成する位置を設定します。 | |
func setInitPosition(windowSize:CGSize) { | |
// 現在の大きさを取得します。 | |
let width = _balloonSprite.contentSizeInPoints.width * CGFloat(self.scale) | |
let height = _balloonSprite.contentSizeInPoints.height * CGFloat(self.scale) | |
// 画面上部へランダムに表示させます。 | |
self.position.x = CGFloat(arc4random_uniform(UInt32(windowSize.width - width))) + width / 2 | |
self.position.y = windowSize.height + height | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
以下の項目はリファレンスを読んで説明しているのではなく、実体験として理解した内容を書いていますので誤っている箇所もあるかもしれません。 | |
もし誤りを見つけた場合はコメントに記載していただけると助かります。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GameScene: CCNode { | |
// 物理ノード | |
weak var _physicsNode:CCPhysicsNode! | |
// フレーム毎に呼び出されるメソッド | |
override func update(delta: CCTime) { | |
// ランダムに気球を生成します。 | |
if arc4random_uniform(100) == 0 { | |
// バルーンを生成します。 | |
let balloonNode = CCBReader.load("Parts/BalloonNode") as! BalloonNode | |
// 気球の初期表示位置を設定 | |
balloonNode.setInitPosition(self.contentSizeInPoints) | |
// 物理ノードにバルーンを設置 | |
_physicsNode.addChild(balloonNode) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment