Key Problem
Riddick04

Hey, i have a little problem with character animating:

if(key[KEY_D])
{
x += 1.5;

anim2.addFrame(0, 7);
anim2.addFrame(1, 7);
anim2.addFrame(2, 7);
anim2.addFrame(3, 7);
}

The character runs and animates, if i have pressed "D", but if i release the button, the character don't stop to animate.

Please help.

MfG Riddick04

P.S.: Wenn jemand deutsch spricht, bitte in deutsch antworten! ;)

CursedTyrant

Post your addFrame function and the code responsible for the actual drawing.

And use [ code ] [/ code ] tags.

Riddick04
1void loop()
2{
3 if (!MapLoad("maps/lvl1.FMP"))
4 MapSetPal8();
5 
6 float x = 50, y = 300;
7 
8 scroll = create_sub_bitmap(doubleBuffer, 0, 0, SCREEN_W*2, SCREEN_H);
9 mario2 = load_video_bitmap("sprites/mario/mariorun2.bmp");
10 
11 FrameSource frames2 = FrameSource(mario2, 4);
12
13 Animation anim2(&frames2);
14 
15 anim2.addFrame(0, 7);
16 
17 Sprite sprite2(&anim2);
18 sprite2.setPos(x, y);
19 sprite2.setSpeed(0, 0);
20 
21 int needsRepaint = TRUE;
22
23 syncTimer(&timerCounter);
24 while(!key[KEY_ESC])
25 {
26 if (timerCounter)
27 {
28 do
29 {
30 if(key[KEY_D])
31 {
32 x += 1.5;
33
34 anim2.addFrame(0, 7);
35 anim2.addFrame(1, 7);
36 anim2.addFrame(2, 7);
37 anim2.addFrame(3, 7);
38 }
39 
40 clear_keybuf();
41 
42 sprite2.setPos(x, y);
43 
44 MapDrawBGT (doubleBuffer, 0, 0, 0, 0, 1280, 480);
45 sprite2.tick();
46 sprite2.draw(doubleBuffer);
47
48 --timerCounter;
49 } while (timerCounter >0);
50 
51 needsRepaint = true;
52 }
53 
54 if (needsRepaint)
55 {
56 needsRepaint = false;
57 show();
58 }
59 }
60}

ANIMATION:

1void Animation::addFrame(FrameInfo &info)
2{
3 frames.push_back(info);
4}
5
6void Animation::tick()
7{
8 if (count >0) {
9 ++ticks;
10 if (ticks >= frames[curFrame].delay) {
11 ++curFrame;
12 if (curFrame >= count) {
13 curFrame = 0;
14 }
15 ticks = 0;
16 }
17 }
18}

SPRITE STRUCT:

1struct Sprite {
2 float x, y, dx, dy;
3 Animation *anim;
4
5 Sprite(Animation *ani) {
6 anim = ani;
7 }
8
9 void draw(BITMAP *dest) {
10 anim->render(dest, (int) x, (int) y);
11 }
12
13 void setPos(float x, float y) {
14 this->x = x;
15 this->y = y;
16 }
17
18 void setSpeed(float dx, float dy) {
19 this->dx = dx;
20 this->dy = dy;
21 }
22
23 virtual void tick() {
24 x += dx;
25 y += dy;
26
27 if (x > SCREEN_W) {
28 x = -anim->getFrameSource()->getWidth();
29 }
30 if (y > SCREEN_H) {
31 y = -anim->getFrameSource()->getHeight();
32 }
33 anim->tick();
34 }
35};
36 
37 
38typedef list<Sprite*> SpriteList;
39typedef SpriteList::iterator SpriteIterator;
40 
41BITMAP* load_video_bitmap(const char* name) {
42 BITMAP* bmp = load_bitmap(name, NULL);
43 if (!bmp) {
44 return NULL;
45 }
46 BITMAP *vid = create_video_bitmap(bmp->w, bmp->h);
47 if (vid) {
48 blit(bmp, vid, 0, 0, 0, 0, bmp->w, bmp->h);
49 destroy_bitmap(bmp);
50 return vid;
51 }
52 return bmp;
53}

I hope thats enough!

MfG Riddick04

Elverion

Unless I'm mistaken (which is possible; it is 7 am and i haven't slept yet), you should have goten a compiler error. addFrame appears to take only one argument, whereas you are supplying two.

Anyways, you have a very messy animation method. I suggest you start the whole animation process from scratch. At the program start or something, you might want to create arrays or vectors of the different animations. That is, each array or vector would stand for one animation(i.e. walking left, walking right, attacking left, etc.). Now, the "animations" would just be pointers to the different frames in the sequence they appear. Keep in mind, if you will be doing something like "MyAnim[0] = load_bitmap(...)", that you must then iterate through each array deleting those bitmaps at the end of the program. Because of that, I would suggest using those pointers to point to data from a datafile(check the allegro docs for that).

Since you should now have those predefined animations that are much easier to work with finished, you can more easily handle the playing or pausing of the animation in your objects' draw event. Just keep a variable or two on hand to make note of if you should be playing the animation or not, and what frame you are currently on. Obviously, when you get to the end of the animation, you need to loop back to the 0th frame, and you should probably only increment your animation frames once every few screen frames.

Ich denkst, diese forum ist nur fur Englisch. All posts must either be in english, or contain a translation to it.

Thread #584760. Printed from Allegro.cc