On Sep 16, 5:06 pm, claudio canepa <[email protected]> wrote:
> On Wed, Sep 16, 2009 at 8:27 AM, karan <[email protected]> wrote:
>
> > Hi, I want to control a sprite on the screen by using the arrow keys.
> > After looking at the examples, I was able to make such a program,
> > however, if I continue to press the arrow keys, it stops. I have to
> > keep releasing the key and pressing it again to move the sprite. Is
> > there a way by which the sprite continues to move if I keep the key
> > pressed?
> > Here is my code:
>
> > import cocos
> > from cocos.actions import *
> > import pyglet
> > from pyglet.window import key
>
> > class hello(cocos.layer.ColorLayer):
>
> > is_event_handler = True
>
> > def __init__(self):
> > super(hello, self).__init__(0,0,0,255)
>
> > self.sprite = cocos.sprite.Sprite("car1.png")
> > self.sprite.position = (30,240)
> > self.add(self.sprite)
>
> > def on_key_press(self, symbol, modifiers):
>
> > if(symbol == key.RIGHT):
> > self.sprite.do(MoveBy((5, 0), 0.1)))
> > if(symbol == key.LEFT):
> > self.sprite.do(MoveBy((-5, 0), 0.1)))
> > if(symbol == key.UP):
> > self.sprite.do(MoveBy((0, 5), 0.1)))
> > if(symbol == key.DOWN):
> > self.sprite.do(MoveBy((0, -5), 0.1)))
>
> > cocos.director.director.init()
> > hello = hello()
> > scene = cocos.scene.Scene(hello)
> > cocos.director.director.run(scene)
>
> In short, on_key_press don't do autorepeat, look at pyglet programming guide
> in the section 'working with the keyboard'.
> You have two alternatives:
> a. track yourself the key state, as you can seen in the example posted
> athttp://thread.gmane.org/gmane.comp.python.cocos2d.user/684
> (look the attach in the second message)
>
> b. read the pyglet docs about keyboards, it is possible to query the keys
> state at each time.
>
> --
> claudio
Thanks claudio, its working now. I guess the key was to put the keys
pressed in a list.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---