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

SwatzZ

Treue Seele

  • »SwatzZ« ist der Autor dieses Themas

Beiträge: 119

Beruf: Gymnasist (10. Klasse)

  • Private Nachricht senden

11

13.05.2013, 15:31

Je nachdem. Mache ich es richtig, wenn ich backgroundmusic.stop(); vor return 0; mache? Also so:

C-/C++-Quelltext

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
    ////////////////////////////////Images and Sounds////////////////////////////////////
    sf::RenderWindow Window(sf::VideoMode(800,600), "Pokémon 2.0", sf::Style::Close);
    /////Sprite/////
    enum direction {Down, Left, Right, Up};
    sf::Vector2i source(1, Down);

    sf::Vector2i gsource(1, Down);

    sf::Texture tplayer;
    tplayer.loadFromFile("Sprites/player.png");
    sf::Sprite player(tplayer);
    player.setPosition(360,250);
    player.setOrigin(16,16);

    sf::Texture tghost;
    tghost.loadFromFile("Sprites/Ghost.png");
    sf::Sprite ghost(tghost);
    ghost.setPosition(417,15);

    sf::Texture tbackground;
    tbackground.loadFromFile("Sprites/Town.png");
    sf::Sprite background(tbackground);
    background.setScale(2.087,1.873);
    background.setPosition(400,300);
    background.setOrigin((tbackground.getSize().x / 2),(tbackground.getSize().y / 2));

    /////Music and Sound/////
    sf::Music backgroundmusic;
    backgroundmusic.openFromFile("Music and Sound/Pokemon_Theme.ogg");
    backgroundmusic.setVolume(50);
    backgroundmusic.setLoop(true);
    backgroundmusic.play();

    //Zeit
    sf::Clock clock;
    sf::Time time = clock.restart();
    sf::Clock animationclockplayer;
    sf::Time animationtimeplayer = animationclockplayer.restart();
    sf::Time animationtime2 = sf::milliseconds(200);
    sf::Clock animationclockghost;
    sf::Time animationtimeghost = animationclockghost.restart();
    sf::Time animationtime3 = sf::milliseconds(100);



    ////////////////////////////////Runtime-Loop////////////////////////////////////
    while(Window.isOpen())
    {
        //time
        animationtimeplayer = animationclockplayer.getElapsedTime();
        animationtimeghost = animationclockghost.getElapsedTime();
        time = clock.getElapsedTime();
        //Event-Loop
        sf::Event Event;
        while (Window.pollEvent(Event))
        {
            switch (Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                backgroundmusic.stop();
                break;
            case sf::Event::KeyPressed:
                if (Event.key.code == sf::Keyboard::Escape)
                {
                    Window.close();
                    backgroundmusic.stop();
                }

                break;
            }
        }
        
        /////Controls player/////
        if (sf::Keyboard::isKeyPressed)
            clock.restart();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            source.y = Down;
            player.move(0,0.1 * time.asMilliseconds());
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            source.y = Left;
            player.move(-0.1 * time.asMilliseconds(),0);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            source.y = Right;
            player.move(0.1 * time.asMilliseconds(),0);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            source.y = Up;
            player.move(0,-0.1 * time.asMilliseconds());
        }
        
        /////Kollisionen/////
        //outline
        if (player.getPosition().y < 46) //down
            player.move(0,0.1 * time.asMilliseconds());
        else if (player.getPosition().x > 725) //left
            player.move(-0.1 * time.asMilliseconds(),0);
        else if (player.getPosition().x < 75) //right
            player.move(0.1 * time.asMilliseconds(),0);
        else if (player.getPosition().y > 555) //up
            player.move(0,-0.1 * time.asMilliseconds());

        ////////////////////////////////Displaying////////////////////////////////////
        
        //Animation player
        if (animationtimeplayer > animationtime2)
        {
        source.x++;
        animationclockplayer.restart();
        }
        if(source.x * 32 >= tplayer.getSize().x)
            source.x = 0;

        //Animation ghost
        if (animationtimeghost > animationtime3)
        {
        gsource.x++;
        animationclockghost.restart();
        if(gsource.x * 32 >= tghost.getSize().x)
            gsource.x = 0;
        }

        /////Updating and Drawing/////
        player.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        ghost.setTextureRect(sf::IntRect(gsource.x * 32, gsource.y * 32, 32, 32));
        Window.clear();
        Window.draw(background);
        //Window.draw(ghost);
        Window.draw(player);
        Window.display();
    }
    backgroundmusic.stop();
    return 0;
}


es geht nämlich immer noch nicht.

BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

12

13.05.2013, 15:59

Siehe meinen vorherigen Edit.

Edit:
Du kannst ja mal versuchen die Instanzen dynamisch zu erzeugen und vor dem Ende zu stoppen und manuell zu deleten.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

SwatzZ

Treue Seele

  • »SwatzZ« ist der Autor dieses Themas

Beiträge: 119

Beruf: Gymnasist (10. Klasse)

  • Private Nachricht senden

13

13.05.2013, 16:30

Ich fühle mich jetzt ehrlich dumm, sowas zu fragen, aber wie erzeuge ich Instanzen dynamisch? Und wie lösche ich sie dann?

Schorsch

Supermoderator

Beiträge: 5 145

Wohnort: Wickede

Beruf: Softwareentwickler

  • Private Nachricht senden

14

13.05.2013, 16:33

C-/C++-Quelltext

1
2
3
4
5
int *p = new int;

// mach irgendwas mit p

delete p;


Wenn du das hier wirklich noch nicht kanntest dann solltest du dringend zu einem Grundlagenbuch greifen. Wenn du es kanntest und nur mit dem Begriff nichts anfangen konntest, dann ist gut;)
„Es ist doch so. Zwei und zwei macht irgendwas, und vier und vier macht irgendwas. Leider nicht dasselbe, dann wär's leicht.
Das ist aber auch schon höhere Mathematik.“

SwatzZ

Treue Seele

  • »SwatzZ« ist der Autor dieses Themas

Beiträge: 119

Beruf: Gymnasist (10. Klasse)

  • Private Nachricht senden

15

13.05.2013, 16:36

Gut, dann versuche ich es mit Zeigern. Ich melde mich dann nochmal. Und ich habe mir den SFML-Thread angeschaut.

SwatzZ

Treue Seele

  • »SwatzZ« ist der Autor dieses Themas

Beiträge: 119

Beruf: Gymnasist (10. Klasse)

  • Private Nachricht senden

16

13.05.2013, 17:10

Es funktioniert immer noch nicht.

C-/C++-Quelltext

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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
int main()
{
    ////////////////////////////////Images and Sounds////////////////////////////////////
    sf::RenderWindow Window(sf::VideoMode(800,600), "Pokémon 2.0", sf::Style::Close);
    
    /////Sprites/////
    enum direction {Down, Left, Right, Up};
    sf::Vector2i source(1, Down);

    sf::Vector2i gsource(1, Down);

    sf::Texture *tplayer = new sf::Texture;
    tplayer->loadFromFile("Sprites/player.png");
    sf::Sprite *player = new sf::Sprite;
    player->setTexture(*tplayer);
    player->setPosition(360,250);
    player->setOrigin(16,16);

    sf::Texture *tghost = new sf::Texture;
    tghost->loadFromFile("Sprites/Ghost.png");
    sf::Sprite *ghost = new sf::Sprite;
    ghost->setTexture(*tghost);
    ghost->setPosition(417,15);

    sf::Texture *tbackground = new sf::Texture;
    tbackground->loadFromFile("Sprites/Town.png");
    sf::Sprite *background = new sf::Sprite;
    background->setTexture(*tbackground);
    background->setScale(2.087,1.873);
    background->setPosition(400,300);
    background->setOrigin((tbackground->getSize().x / 2),(tbackground->getSize().y / 2));

    /////Music and Sound/////
    sf::Music *backgroundmusic = new sf::Music;
    backgroundmusic->openFromFile("Music and Sound/Pokemon_Theme.ogg");
    backgroundmusic->setVolume(50);
    backgroundmusic->setLoop(true);
    backgroundmusic->play();

    //Zeit
    sf::Clock *clock = new sf::Clock;
    sf::Time *time = new sf::Time;
    *time = clock->restart();
    sf::Clock *animationclockplayer = new sf::Clock;
    sf::Time *animationtimeplayer = new sf::Time;
    *animationtimeplayer = animationclockplayer->restart();
    sf::Time *animationtime2 = new sf::Time;
    *animationtime2 = sf::milliseconds(200);
    sf::Clock *animationclockghost = new sf::Clock;
    sf::Time *animationtimeghost = new sf::Time;
    *animationtimeghost = animationclockghost->restart();
    sf::Time *animationtime3 = new sf::Time;
    *animationtime3 = sf::milliseconds(100);



    ////////////////////////////////Runtime-Loop////////////////////////////////////
    while(Window.isOpen())
    {
        //time
        *animationtimeplayer = animationclockplayer->getElapsedTime();
        *animationtimeghost = animationclockghost->getElapsedTime();
        *time = clock->getElapsedTime();
        //Event-Loop
        sf::Event *Event = new sf::Event;
        while (Window.pollEvent(*Event))
        {
            switch (Event->type)
            {
            case sf::Event::Closed:
                Window.close();
                break;
            case sf::Event::KeyPressed:
                if (Event->key.code == sf::Keyboard::Escape)
                {
                    Window.close();
                }
                break;
            }
        }
        
        /////Controls player/////
        if (sf::Keyboard::isKeyPressed)
            clock->restart();

        if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
        {
            source.y = Down;
            player->move(0,0.1 * time->asMilliseconds());
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
        {
            source.y = Left;
            player->move(-0.1 * time->asMilliseconds(),0);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
        {
            source.y = Right;
            player->move(0.1 * time->asMilliseconds(),0);
        }
        else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
        {
            source.y = Up;
            player->move(0,-0.1 * time->asMilliseconds());
        }
        
        /////Kollisionen/////
        //outline
        if (player->getPosition().y < 46) //down
            player->move(0,0.1 * time->asMilliseconds());
        else if (player->getPosition().x > 725) //left
            player->move(-0.1 * time->asMilliseconds(),0);
        else if (player->getPosition().x < 75) //right
            player->move(0.1 * time->asMilliseconds(),0);
        else if (player->getPosition().y > 555) //up
            player->move(0,-0.1 * time->asMilliseconds());

        ////////////////////////////////Displaying////////////////////////////////////
        
        //Animation player
        if (animationtimeplayer > animationtime2)
        {
        source.x++;
        animationclockplayer->restart();
        }
        if(source.x * 32 >= tplayer->getSize().x)
            source.x = 0;

        //Animation ghost
        if (animationtimeghost > animationtime3)
        {
        gsource.x++;
        animationclockghost->restart();
        if(gsource.x * 32 >= tghost->getSize().x)
            gsource.x = 0;
        }

        /////Updating and Drawing/////
        player->setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
        ghost->setTextureRect(sf::IntRect(gsource.x * 32, gsource.y * 32, 32, 32));
        Window.clear();
        Window.draw(*background);
        //Window.draw(ghost);
        Window.draw(*player);
        Window.display();
    }
    backgroundmusic->stop();
    delete clock;
    delete time;
    delete animationclockplayer;
    delete animationtimeplayer;
    delete animationclockghost;
    delete animationtimeghost;
    delete animationtime2;
    delete animationtime3;
    delete tplayer;
    delete player;
    delete tghost;
    delete ghost;
    delete tbackground;
    delete background;
    delete backgroundmusic;
    return 0;
}



Ich hab Zeiger zwar jetzt im Blut, auf der anderen Seite kann ich den Mist aber wohl wieder beseitigen -_- :D

/Edit:

Ich nutzte die Gelegenheit mal, um zu fragen: Welche Vor-/Nachteile hat dieser Code mit den Zeigern gegenüber dem vorherigen?

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »SwatzZ« (13.05.2013, 17:31)


BlueCobold

Community-Fossil

Beiträge: 10 738

Beruf: Teamleiter Mobile Applikationen & Senior Software Engineer

  • Private Nachricht senden

17

13.05.2013, 17:57

Vorteile? In Deinem Fall gar keine. Du solltest außerdem nur die Musik austesten, nicht gleich alles. Wenn's nicht geht -> SFML Forum. Der Link steht ja oben.
Teamleiter von Rickety Racquet (ehemals das "Foren-Projekt") und von Marble Theory

Willkommen auf SPPRO, auch dir wird man zu Unity oder zur Unreal-Engine raten, ganz bestimmt.[/Sarkasmus]

SwatzZ

Treue Seele

  • »SwatzZ« ist der Autor dieses Themas

Beiträge: 119

Beruf: Gymnasist (10. Klasse)

  • Private Nachricht senden

18

13.05.2013, 18:13

Ich hab es vorher erst nur mit der Musik gemacht, aber nichts. Naja, ich lass es einfach. Ich klammer den Code erstmal aus und versuche es mit dem Forum. Danke!

Werbeanzeige