Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Don't forget to read Tuesday's email newsletter for your chance to win a free copy of Construct 2!


SDL Surface not appearing


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 nano511   Members   -  Reputation: 103

Like
0Likes
Like

Posted 04 August 2011 - 07:34 AM

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


Sponsor:

#2 Wooh   Members   -  Reputation: 637

Like
0Likes
Like

Posted 04 August 2011 - 07:59 AM

In the constructor you have
int 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.

#3 rip-off   Moderators   -  Reputation: 8518

Like
0Likes
Like

Posted 04 August 2011 - 08:41 AM

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.

#4 nano511   Members   -  Reputation: 103

Like
0Likes
Like

Posted 04 August 2011 - 08:45 AM

In the constructor you have

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




#5 rip-off   Moderators   -  Reputation: 8518

Like
1Likes
Like

Posted 04 August 2011 - 08:47 AM

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;


#6 nano511   Members   -  Reputation: 103

Like
0Likes
Like

Posted 04 August 2011 - 09:14 AM

XD how could i not notice that lol!

But he's still not showing up. :(

#7 rip-off   Moderators   -  Reputation: 8518

Like
0Likes
Like

Posted 04 August 2011 - 09:40 AM

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?

#8 nano511   Members   -  Reputation: 103

Like
0Likes
Like

Posted 04 August 2011 - 09:47 AM

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!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS