这里的第二个问题,我的第一个问题是答案并且真的很有帮助,所以我会再试一次。
这是代码和解释:
import cocos
from cocos.actions import *
import pyglet
from pyglet.window import key
from pyglet.window.key import KeyStateHandler
from cocos.director import director
keys = KeyStateHandler()
class ScaleTestLayer(cocos.layer.Layer):
is_event_handler = True
def __init__(self):
super( ScaleTestLayer, self ).__init__()
self.sprite = cocos.sprite.Sprite('grossini.png')
self.sprite.position = 320,240
self.drag = False
self.add(self.sprite)
self.rect = self.sprite.get_rect()
def on_mouse_release(self, x, y, buttons, modifiers):
if self.rect.contains(x, y) == True:
if self.drag == False:
scale = ScaleBy(5, duration=1)
if buttons == pyglet.window.mouse.LEFT:
self.sprite.do(scale)
if buttons == pyglet.window.mouse.MIDDLE:
rotate = RotateBy(180, 1)
self.sprite.do(rotate)
if buttons == pyglet.window.mouse.RIGHT:
scale = (Reverse(scale))
self.sprite.do(scale)
else:
self.drag = False
def on_mouse_drag(self, x, y, dx, dy, buttons, modifiers):
if self.rect.contains(x, y):
self.drag = True
self.sprite.position = director.get_virtual_coordinates (x, y)
self.rect.center = self.sprite.position
def on_key_press(self, symbol, modifiers):
if keys[key.SPACE]:
jump = JumpBy((5,0), duration=0.5)
self.sprite.do(jump)
if keys[key.RIGHT]:
move = MoveBy((10,0), 0.5)
self.sprite.do(move)
if __name__ == "__main__":
cocos.director.director.init()
test_layer = ScaleTestLayer ()
main_scene = cocos.scene.Scene (test_layer)
cocos.director.director.run (main_scene)
所以这是我的问题。当我按 SPACE 或 RIGHT 时,它不会执行该操作。我打印了值,并且 keys[key.SPACE] 返回 False,即使我确实按下了这些按钮。我错过了什么?也许一些push_handlers?我不知道如何使用它。
预先感谢。