cocos2d-helloworld
2283 ワード
最も基本的な要素
Layerクラスを定義し、HellowrldはLayerから継承し、labelを作成し、画面に表示するためにLayerに追加します.ここで,Layer,LabelはいずれもCocosNodeのサブクラスであり,そのデフォルトのdrawメソッドがある.これをaddに入れるだけで自動的に表示されます.デフォルトの座標原点は左下にあり、以下のコードで示すように簡単です.
アクションの追加
cococos 2 dでは高級なaction処理が多く行われており、QTの処理とよく似ています.
cocos.Actionsの説明では、いろいろな動作ができます.次の例では、文字列labelを左下から中間に移動し、非常に簡単に実現します.
Eventの追加
イベントの処理も便利ですが、ここではイベントを受け入れるにはis_を設定する必要があることに注意します.event_handlerはTrueで、独自のEvent処理方法を追加するのも簡単で、対応する動作にon_を追加するだけですで行ないます.詳細は次のコードで示します.
<本節終了>
Layerクラスを定義し、HellowrldはLayerから継承し、labelを作成し、画面に表示するためにLayerに追加します.ここで,Layer,LabelはいずれもCocosNodeのサブクラスであり,そのデフォルトのdrawメソッドがある.これをaddに入れるだけで自動的に表示されます.デフォルトの座標原点は左下にあり、以下のコードで示すように簡単です.
import cocos
class HelloWorld(cocos.layer.Layer):
def __init__(self):
super(HelloWorld,self).__init__()
label = cocos.text.Label("helloWorld")
self.add(label)
if __name__ == '__main__':
cocos.director.director.init()
layer = HelloWorld()
scene = cocos.scene.Scene(layer)
cocos.director.director.run(scene)
アクションの追加
cococos 2 dでは高級なaction処理が多く行われており、QTの処理とよく似ています.
cocos.Actionsの説明では、いろいろな動作ができます.次の例では、文字列labelを左下から中間に移動し、非常に簡単に実現します.
import cocos
from cocos.actions import *
class HelloWorld(cocos.layer.Layer):
def __init__(self):
super(HelloWorld,self).__init__()
label = cocos.text.Label("helloWorld")
self.add(label)
move = MoveTo((100, 100), 2) + MoveTo((200, 200), 5)
label.do(action)
if __name__ == '__main__':
cocos.director.director.init()
layer = HelloWorld()
scene = cocos.scene.Scene(layer)
cocos.director.director.run(scene)
Eventの追加
イベントの処理も便利ですが、ここではイベントを受け入れるにはis_を設定する必要があることに注意します.event_handlerはTrueで、独自のEvent処理方法を追加するのも簡単で、対応する動作にon_を追加するだけですで行ないます.詳細は次のコードで示します.
import cocos,pyglet
class KeyDisplay(cocos.layer.Layer):
is_event_handler = True
def __init__(self):
super(KeyDisplay,self).__init__()
self.text = cocos.text.Label("",x=100,y=280)
self.keys_pressed = []
self.update_text()
self.add(self.text)
def update_text(self):
key_names = [pyglet.window.key.symbol_string(k) for k in self.keys_pressed]
text = 'Keys:'+','.join(key_names)
self.text.element.text = text
def on_key_press(self,key,modifiers):
self.keys_pressed.append(key)
self.update_text()
if __name__ == '__main__':
cocos.director.director.init()
layer = KeyDisplay()
scene = cocos.scene.Scene(layer)
cocos.director.director.run(scene)
<本節終了>