Hi,
I am trying to move a sprite around the screen using the cursor
keys, my code is below (its very basic)
Is there a better way than using "self.sprite.stop()" to stop the
sprite moving?
I tried a few ways mentioned on this forum but I cannot get them to
work, such as:
self.sprite.remove_action( self.action )
I am very new to python programming so any advice appreciated
Thanks.
#!/usr/bin/python
import sys
import os
import cocos
from cocos.actions import *
class CarScene(cocos.layer.ColorLayer):
KEY_UP = 65362
KEY_DOWN = 65364
KEY_LEFT = 65361
KEY_RIGHT = 65363
KEY_LEFT_CTRL = 65507
is_event_handler = True
def __init__(self):
# background color
super( CarScene, self ).__init__(64,64,224,255)
# show car
self.sprite = cocos.sprite.Sprite('car.png')
self.sprite.position = 320, 240
self.add( self.sprite )
def on_key_press (self, key, modifiers):
if key == self.KEY_LEFT:
print( "move left" )
self.action = Repeat( MoveBy( (-1, 0), 0.01) )
elif key == self.KEY_RIGHT:
print( "move right" )
self.action = Repeat( MoveBy( (+1, 0), 0.01) )
elif key == self.KEY_UP:
print( "move up" )
self.action = Repeat( MoveBy( (0, +1), 0.01) )
elif key == self.KEY_DOWN:
print( "move down" )
self.action = Repeat( MoveBy( (0, -1), 0.01) )
elif key == self.KEY_LEFT_CTRL:
print( "start fire" )
else:
print( "key_press = " + str(key) )
self.sprite.do( self.action )
def on_key_release (self, key, modifiers):
if key == self.KEY_LEFT:
print( "stop moving left" )
elif key == self.KEY_RIGHT:
print( "stop moving right" )
elif key == self.KEY_UP:
print( "stop moving up" )
elif key == self.KEY_DOWN:
print( "stop moving down" )
elif key == self.KEY_LEFT_CTRL:
print( "stop fire" )
else:
print( "key_press = " + str(key) )
self.sprite.stop()
if __name__ == "__main__":
cocos.director.director.init()
cocos.director.director.run( cocos.scene.Scene( CarScene() ) )
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"cocos2d discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cocos-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---