Reputation: 12273
This problem is so weird that I think I'm doing a huge mistake somewhere.
I'm using cocos2d in python3. I created a simple example, which basically is a merge of samples/hello_world.py
and samples/handling_events.py
, and just visualize a moving text while also checking for events.
Problem is: events are processed basically randomly while the animation is in progress: sometimes, pressing ESC, the program stops after few instants, sometimes it doesn't stop. Pressing the keyboard usually doesn't show anything in the text, but if you press a lot of keys maybe you get a couple to get visualized. Same with the mouse: if you move or click a lot, sometimes you see the event processed.
I can't understand what's happening: isn't cocos supposed to process the events before every rendered frame? Am I missing something?
Source code
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)
Upvotes: 0
Views: 143
Reputation: 12273
This issue was due to a bug in pyglet.
For details:
Upvotes: 0