Simple question about using += (sorry about posting in the wrong group)

T

todd_montana

Hi there,

I have a class that has the private member variable called 'int frame'.
I then have a constructor that sets it to 0. Now, inside the
constructor i want to increment it by using: frame += 1;

I am using frame to load images from an array that creates animation.
However, when declaring the above inside the constructor or any member
function for that matter, it doesnt increment properly. It will
increase by one but not continue like it should.

If anyone knows how I can do this it would help a lot.
Thanks
Jay
 
P

Phlip

todd_montana said:
I have a class that has the private member variable called 'int frame'.
I then have a constructor that sets it to 0. Now, inside the
constructor i want to increment it by using: frame += 1;

I suspect you don't understand constructors.

However, I don't know, because you didn't post any code. Please read this
welcome message...

http://www.slack.net/~shiva/welcome.txt

....then post again (without apologies) and with a little code so we can
review it.

We like code.
 
I

Ian Collins

Hi there,

I have a class that has the private member variable called 'int frame'.
I then have a constructor that sets it to 0. Now, inside the
constructor i want to increment it by using: frame += 1;

I am using frame to load images from an array that creates animation.
However, when declaring the above inside the constructor or any member
function for that matter, it doesnt increment properly. It will
increase by one but not continue like it should.

If anyone knows how I can do this it would help a lot.

Post an example!
 
T

todd_montana

ok thanks for the info, this is my first post.
here is the class and constructor. The idea is this: Once I initialize
the variable 'frame'
and the filenames array, I use the constructor to also setup my
texturing and binding. This means that when I create instances of the
class, it will allow me to have multiple sprites that all animate the
same.

NOTE: If I have a variable in the main( ), that is incrememnted like
this: Frame += 1;
And then I pass it as an input parameter to the constructor, the
sprite will animate correctly, but I dont want to pass it in if there
is another way.


class CAnimation
{
private:
int frame; //identifier for current
sprite
char Sprites[2][25] //Array to hold image
filenames

public:
CAnimation( ); //Constructor
};

CAnimation( )::CAnimation( )
{
char Sprites[2][25] = {"image1.tga", "image2.tga"};
frame = 0;
frame += 1;

//The image that will be loaded depends on the frame variable
if (STGAInstance.loadTGA(Sprites[frame]))
{
//Generate and bind Texture using openGL...
}
}
 
I

Ian Collins

ok thanks for the info, this is my first post.

Have a read of said:
here is the class and constructor. The idea is this: Once I initialize
the variable 'frame'
and the filenames array, I use the constructor to also setup my
texturing and binding. This means that when I create instances of the
class, it will allow me to have multiple sprites that all animate the
same.

class CAnimation
{
private:
int frame; //identifier for current

I expect this should be

static int frame;

Somewhere in your code you will have:

int CAnimation::frame = 0;
sprite
char Sprites[2][25] //Array to hold image
filenames

public:
CAnimation( ); //Constructor

Avoid self evident comments, they add nothing.
};

CAnimation( )::CAnimation( )
{
char Sprites[2][25] = {"image1.tga", "image2.tga"};
frame = 0;
remove this line;
frame += 1;

Or just ++frame;
 
T

todd_montana

Thanks for your post Ian, I tried the modifications that you mentioned
but it still does not increment properly, Any more ideas?
 
P

Puppet_Sock

Thanks for your post Ian, I tried the modifications that you mentioned
but it still does not increment properly, Any more ideas?

First, please try to include some context. If you are posting from
google, click the "show options" link and then click the "reply"
link that shows up after that. That way you get quoted text.

Next, in what way "does not incremetn properly?" It's hard to
know what you want it to do, and what it does instead. Specially
since you didn't show the code you used, and didn't say what
you wanted it to do.
Socks
 
T

todd_montana

ok thanks for the info, this is my first post.
here is the class and constructor. The idea is this: Once I initialize
the variable 'frame'
and the filenames array, I use the constructor to also setup my
texturing and binding. This means that when I create instances of the
class, it will allow me to have multiple sprites that all animate the
same.

NOTE: If I have a variable in the main( ), that is incrememnted like
this: Frame += 1;
And then I pass it as an input parameter to the constructor, the
sprite will animate correctly, but I dont want to pass it in if there
is another way.


class CAnimation
{
private:
int frame; //identifier for current
sprite
char Sprites[2][25] //Array to hold image
filenames

public:
CAnimation( ); //Constructor
};

CAnimation( )::CAnimation( )
{
char Sprites[2][25] = {"image1.tga", "image2.tga"};
frame = 0;
frame += 1;

//The image that will be loaded depends on the frame variable
if (STGAInstance.loadTGA(Sprites[frame]))
{
//Generate and bind Texture using openGL...
}
}
 
I

Ian Collins

Thanks for your post Ian, I tried the modifications that you mentioned
but it still does not increment properly, Any more ideas?
Care to elaborate, with code?

Please quote context in your reply.
 
T

todd_montana

Ian said:
Care to elaborate, with code?

Please quote context in your reply.

The outcome is the same as the original way I had coded. The result is
that the second image in the array is loaded. I am after it to run
through all the filenames so as to create the animation. Lets say I add
extra images, it still wont continually increase the variable, frame.
private:
static int frame;
char Sprites[2][25]
public:
CAnimation( );

CAnimation( )::CAnimation( )
{
char Sprites[4][25] = {"image1.tga", "image2.tga", "image3.tga",
"image4.tga};
frame = 0;
++frame;
}
// frame should go, 0,1,2,3,4,etc...
// instead it now equals 1

I tried using a for loop but no joy
Thanks
 
M

Marcus Kwok

The outcome is the same as the original way I had coded. The result is
that the second image in the array is loaded. I am after it to run
through all the filenames so as to create the animation. Lets say I add
extra images, it still wont continually increase the variable, frame.
private:
static int frame;
char Sprites[2][25]
public:
CAnimation( );

CAnimation( )::CAnimation( )
{
char Sprites[4][25] = {"image1.tga", "image2.tga", "image3.tga",
"image4.tga};
frame = 0;

Here, you always reset frame to 0 before incrementing it, so you always
get 1.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top