PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

initialising an object with parameters

My Sprite class contains an array of Rects dependant on the number of frames in the Sprite. I can get the array defined by declaring it in the header as:-
Rect *FrameArray and then initialising it in the function which also loads the image:-
FrameArray = new Rect[_f] ;
While this works what I wanted to do was set up the frame total in the constructor but my attempts to define a Sprite as a private variable at the bottom of MyGame kept failing with a 'constant' error so I just create a Sprite using an empty constructor and this works.

The question is how do I create an object with parameters available to the rest of the game code ( or is this not possible ). I don't see any examples anywhere of this happening.
BurningSheep · 28 Jun 2002, 14:30 UTC
I'm not sure if this is what you mean, but you can pass parameters to the constructor like this:

MyObject obj(12, "Hello");

or

MyObject * obj;
obj = new MyObject(12, "Hello");

assuming the constructor of the MyObject class looks something like this:

MyObject::MyObject(int x, char * text)
{
// init stuff
}
Is this what you were trying to do?

Code:
class MyGame : public Game
{
public:
//Constructors and Destructors
MyGame();
virtual ~MyGame();

//Overrides from class Game
virtual void GameEnd();
virtual void GameLoop();
virtual bool GameInit();

private:
SpriteClass m_MySprite(10); //Initialize sprite with number of frames

}


In that case, no I'm pretty sure you can't do that. At least I've never been able to.

Here's what I do:

Code:
class MyGame : public Game
{
public:
//Constructors and Destructors
MyGame();
virtual ~MyGame();

//Overrides from class Game
virtual void GameEnd();
virtual void GameLoop();
virtual bool GameInit();

private:
SpriteClass* m_MySprite;

}


Then, in the MyGame constructor, or in GameInit(), you can call the constructor:

Code:
m_MySprite = new SpriteClass(10); //Initialize sprite with number of frames


Pam
Conan · 28 Jun 2002, 19:34 UTC — C++ can be so cumbersome
Thanks Pam + Burning Sheep
Im doing similar to you Pam. It just seems odd that there's no way to define a class object with parameters inside 'PF'
I could cheat & make the first charater or two of the image name = the frame count & chop this out into a number at load time.

Docs wise I need to find out how much support for loading files from disk into a surface there is. If it wasn't for the Plasma demo I would not have realised that you could lead a .raw file for instance. Storing some big files on the PocketPC will cut down on download time during development.

The plasma demo is very interesting. I need my wife who is a maths teacher to explain the math. I need to work up some light based effects like lens flares & nebula for my game so I will be scouring the web for samples but unfortunately nowadays so much is based around OpenGL & DirectX. I also need some 3D so I'm looking at either embedding very basic support cribbed from the iPaq demo Jacco did or the complex option of merging PocketGL into PocketFrog. I think the linker would have a fit somehow.

Good weekend to all