• Advertisement
Sign in to follow this  

Animation is a blight and I can't figure it out ! help :(

This topic is 2171 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello I'm trying really hard to listen to the advice some people give but I generally don't want to do it a way that requires more annoying irritation and or emotional pain so I've been doing animation in SDL using interger counters like so

[CODE]
int timer = 0;
timer++;

if(timer == 1)
{
FRAME= 1;
}

if(timer == 2)
{
FRAME= 2;
}

if(timer == 3)
{
FRAME= 3;
timer = 0;
}

EnemyRect = StorageRect;
SDL_BlitSurface(Enemysprite[FRAME],NULL,Screen,EnemyRect);
[/CODE]

and the problem is that everytime the code sets the timer to 0 it causes the sprite to dissappear for a few milliseconds and then shows again and repeats

and I have no Idea how to stop it from disappearing

could someone please tell me how to stop this from happening in the simplest way possible that doesn't require irritation


Thanks in Advance

Share this post


Link to post
Share on other sites
Advertisement
I understand your logic, but I don't understand how your code is structured. Is this all code in a single function? Is it the timer declaration somewhere else as a global variable and the code starting from "timer++"in a function?

Share this post


Link to post
Share on other sites
The timer declaration is just a int timer = 0; at the top of the cpp file as simple as that [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] and timer++ is not in a function.

Share this post


Link to post
Share on other sites
Why are you adding +1 to timer if it isn't within a function like main() or update()? It doesn't make sense to update timer without those.

As far as your code goes, I guess you are simply doing this as a test and that is why you aren't doing this in an OOP friendly manner and also why your code isn't structured in any refined way -- so, basically you just loop through all three of the images and then, reaching the final one, reset to the first one? But does it do this multiple times? Or is it just the one time (as you indicated by saying: "timer++ not in a function" meaning nothing of this is in a function being continuously updated)?

Finally, what is the image you are looping through? Where are EnemyRect and StorageRect defined? Lots and lots missing here to take any wild guesses, and I'm too tired honestly to try and figure out what it could be without more info about how the rest of your program is built.

Share this post


Link to post
Share on other sites
The main while loop of the program contains these 3 things

1).logic
2.) draw
3.)update

the rects are declared in cpp files ofc

and timer++; is just how it looks in the code I showed


The Image is a png file loaded onto a sdl_Surface

what more do you wanna know?

Share this post


Link to post
Share on other sites
[quote name='DarkHorseKnight' timestamp='1328745743' post='4911129']
what more do you wanna know?
[/quote]

Post code? For instance, what's stopping you from using this:
int timer = 0;

...

FRAME = timer = (timer + 1) % 4;

Without seeing more than these lines, I can't provide any help; I can't even be sure the problem lies in the lines that you posted.

Share this post


Link to post
Share on other sites
sorry to hear that Ectara but the idea would be to figure out what the common causes of the problem are wouldn't it? I'm no expert so can't really say

but heres the source code

[url="http://pastebin.com/McJdnZRR"]http://pastebin.com/McJdnZRR[/url]

this is just the cpp file where the problem lies

I have changed from using if statements to using switch statements for the enemy1 timer which is where the problem lies [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] which is on line 338 +

Share this post


Link to post
Share on other sites
[quote name='DarkHorseKnight' timestamp='1328733428' post='4911040']
Hello I'm trying really hard to listen to the advice some people give but I generally don't want to do it a way that requires more annoying irritation and or emotional pain so I've been doing animation in SDL using interger counters like so

[CODE]
int timer = 0;
timer++;

if(timer == 1)
{
FRAME= 1;
}

if(timer == 2)
{
FRAME= 2;
}

if(timer == 3)
{
FRAME= 3;
timer = 0;
}

EnemyRect = StorageRect;
SDL_BlitSurface(Enemysprite[FRAME],NULL,Screen,EnemyRect);
[/CODE]

and the problem is that everytime the code sets the timer to 0 it causes the sprite to dissappear for a few milliseconds and then shows again and repeats

and I have no Idea how to stop it from disappearing

could someone please tell me how to stop this from happening in the simplest way possible that doesn't require irritation


Thanks in Advance
[/quote]

Wow, that's not the prettiest code I've ever seen. You need to learn how to use functions and structures, as I suggested before. It will make things much easier for you.

How is OrangeGhostKnightSurf defined? is OrangeGhostKnightSurf[0]...[5] all setup with images?

Share this post


Link to post
Share on other sites
This is how the code looks


[code] //SurfaceStorage.h
SDL_Surface *OrangeGhostKnightSurf[5];


//main.cpp
//////////////////
//Enemy Loading
/////////////////
OrangeGhostKnightSurf[0] = IMG_Load("ArtWork/Enemys/Idle Frame 0.png");
OrangeGhostKnightSurf[1] = IMG_Load("ArtWork/Enemys/Idle Frame 1.png");
OrangeGhostKnightSurf[2]= IMG_Load("ArtWork/Enemys/Idle Frame 2.png");
OrangeGhostKnightSurf[3] = IMG_Load("ArtWork/Enemys/Idle Frame 3.png");
OrangeGhostKnightSurf[4]= IMG_Load("ArtWork/Enemys/Idle Frame 4.png");
OrangeGhostKnightSurf[5] = IMG_Load("ArtWork/Enemys/Idle Frame 5.png");
[/code]

Share this post


Link to post
Share on other sites
SDL_Surface *OrangeGhostKnightSurf[5];


There's your problem. You only allocate 5 Surfaces elements in your array, yet you fill it with 6 (0 - 5 is 6 images). You need to define it with 6 elements:

SDL_Surface *OrangeGhostKnightSurf[6];

That will solve you immediate problem. Your bigger problem is you aren't using functions to break up your code into pieces, and you aren't using structure to group common components and their attributes. You're going to have one massive headache trying to deal with that.

Share this post


Link to post
Share on other sites
I'm going to be using classes/structures and functions in my next game project.

You have inspired me :D and thanks so much you solved my problem I can't believe how strict arrays are but thanks so much I now know to be carefull when using arrays :D

Share this post


Link to post
Share on other sites
[quote name='DarkHorseKnight' timestamp='1328823341' post='4911446']
You have inspired me [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] and thanks so much you solved my problem [u]I can't believe how strict arrays are[/u] but thanks so much I now know to be carefull when using arrays [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
[/quote]

Which is why C++ has std::vectors. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

[code]int timer = 0;
FRAME = timer;

timer++;

if(timer == 3)
{
timer = 0;
}[/code]

The actual reason you never saw the first frame, is because you never actually set FRAME to 0 (at least not in the code you posted).
Further, even if you [i]did[/i] set FRAME to 0, you are also skipping over 'timer' being zero.

This is what you were doing in your original post:
[code]int timer = 0; //Timer starts off at zero.
timer++; //Timer is now 1, skipping zero.

//More code... several loops pass...

if(timer == 3)
{
FRAME= 3;
timer = 0; //Timer is now zero...
}

//Next loop...
timer++; //Timer is now 1, you still skipped zero.[/code]

Share this post


Link to post
Share on other sites
[quote name='Servant of the Lord' timestamp='1328825511' post='4911454']
[quote name='DarkHorseKnight' timestamp='1328823341' post='4911446']
You have inspired me [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] and thanks so much you solved my problem [u]I can't believe how strict arrays are[/u] but thanks so much I now know to be carefull when using arrays [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
[/quote]

Which is why C++ has std::vectors. [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img]

[code]int timer = 0;
FRAME = timer;

timer++;

if(timer == 3)
{
timer = 0;
}[/code]

The actual reason you never saw the first frame, is because you never actually set FRAME to 0 (at least not in the code you posted).
Further, even if you [i]did[/i] set FRAME to 0, you are also skipping over 'timer' being zero.

This is what you were doing in your original post:
[code]int timer = 0; //Timer starts off at zero.
timer++; //Timer is now 1, skipping zero.

//More code... several loops pass...

if(timer == 3)
{
FRAME= 3;
timer = 0; //Timer is now zero...
}

//Next loop...
timer++; //Timer is now 1, you still skipped zero.[/code]
[/quote]

He changed it in his follow up post (see his pastebin code here:[url="http://pastebin.com/McJdnZRR"]http://pastebin.com/McJdnZRR[/url])

I saw that too, but it isn't there in his pastebin code, so i didn't comment on it.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement