我正在基於cocos2d編程指南中的一些introductory lessons(適用於2D遊戲設計)開發以下程序。如何在cocos2d(python)中創建圖像的按鍵觸發器移動?
該程序有兩個類,HelloWorld和KeyDisplay。
HelloWorld顯示一隻鳥的圖像和短語「Get this bird some pizza!」。該短語每2秒使用cocos.actions.ScaleBy
和cocos.actions.Reverse
放大和縮小。它效果很好。
KeyDisplay使用用戶鍵盤輸入並使用on_key_press函數將字符寫入屏幕。它也正常工作。
接下來我想通過KeyDisplay修改HelloWorld中正在進行的操作。例如,我想更改圖像的比例(sprite.scale
)或更改圖像的位置(sprite.position
)。這需要HelloWorld和KeyDisplay類對象相互交談。
不幸的是,KeyDisplay類不知道HelloWorld類,因此變量不可訪問。例如,如果我嘗試在on_key_press
函數中設置sprite.scale = 0.5,則會出現錯誤。
KeyDisplay是否有辦法響應按鍵事件,修改我的HelloWorld類中的精靈和其他變量?我猜這是一個python編程問題和一個cocos2d遊戲設計問題。通常我需要在HelloWorld類中創建精靈和標籤(在__init__(self)
方法中)。到目前爲止,我還沒有被允許在全球範圍內創建精靈或標籤。感謝您的任何建議。
import cocos
import pyglet
# note: needed to change something in the operating system before importing cocos, running this command:
# defaults write org.python.python ApplePersistenceIgnoreState NO
class HelloWorld(cocos.layer.ColorLayer):
def __init__(self):
super(HelloWorld, self).__init__(64,64,224,255)
self.world_width = 1000
self.world_height = 1000
self.px_width = 1000
self.px_height = 1000
label = cocos.text.Label('Get this bird some pizza!',
font_name='Times New Roman',
font_size=32,
anchor_x='center', anchor_y='center')
label.position = 320,100
self.add(label)
sprite = cocos.sprite.Sprite('bower-bird.jpeg')
sprite.position = 320,320
sprite.scale = 1
self.add(sprite, z=1)
scale = cocos.actions.ScaleBy(3, duration=2)
label.do(cocos.actions.Repeat(scale + cocos.actions.Reverse(scale)))
#sprite.do(cocos.actions.Repeat(scale + cocos.actions.Reverse(scale)))
class KeyDisplay(cocos.layer.Layer):
# If you want that your layer receives director.window events
# you must set this variable to 'True'
is_event_handler = True
def __init__(self):
super(KeyDisplay, self).__init__()
self.text = cocos.text.Label("", x=100, y=280)
# To keep track of which keys are pressed:
self.keys_pressed = set()
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)
# Update self.text
self.text.element.text = text
def on_key_press (self, key, modifiers):
"""This function is called when a key is pressed.
'key' is a constant indicating which key was pressed.
'modifiers' is a bitwise or of several constants indicating which
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
"""
self.keys_pressed.add (key)
self.update_text()
def on_key_release (self, key, modifiers):
"""This function is called when a key is released.
'key' is a constant indicating which key was pressed.
'modifiers' is a bitwise or of several constants indicating which
modifiers are active at the time of the press (ctrl, shift, capslock, etc.)
Constants are the ones from pyglet.window.key
"""
self.keys_pressed.remove (key)
self.update_text()
def update_text(self):
key_names = [pyglet.window.key.symbol_string (k) for k in self.keys_pressed]
text = 'Keys: '+','.join (key_names)
# Update self.text
self.text.element.text = text
#start it up ...
cocos.director.director.init()
hello_layer = HelloWorld()
main_scene = cocos.scene.Scene (hello_layer, KeyDisplay())
cocos.director.director.run (main_scene)