[英]cocos2d process events randomly while animating
這個問題太奇怪了,我覺得我在某個地方犯了一個大錯。
我在python3中使用cocos2d。 我創建了一個簡單的示例,它基本上是samples/hello_world.py
和samples/handling_events.py
的合並,只是在查看事件時可視化移動文本。
問題是:在動畫進行過程中,事件基本上是隨機處理的:有時,按ESC鍵,程序會在幾個瞬間停止,有時它不會停止。 按下鍵盤通常不會在文本中顯示任何內容,但是如果按下很多按鍵,可能會得到一對可視化。 與鼠標相同:如果您移動或點擊很多,有時您會看到已處理的事件。
我無法理解發生了什么:是不是cocos應該在每個渲染幀之前處理事件? 我錯過了什么嗎?
源代碼
import cocos
import pyglet
from cocos.actions import *
class HelloWorld(cocos.layer.ColorLayer):
def __init__(self):
super(HelloWorld, self).__init__(64, 64, 200, 255)
label = cocos.text.Label(
'Hello, World!',
font_name='Times New Roman',
font_size=32,
anchor_x='center',
anchor_y='center')
label.position = (320, 240)
# label.scale = 2 # Pixel scale
self.add(label)
scale = ScaleBy(3, duration=2)
# Unlimited repeat
#label.do(Repeat(scale + Reverse(scale)))
# Limited repeat
label.do((scale + Reverse(scale)) * 3)
class KeyDisplay(cocos.layer.Layer):
# This is necessary to receive events
is_event_handler = True
def __init__(self):
super(KeyDisplay, self).__init__()
self.text = cocos.text.Label('', x=100, y=200)
self.down_keys_ = set()
self.update_text()
self.add(self.text)
def update_text(self):
key_names = [pyglet.window.key.symbol_string(k) for k in self.down_keys_]
self.text.element.text = 'Keys: ' + ' '.join(key_names)
def on_key_press(self, key, modifiers):
self.down_keys_.add(key)
self.update_text()
def on_key_release(self, key, modifiers):
self.down_keys_.remove(key)
self.update_text()
class MouseDisplay(cocos.layer.Layer):
is_event_handler = True
def __init__(self):
super(MouseDisplay, self).__init__()
self.text = cocos.text.Label('', x=100, y=150)
self.add(self.text)
def on_mouse_motion(self, x, y, dx, dy):
self.text.element.text = 'Moving'
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
self.text.element.text = 'Dragging'
def on_mouse_press(self, x, y, buttons, modifiers):
self.text.element.text = 'Pressed'
def on_mouse_release(self, x, y, buttons, modifiers):
self.text.element.text = 'Released'
if __name__ == '__main__':
cocos.director.director.init()
# A layer with text
hello_layer = HelloWorld()
hello_layer.do(RotateBy(360 * 2, duration=5))
# A layer for key
key_layer = KeyDisplay()
# A layer for mouse
mouse_layer = MouseDisplay()
main_scene = cocos.scene.Scene(hello_layer, key_layer, mouse_layer)
cocos.director.director.run(main_scene)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.