Connecting Tech Pros Worldwide Forums | Help | Site Map

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

todd_montana@hotmail.com
Guest
 
Posts: n/a
#1: Mar 25 '06
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


Phlip
Guest
 
Posts: n/a
#2: Mar 25 '06

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


todd_montana wrote:[color=blue]
>
> 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;[/color]

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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!


Ian Collins
Guest
 
Posts: n/a
#3: Mar 25 '06

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


todd_montana@hotmail.com wrote:[color=blue]
> 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.[/color]

Post an example!

--
Ian Collins.
todd_montana@hotmail.com
Guest
 
Posts: n/a
#4: Mar 25 '06

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


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...
}
}

Ian Collins
Guest
 
Posts: n/a
#5: Mar 25 '06

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


todd_montana@hotmail.com wrote:[color=blue]
> ok thanks for the info, this is my first post.[/color]

Have a read of <http://cfaj.freeshell.org/google/> if you are using google.
[color=blue]
> 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[/color]

I expect this should be

static int frame;

Somewhere in your code you will have:

int CAnimation::frame = 0;
[color=blue]
> sprite
> char Sprites[2][25] //Array to hold image
> filenames
>
> public:
> CAnimation( ); //Constructor[/color]

Avoid self evident comments, they add nothing.
[color=blue]
> };
>
> CAnimation( )::CAnimation( )
> {
> char Sprites[2][25] = {"image1.tga", "image2.tga"};
> frame = 0;[/color]
remove this line;
[color=blue]
> frame += 1;[/color]

Or just ++frame;

--
Ian Collins.
todd_montana@hotmail.com
Guest
 
Posts: n/a
#6: Mar 27 '06

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


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

Puppet_Sock
Guest
 
Posts: n/a
#7: Mar 27 '06

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


todd_montana@hotmail.com wrote:[color=blue]
> Thanks for your post Ian, I tried the modifications that you mentioned
> but it still does not increment properly, Any more ideas?[/color]

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

todd_montana@hotmail.com
Guest
 
Posts: n/a
#8: Mar 27 '06

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


todd_montana@hotmail.com wrote:[color=blue]
> 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...
> }
> }[/color]

Ian Collins
Guest
 
Posts: n/a
#9: Mar 27 '06

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


todd_montana@hotmail.com wrote:[color=blue]
> Thanks for your post Ian, I tried the modifications that you mentioned
> but it still does not increment properly, Any more ideas?
>[/color]
Care to elaborate, with code?

Please quote context in your reply.

--
Ian Collins.
todd_montana@hotmail.com
Guest
 
Posts: n/a
#10: Mar 27 '06

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



Ian Collins wrote:[color=blue]
> todd_montana@hotmail.com wrote:[color=green]
> > Thanks for your post Ian, I tried the modifications that you mentioned
> > but it still does not increment properly, Any more ideas?
> >[/color]
> Care to elaborate, with code?
>
> Please quote context in your reply.
>
> --
> Ian Collins.[/color]

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.
[color=blue]
> private:
> static int frame;
> char Sprites[2][25]
> public:
> CAnimation( );[/color]

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

Marcus Kwok
Guest
 
Posts: n/a
#11: Mar 28 '06

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


todd_montana@hotmail.com <todd_montana@hotmail.com> wrote:[color=blue]
> 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.
>[color=green]
>> private:
>> static int frame;
>> char Sprites[2][25]
>> public:
>> CAnimation( );[/color]
>
> CAnimation( )::CAnimation( )
> {
> char Sprites[4][25] = {"image1.tga", "image2.tga", "image3.tga",
> "image4.tga};
> frame = 0;[/color]

Here, you always reset frame to 0 before incrementing it, so you always
get 1.
[color=blue]
> ++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
>[/color]

--
Marcus Kwok
Closed Thread