Sign in to follow this  
nano511

SDL Surface not appearing

Recommended Posts

nano511    103
I just made this new class for my "monster". But the monster is not appearing.

Here is the relevant code

Monster.cpp
[code]
Monster::Monster( Environment* GameEnvironment )
{
graphics = GameEnvironment->graphics;
monsterRight[0] = graphics->LoadImage( "Monster_Right_0.bmp" );
monsterRight[1] = graphics->LoadImage( "Monster_Right_1.bmp" );
monsterRight[2] = graphics->LoadImage( "Monster_Right_2.bmp" );
monsterRight[3] = graphics->LoadImage( "Monster_Right_3.bmp" );
monsterLeft[0] = graphics->LoadImage( "Monster_Left_0.bmp" );
monsterLeft[1] = graphics->LoadImage( "Monster_Left_1.bmp" );
monsterLeft[2] = graphics->LoadImage( "Monster_Left_2.bmp" );
monsterLeft[3] = graphics->LoadImage( "Monster_Left_3.bmp" );

direction = LEFT;
frame = 0;
int xPos = 490;
int health = 100;
}
[/code]
[code]
void Monster::Move( int playerX )
{
if( playerX < xPos )
{
direction = LEFT;
frame++;
xVel -= 3;
}
else if( playerX > xPos )
{
direction = RIGHT;
frame++;
xVel += 3;
}
}
[/code]
[code]
void Game::HandleEvents()
{
...

player->HandleEvents();
monster->Move( player->xPos );
}
[/code]
[code]
void Game::Logic()
{
...

monster->xPos += monster->xVel;

...
}
[/code]
[code]
void Game::Render()
{
...

if( monster->frame >= 4 )
monster->frame = 0;

...

// Draw the monster
if( monster->direction == RIGHT )
graphics->ApplySurface( monster->xPos, 311, monster->monsterRight[ monster->frame ], graphics->screen );
else if( monster->direction == LEFT )
graphics->ApplySurface( monster->xPos, 311, monster->monsterLeft[ monster->frame ], graphics->screen );


graphics->UpdateScreen();
}
[/code]

Share this post


Link to post
Share on other sites
Wooh    1088
In the constructor you have[code]int xPos = 490;
int health = 100;
[/code]These are local variables and they are not used anywhere. I think what you meant to do was to set Monster::xPos and Monster::health. I guess this could be what caused the problem because if xPos get a very large value the monster will not be within the screen.

Share this post


Link to post
Share on other sites
rip-off    10979
You also increment (or decrement) the velocity every frame - unless the monster position is exactly equal the player. Instead, you should use either an acceleration value (which should probably be limited to some reasonable maximum) or instead simply set the velocity, instead of incrementing it.

Share this post


Link to post
Share on other sites
nano511    103
[quote name='Wooh' timestamp='1312466354' post='4844485']
In the constructor you have[code]int xPos = 490;
int health = 100;
[/code]These are local variables and they are not used anywhere. I think what you meant to do was to set Monster::xPos and Monster::health. I guess this could be what caused the problem because if xPos get a very large value the monster will not be within the screen.
[/quote]

What do you mean they're local. Those are member variables. Health isnt used anywhere yet, but monster->xPos [i]is[/i] used in the code. The number is so large because i want him to start off the screen, then walk over to the player.

And thank you to the guy who said about setting the velocity instead of incrementing it.


Share this post


Link to post
Share on other sites
rip-off    10979
Because you used the "int" keyword, you are creating new, unused variables and giving them values. If you want to assign to your member variables, instead write:
[code]
xPos = 490;
health = 100;
[/code]

Share this post


Link to post
Share on other sites
nano511    103
Yeah i figured it out. I keep my images in my Media folder, and i forgot to prefix all the images with Media\.

Thanks for the help though!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this