更改用于显示对象的精灵的最佳方法是什么?
What is the best way to change the sprite used to display an object?
我有一个对象可以根据它面向的方向改变它的显示。该对象采用 4x4 帧网格,并将每行 4 帧用作每个状态的动画。
目前,我正在使用以下方法将它们加载到单独的精灵中:
def create_animation(image_grid, start_idx, end_idx):
frames = []
for frame in image_grid[start_idx:end_idx]:
frames.append(pyglet.image.AnimationFrame(frame, 0.1))
return pyglet.sprite.Sprite(pyglet.image.Animation(frames))
然后把应该显示的sprite加到一个batch
来绘制,不该绘制的就去掉。
但是,阅读文档时,我看到了这个:
Sprite.batch
The sprite can be migrated from one batch to another, or removed from its batch (for individual drawing). Note that this can be an expensive operation.
有没有更好的方法来实现我正在尝试做的事情,而不会影响批量切换单个精灵的性能?
您可以将图像加载为 TextureGrid:
img = pyglet.resource.image("obj_grid.png")
img_grid = pyglet.image.ImageGrid(
img,
4, # rows, direction
4 # cols, frames of the animation
)
texture_grid = pyglet.image.TextureGrid(img_grid) # this is the one you actually use
创建您的(单个)精灵:
batch = pyglet.graphics.Batch() # unless you already have a batch
my_object = pyglet.sprite.Sprite(
img=texture_grid[0, 0], # [row, col] for starting grid /frame
batch=batch
)
Determine/change 方向("row",我是根据输入猜测的?)。
循环 0-3("col"/动画帧):
pyglet.clock.schedule_interval(change_frame, 0.1, my_object)
def change_frame(dt, my_object): # pyglet always passes 'dt' as argument on scheduled calls
my_object.col += 1
my_object.col = my_object.col & 3 # or my_object.col % 3 if its not a power of 2
并手动设置框架:
current_frame = self.texture_grid[self.row, self.col].get_texture()
my_object._set_texture(current_frame)
没有额外调用 draw(),没有弄乱 batch()。一切都像往常一样绘制,但你可以根据需要更改它绘制的纹理:)
我有一个对象可以根据它面向的方向改变它的显示。该对象采用 4x4 帧网格,并将每行 4 帧用作每个状态的动画。
目前,我正在使用以下方法将它们加载到单独的精灵中:
def create_animation(image_grid, start_idx, end_idx):
frames = []
for frame in image_grid[start_idx:end_idx]:
frames.append(pyglet.image.AnimationFrame(frame, 0.1))
return pyglet.sprite.Sprite(pyglet.image.Animation(frames))
然后把应该显示的sprite加到一个batch
来绘制,不该绘制的就去掉。
但是,阅读文档时,我看到了这个:
Sprite.batch
The sprite can be migrated from one batch to another, or removed from its batch (for individual drawing). Note that this can be an expensive operation.
有没有更好的方法来实现我正在尝试做的事情,而不会影响批量切换单个精灵的性能?
您可以将图像加载为 TextureGrid:
img = pyglet.resource.image("obj_grid.png")
img_grid = pyglet.image.ImageGrid(
img,
4, # rows, direction
4 # cols, frames of the animation
)
texture_grid = pyglet.image.TextureGrid(img_grid) # this is the one you actually use
创建您的(单个)精灵:
batch = pyglet.graphics.Batch() # unless you already have a batch
my_object = pyglet.sprite.Sprite(
img=texture_grid[0, 0], # [row, col] for starting grid /frame
batch=batch
)
Determine/change 方向("row",我是根据输入猜测的?)。
循环 0-3("col"/动画帧):
pyglet.clock.schedule_interval(change_frame, 0.1, my_object)
def change_frame(dt, my_object): # pyglet always passes 'dt' as argument on scheduled calls
my_object.col += 1
my_object.col = my_object.col & 3 # or my_object.col % 3 if its not a power of 2
并手动设置框架:
current_frame = self.texture_grid[self.row, self.col].get_texture()
my_object._set_texture(current_frame)
没有额外调用 draw(),没有弄乱 batch()。一切都像往常一样绘制,但你可以根据需要更改它绘制的纹理:)