我想沿特定方向移动精灵,直到 cocos2d 中的下一次按键。
处理按键的代码如下,
def on_key_press(self, symbol, modifiers):
if symbol == key.LEFT:
DIRECTION="left"
before=self.player.position
height=self.player.position[1]
width=self.player.position[0]-1
self.player.position=width,height
self.player.rotation=-90
if(height>=600 or height<=0):
print ("you lose)
if(width>=800 or width<=0):
print ("you lose)
elif symbol == key.RIGHT:
DIRECTION="right"
before=self.player.position
height=self.player.position[1]
width=self.player.position[0]+1
self.player.position=width,height
self.player.rotation=90
if(height>=600 or height<=0):
print ("you lose)
if(width>=800 or width<=0):
print ("you lose")
...
我尝试了这个函数,它调用上面的函数来保持精灵沿着一个方向移动,
def keep_going(self,DIRECTION):
if(DIRECTION=="left"):
self.on_key_press(key.LEFT,512)
...
但是精灵很容易超出屏幕边界,有没有办法让精灵以受控的方式沿着方向移动?