如何根据演员在 Cocos2d Python 中面对的方向更改我的精灵图像?

问题描述

我是 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

"""

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)