我是 Cocos2d 的新手,我正在研究如何使用 pyglet ImageGrid 和 Animation 方法来创建精灵动画。我已经成功地将正确的切片分配给我的僵尸精灵对象 - 在init()函数中 - 取决于它面向的方向(在创建时随机分配)。僵尸精灵然后在他们走向边界时通过循环。一旦他们到达边界然后改变方向,动画就不会更新为面向相同的方向,所以我最终会得到一堆月球漫步僵尸……这并不是我想要的效果。如何在动画改变方向时更新它们?
"""
def __init__(self, x, y):
super(Zombie, self).__init__()
duration = 0.1
self.x = x
self.y = y
self.speed = 45
directions = [-1, 1]
i = randint(0, 1)
for index, direction in enumerate(directions):
if i == index:
self.direction = direction
img = load('sprites/zombiewalk.png')
img_grid = ImageGrid(img, 1, 12, item_width=32, item_height=48)
if self.direction == -1:
animation = Animation.from_image_sequence(img_grid[3:6], duration)
sprite = cocos.sprite.Sprite(animation)
self.add(sprite)
elif self.direction == 1:
animation = Animation.from_image_sequence(img_grid[6:9], duration)
sprite = cocos.sprite.Sprite(animation)
self.add(sprite)
def update(self, elapsed):
new_x = self.position[0] + self.speed * self.direction * elapsed
self.position = (new_x, self.y)
if self.position[0] < 0 or self.position[0] > 800:
self.direction *= -1
"""