[英]Pyglet/Cocos2d, window appears but sprite doesn't spawn
I am trying to create a sprite class and a key handle to move the sprite, but it doesn't seem to be working. 我正在尝试创建一个Sprite类和一个用于移动Sprite的键控柄,但是它似乎没有用。 When executed the window will appear, but sprite doesn't. 执行后,窗口将出现,但精灵不会。 What's wrong with it? 它出什么问题了?
import pyglet
import cocos
from pyglet.window import key
window = pyglet.window.Window()
keyboard = key.KeyStateHandler()
class Player(cocos.layer.Layer):
def __init__(self):
super(Player, self).__init__()
img = pyglet.image.load('../game/resources/sprite_1.png')
self.sprite = pyglet.sprite.Sprite(img)
def animate(dt, velocity, sprite):
sprite.position += dt * velocity
def on_key_press(self, symbol, keyboard, modifiers):
if symbol == key.RIGHT:
self.img.x += dt * 10
elif symbol == key.LEFT:
self.img.x -= dt * 10
elif symbol == key.UP:
self.img.y += dt * 10
elif symbol == key.DOWN:
self.img.y -= dt * 10
def on_key_release(self, smybol, keyboard, modifiers):
if symbol == key.RIGHT:
self.sprite.x = dt * 0
elif symbol == key.LEFT:
self.sprite.x = dt * 0
elif symbol == key.DOWN:
self.sprite.y = dt * 0
elif symbol == key.UP:
self.sprite.y = dt * 0
pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
pyglet.app.run()
There are quite a few problems in your code which are preventing anything from happening: 您的代码中有很多问题阻止了任何事情的发生:
You're creating a cocos2d entity within pyglet, without telling pyglet how to handle it. 您正在pyglet中创建一个cocos2d实体,而没有告诉pyglet如何处理它。 You're possibly better off using cocos2d for things like window creation etc. 使用cocos2d进行窗口创建等操作可能会更好。
You're creating a pyglet Sprite
when you should probably be using a cocos2d one, and you're not attaching it to the Layer
. 当您可能应该使用cocos2d时,您正在创建pyglet Sprite
,并且没有将其附加到Layer
。
animate
isn't part of the API for Layer
(and even if it was, it would probably not be a static method). animate
不是Layer
API的一部分(即使它是,也可能不是静态方法)。
You haven't defined Player
as an event handler, so on_key_press
etc won't do anything. 您尚未将Player
定义为事件处理程序,因此on_key_press
等不会执行任何操作。
You're passing more arguments into on_key_press
etc than are required, and are referencing dt
without passing it in. 您在on_key_press
等中传递了比所需更多的参数,并且引用了dt
而不传递它。
Here is a modified version of your sample code which should help you move forward: 这是示例代码的修改版本,可以帮助您继续前进:
import cocos
import pyglet
from pyglet.window import key
keyboard = key.KeyStateHandler()
class Player(cocos.layer.Layer):
is_event_handler = True
def __init__(self):
super(Player, self).__init__()
img = pyglet.image.load('../game/resources/sprite_1.png')
self.sprite = cocos.sprite.Sprite(img)
self.add(self.sprite)
def on_key_press(self, symbol, modifiers):
print 'pressing'
if symbol == key.RIGHT:
self.sprite.x += 10
elif symbol == key.LEFT:
self.sprite.x -= 10
elif symbol == key.UP:
self.sprite.y += 10
elif symbol == key.DOWN:
self.sprite.y -= 10
if __name__ == '__main__':
cocos.director.director.init()
player = Player()
main_scene = cocos.scene.Scene( player )
cocos.director.director.run( main_scene )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.