• Advertisement
Sign in to follow this  

SDL Surface not appearing

This topic is 2364 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

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

Here is the relevant code

Monster.cpp

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


void Monster::Move( int playerX )
{
if( playerX < xPos )
{
direction = LEFT;
frame++;
xVel -= 3;
}
else if( playerX > xPos )
{
direction = RIGHT;
frame++;
xVel += 3;
}
}


void Game::HandleEvents()
{
...

player->HandleEvents();
monster->Move( player->xPos );
}


void Game::Logic()
{
...

monster->xPos += monster->xVel;

...
}


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();
}

Share this post


Link to post
Share on other sites
Advertisement
In the constructor you haveint xPos = 490;
int health = 100;
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
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

In the constructor you haveint xPos = 490;
int health = 100;
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.


What do you mean they're local. Those are member variables. Health isnt used anywhere yet, but monster->xPos is 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
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:

xPos = 490;
health = 100;

Share this post


Link to post
Share on other sites
Are you sure the images are loading? Have you used your debugger to step through the render function and inspected the values of the monster's position, etc?

Share this post


Link to post
Share on other sites
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
Sign in to follow this  

  • Advertisement