动画问题?

时间:2011-08-23 15:20:05

标签: c++ animation allegro

现在,当我开始使用C ++进行游戏时,我向左或向上走。但是角色只是幻灯片看起来不像他正在走路......而且我已经将所有图片都加载到我的游戏中了正在工作..但我不知道我将如何解决它..我无法弄清楚当你按住按钮时如何改变图片..

顺便说一下,这是在快板..

这是我的绘图播放器代码:

void Player::Draw(BITMAP *Buffer){
    draw_sprite(Buffer, Oskar[picNumber], x, y);
}

Oskar []是包含所有图片的数组的名称..

按下按钮时,以下是更改角色图片的内容:

void Player::Controls(){

if(key[KEY_RIGHT]){
    velocityX = speed;
    picNumber = 6;
}

else if(key [KEY_LEFT]){
    velocityX = -speed;
    picNumber = 9;
}

else{
    velocityX = 0;
}

if(key [KEY_UP]){
    velocityY = -speed;
    picNumber = 3;
}
else if(key [KEY_DOWN]){
    velocityY = speed;
    picNumber = 0;
}
else{
    velocityY = 0;
}

x += velocityX;
y += velocityY;
}

它所有关于我创建picNumber的变量..我拥有的所有图片都在一个数组中,picNumber代表要绘制的图片。 很高兴能得到一些帮助。我一整天都在想这个......

修改

#include "Player.h"
#include "Global.h"
#include <allegro.h>


Player::Player(){

}


Player::~Player(){

}

void Player::Init(){
        x = 10;
        y = 10;
        velocityX = 0;
        velocityY = 0;
        speed = 1;
        picNumber = x % MAXPICS;

        OskarFront[0] = load_bitmap("Character\\OskarFront.bmp", NULL);
        OskarFront[1] = load_bitmap("Character\\OskarStanding.bmp", NULL);
        OskarFront[2] = load_bitmap("Character\\OskarFront2.bmp", NULL);

        OskarBack[0] = load_bitmap("Character\\OskarBack.bmp", NULL);
        OskarBack[1] = load_bitmap("Character\\OskarStandingBack.bmp", NULL);
        OskarBack[2] = load_bitmap("Character\\OskarBack2.bmp", NULL);

        OskarRight[0] = load_bitmap("Character\\Oskar1.bmp", NULL);
        OskarRight[1] = load_bitmap("Character\\Oskar.bmp", NULL);
        OskarRight[2] = load_bitmap("Character\\Oskar2.bmp", NULL);

        OskarLeft[0] = load_bitmap("Character\\OskarLeft.bmp", NULL);
        OskarLeft[1] = load_bitmap("Character\\OskarLeftStand.bmp", NULL);
        OskarLeft[2] = load_bitmap("Character\\OskarLeft2.bmp", NULL);
}

void Player::Update(){
        Player::Controls();
}

void Player::Draw(BITMAP *Buffer){

        if(walkingRight == true){
                draw_sprite(Buffer, OskarRight[picNumber], x, y);
        }

        else if(walkingLeft == true){
                draw_sprite(Buffer, OskarLeft[picNumber], x, y);
        }

        else if(walkingFront == true){
                draw_sprite(Buffer, OskarFront[picNumber], x, y);
        }

        else if(walkingBack == true){
                draw_sprite(Buffer, OskarBack[picNumber], x, y);
        }

        else{
                draw_sprite(Buffer, OskarFront[1], x, y);
        }
}

void Player::Controls(){

        if(key[KEY_RIGHT]){
                velocityX = speed;
                walkingRight = true;
        }

        else if(key [KEY_LEFT]){
                velocityX = -speed;
                walkingLeft = true;
        }

        else{
                walkingRight = false;
                walkingLeft = false;
                velocityX = 0;
        }

        if(key [KEY_UP]){
                velocityY = -speed;
                walkingFront = true;
        }
        else if(key [KEY_DOWN]){
                velocityY = speed;
                walkingBack = true;
        }
        else{
                velocityY = 0;
                walkingFront = false;
                walkingBack = false;
        }

        x += velocityX;
        y += velocityY;
}

这里现在是我在这里获得帮助之后键入的新完整代码..它现在不工作当我走上它显示前面的图片并且正在向上走的图片显示..但左右工作..也它不像动画那样改变画面..

3 个答案:

答案 0 :(得分:3)

每个移动方向只有一个索引!

假设您已经绘制了大约3(左腿向前,重叠,右腿向前),您将需要随着行进的进展随时间或移动增加索引。

有趣的是Allegro已经取得了多大的进步 - 十年前它主要是DOS图形(我用它与DJGPP一起),并且有一份住在伦敦公寓的作者的笔记。 我的想法多么严峻,只是现在才知道我们省份的伦敦价格有多远。

在回答您的评论时,请尝试以下内容:

#define MAXPICS 3
BITMAP *OskarWalksLeft[MAXPICS];
BITMAP *OskarWalksRight[MAXPICS];
picNumber = x % MAXPICS;
if (facingLeft) {
    draw_sprite(Buffer, OskarWalksLeft[picNumber], x, y);
} else //facingRight
    draw_sprite(Buffer, OskarWalksRight[picNumber], x, y);

或者如果您不希望每个像素有一个新帧,但每3个左右尝试替换

picNumber = x % MAXPICS;

picNumber = (x / 3)  % MAXPICS;

我已经尝试整理你的代码并想出这个,我没有allegro所以无法测试它:

#include "Player.h"
#include "Global.h"
#include <allegro.h>

Player::Player(){}

Player::~Player(){}

enum playerDir {left, right, front, back, stationary} direction;
BITMAP *Oskar[stationary][3];

void Player::Init(){
    x = 10;
    y = 10;
    velocityX = 0;
    velocityY = 0;
    speed = 1;
    picNumber = x % MAXPICS;

    char filename[256];
    const char *dirStrs[] = {"left","right","front","back"};
    for (playerDir i = left; i < stationary; (int)i = (int)i + 1)
        for (j = 0; j < 3; ++j) {
            strcpy(filename, "Character\\Oskar");
            strcat(filename, dirStrs[i]);
            strcat(filename, itoa(j));
            strcat(filename, ".bmp");
            Oskar[i][j] = load_bitmap(filename, NULL);
        }
}

void Player::Update(){Player::Controls();}
void Player::Draw(BITMAP *Buffer){
    switch (direction) {
        case left :
                draw_sprite(Buffer, OskarLeft[picNumber], x, y);
                break;
        case right :
                draw_sprite(Buffer, OskarRight[picNumber], x, y);
                break;
        case front :
                draw_sprite(Buffer, OskarFront[picNumber], x, y);
                break;
        case back :
                draw_sprite(Buffer, OskarBack[picNumber], x, y);
                break;
        default:
                draw_sprite(Buffer, OskarFront[1], x, y);
        }
}

void Player::Controls(){
    direction = stationary;
    if(key[KEY_RIGHT]){
        velocityX = speed;
        direction = right;
    } else if(key [KEY_LEFT]){
        velocityX = -speed;
        direction = left;
    } else velocityX = 0;

    if(key [KEY_UP]){
        velocityY = -speed;
        direction = front;
    } else if(key [KEY_DOWN]){
        velocityY = speed;
        direction = back;
    } else velocityY = 0;

    x += velocityX;
    y += velocityY;
 }

答案 1 :(得分:0)

你似乎没有做任何动画。您似乎只是根据用户按下的方向键设置特定帧。你需要一个像这样的函数:

void Player::updateFrame(float time_delta)
{
    time_to_next_frame -= time_delta;
    while (time_to_next_frame <= 0.0f)
    {
        nextFrame();
        time_to_next_frame += time_per_frame;
    }
}

每帧调用一次,传递自上一帧以来经过的时间。

答案 2 :(得分:0)

在回答您对其他2个答案的评论时,您似乎不确定如何不超过特定值。

要做到这一点(例如,你想要的东西在[0,2] + c范围内,其中c是一些常量决定索引的开始。例如,你可以做以下事情:

void Player::nextFrame( int startIndex /* Changes based on your direction */) {
      static unsigned short iIndex = 0;

      // Increment and map value to the range [0, 2]
      ++iIndex;
      iIndex %= 3;


      // Note: this assumes that picNumber is a member function, else you
      // will have to pass it by reference to this function.
      picNumber = iIndex + startIndex;
}

这应该做你想要的(即根据改变起始索引的方向来适应,然后递增,将值映射到范围[0,2]。

希望这有帮助!