Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (WinAPI, Konsole) » Node List Warum funktioniert das nicht?

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
24.06.2013, 18:40 Uhr
DuffCola



Warum zum Teufel gibt der in der GameLoop immer nur einmal GameObject Update! und GameObject Render! aus, obwohl ich doch 4 GameObjecte hinzugefügt habe?


C++:
#include <iostream>
using namespace std;

class GameObject
{
public:
    GameObject *nextGameObject;

    GameObject()
    {
        cout<<"GameObject Constructor!\n";
        nextGameObject = nullptr;
    }
    ~GameObject()
    {
        cout<<"GameObject Destructor\n";
        if(nextGameObject != nullptr)
        {
            delete nextGameObject;
        }
    }

    virtual void Update()
    {
        cout<<"GameObject Update!\n";
    }
    virtual void Render()
    {
        cout<<"GameObject Render!\n";
    }
};

class GameObjectManager
{
private:
    GameObject *firstGameObject;
public:
    GameObjectManager()
    {
        firstGameObject = nullptr;
    }
    ~GameObjectManager()
    {
        if(firstGameObject != nullptr)
        {
            delete firstGameObject;
        }
    }

    void Update()
    {
        if(firstGameObject != nullptr)
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject->Update();
                helpGameObject = helpGameObject->nextGameObject;
            }
        }
    }
    void Render()
    {
        if(firstGameObject != nullptr)
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject->Render();
                helpGameObject = helpGameObject->nextGameObject;
            }
        }
    }

    void Add(GameObject *newGameObject)
    {
        if(firstGameObject == nullptr)
        {
            firstGameObject = newGameObject;
        }
        else
        {
            GameObject *helpGameObject = firstGameObject;
            while(helpGameObject != nullptr)
            {
                helpGameObject = helpGameObject->nextGameObject;
            }
            helpGameObject = newGameObject;
        }
    }
};

int main()
{
    GameObjectManager gom;
    bool run = true;

    gom.Add(new GameObject);
    gom.Add(new GameObject);
    gom.Add(new GameObject);
    gom.Add(new GameObject);

    while(run)
    {
        cout<<"GameLoop Start\n";
        gom.Update();
        gom.Render();
        cout<<"GameLoop End\n";
        cin.get();
    }

    return 0;
}



---

Edit: cpp-Tags korrigiert; klein geschrieben.

Dieser Post wurde am 25.06.2013 um 21:38 Uhr von Hans editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
25.06.2013, 10:38 Uhr
Tommix



'Morgen,
Du weist nextGameObject nie einen Wert zu, sondern helpGameObject (was aber nur eine Kopie ist).

Gruss, Tommix
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (WinAPI, Konsole) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: