I want to move a sprite along a particular direction until next keypress in cocos2d.
The code to handle a keypress is as follows,
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")
...
I tried this function which calls the above function to keep the sprite moving along a direction,
def keep_going(self,DIRECTION):
if(DIRECTION=="left"):
self.on_key_press(key.LEFT,512)
...
but the sprite easily goes beyond the screen boundaries, Is there a way to make the sprite move along the direction in a controlled fashion?