Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

Anonymous

unregistriert

1

14.05.2006, 15:08

Animations Problem

hi, ich hab ein problem, ich programmiere ja grad ein mario game mit allegro. wenn ich D drücke, dann beginnt er zu animieren und bewegt sich auch, aber wenn ich D wieder loslasse, dann bleibt er zwar stehen, animiert aber weiter.

hier ist der erforderliche code:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void loop()
{
    if (!MapLoad("maps/lvl1.FMP"))
        MapSetPal8();

    float x = 50, y = 300;

    scroll = create_sub_bitmap(doubleBuffer, 0, 0, SCREEN_W*2, SCREEN_H);
    mario2 = load_video_bitmap("sprites/mario/mariorun2.bmp");

    FrameSource frames2 = FrameSource(mario2, 4);
    
    Animation anim2(&frames2);

    anim2.addFrame(0, 7);

    Sprite sprite2(&anim2);
    sprite2.setPos(x, y);
    sprite2.setSpeed(0, 0);

    int needsRepaint = TRUE;
        
    syncTimer(&timerCounter);
    while(!key[KEY_ESC])
    {   
        if (timerCounter) 
        {
            do 
            {
                if(key[KEY_D])
                {
                    x += 1.5;
                    
                    anim2.addFrame(0, 7);
                    anim2.addFrame(1, 7);
                    anim2.addFrame(2, 7);
                    anim2.addFrame(3, 7);
                }

                clear_keybuf();

                sprite2.setPos(x, y);

                MapDrawBGT (doubleBuffer, 0, 0, 0, 0, 1280, 480);
                sprite2.tick();                                        
                sprite2.draw(doubleBuffer);
                
                --timerCounter;
            } while (timerCounter >0);

            needsRepaint = true;
        }

        if (needsRepaint)
        {       
            needsRepaint = false;
            show();
        }
    }
}


ANIMATIONEN:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
FrameSource::FrameSource(BITMAP *bmp, int cnt) : frames(bmp), count(cnt) {
        if (frames && count > 0) {
            w = frames->w / count;
            h = frames->h;
        }
    }
        
        
// Gibt die bitmap bei explizitem Aufruf
// frei.
void  FrameSource::destroy() {
    if (frames != NULL) {
        destroy_bitmap(frames);
        frames = NULL;
    }
}
        
// Zeigt Frame <index> an der Position (x,y) an
void FrameSource::render(BITMAP *dst, int index, int x, int y) {
    masked_blit(frames, dst, w * index, 0, x, y, w, h);    
}
// Ausmasse eines Frames
int FrameSource::getWidth() {
    return w;
}

int FrameSource::getHeight() {
    return h;
}

Animation::Animation(FrameSource *src) : source(src), ticks(0), curFrame(0), count(0) {
}

Animation::~Animation() {
}


void Animation::addFrame(int index, int delay, int dx, int dy) {
    FrameInfo info(index, delay, dx, dy);            
    addFrame(info);
    ++count;
}

        
void Animation::addFrame(FrameInfo &info) 
{
    frames.push_back(info);            
}
        
void Animation::tick() 
{            
    if (count >0) {
        ++ticks;
        if (ticks >= frames[curFrame].delay) {
            ++curFrame;
            if (curFrame >= count) {
                curFrame = 0;
            }
            ticks = 0;
        }                    
    }
}

void Animation::render(BITMAP *dst, int x, int y) {    
    source->render(dst,frames[curFrame].frame, x + frames[curFrame].dx, y + frames[curFrame].dy);
}

FrameSource* Animation::getFrameSource() {
    return source;
}


SPRITES:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef SPRITES_HEADER
#define SPRITES_HEADER

#include <allegro.h>
#include <list>
#include "util.h"
#include "animation.h"

using namespace std;

struct Sprite {
    float x, y, dx, dy;
    Animation *anim;
    
    Sprite(Animation *ani) {
        anim = ani;
    }
    
    void draw(BITMAP *dest) {
        anim->render(dest, (int) x, (int) y);
    }
    
    void setPos(float x, float y) {
        this->x = x;
        this->y = y;
    }
    
    void setSpeed(float dx, float dy) {
        this->dx = dx;
        this->dy = dy;
    }
    
    virtual void tick() {
        x += dx;
        y += dy;
        
        if (x > SCREEN_W) {
            x = -anim->getFrameSource()->getWidth();
        }
        if (y > SCREEN_H) {
            y = -anim->getFrameSource()->getHeight();
        }
        anim->tick();
    }
};


typedef list<Sprite*>         SpriteList;
typedef SpriteList::iterator  SpriteIterator;

BITMAP* load_video_bitmap(const char* name) {
    BITMAP* bmp = load_bitmap(name, NULL);
    if (!bmp) {
        return NULL;
    }
    BITMAP *vid = create_video_bitmap(bmp->w, bmp->h);
    if (vid) {
        blit(bmp, vid, 0, 0, 0, 0, bmp->w, bmp->h);
        destroy_bitmap(bmp);
        return vid;
    }
    return bmp;
}
#endif


Ich hoffe ihr könnt mir helfen.

MfG DarkRaider.

Fred

Supermoderator

Beiträge: 2 121

Beruf: Softwareentwickler

  • Private Nachricht senden

2

14.05.2006, 19:25

Also ich finde den Code etwas undurchsichtig. man weiß nicht genau wie was funktioniert.

Anonymous

unregistriert

3

15.05.2006, 21:58

okay, hat sich erledigt, doch jetzt habe ich ein anderes problem. ich will ganz einfach, dass mario springt, dafür habe ich diese abfrage geschrieben:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
            if((getKeyState2(KEY_W) == KEY_PRESSED) && (getKeyState(KEY_D) != KEY_PRESSED))
                {
                    y2 = y;
                    y2 -= 30;

                    while(y > y2)
                    {
                        masked_blit(mariojump, doubleBuffer, 0, 0, x, y, mariojump->w, mariojump->h);
                        y -= 1;
                    }
                }


Das Problem ist halt, dass mario viel zu schnell an der gewünschten stelle ist und mit der Sleep ähnlichen funktion "rest" hat es auch nicht geklappt. Mit timern habe ich leider noch nicht so viele erfahrungen, aber so schwer kann es doch nicht sein...

mfg darkraider.

CW_Kovok

Alter Hase

Beiträge: 836

Wohnort: nähe Bonn

Beruf: Schüler

  • Private Nachricht senden

4

17.05.2006, 12:06

dann zieh doh nich immer 1 ab, sonder 0.001 oder so und wenn es integer sind bau einfach noxch eine Schleife ein
Was es alles gibt, das ich nich brauche - Aristoteles

Anonymous

unregistriert

5

17.05.2006, 12:09

ja ich habs schon mit 0.000001 probiert, ist genauso schnell. Mein Kollege kümmert sich jetzt aber drum. Trotzdem danke.

MfG DarkRaider.

Nox

Supermoderator

Beiträge: 5 272

Beruf: Student

  • Private Nachricht senden

6

17.05.2006, 12:41

Also sowas in Zeiten von FPS Angaben ist schon eine rechte Vergewaltigung ;)

Man multipliziert die Geschwindigkeit einer Bewegung einfach mit der Zeit die er pro Durchgang braucht. Wie man das jetzt genau auf dein Beispiel anwenden kann, müsst man noch überlegen.
PRO Lernkurs "Wie benutze ich eine Doku richtig"!
CONTRA lasst mal die anderen machen!
networklibbenc - Netzwerklibs im Vergleich | syncsys - Netzwerk lib (MMO-ready) | Schleichfahrt Remake | Firegalaxy | Sammelsurium rund um FPGA&Co.

Werbeanzeige