Jump to content
  • Advertisement
Sign in to follow this  
In_Yack_Mode

Trying to use functions

This topic is 3491 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 have never used functions I built so I decided to try and build some I could use later. I tried to make one that would check if the arrow keys were pressed and if they were move the man. Can that be done with a function? I tried making it and putting it in an include file. Here is my attempt: Source file:
#include "movement.h" // it being included 
(...) void check_keys();// its declaration
(...) check_keys(); // it being called
Header
int man_x = 0; 
int man_y = 0; 
void check_keys();
void check_keys()
    {
        if(key[KEY_RIGHT])			
                        {
				man_x ++;			
			}
        if(key[KEY_LEFT])
			{
				man_x --;
			} 
	if(key[KEY_UP])
			{
				man_y --;
			}
	if(key[KEY_DOWN])
			{
				man_y ++;				
			}   
    }END_OF_FUNCTION(check_keys); 
It compiles fine but does nothing when I try pressing the arrow keys. Do you notice what might me wrong?

Share this post


Link to post
Share on other sites
Advertisement
I'm assuming key[] is an array to store the state of all keys, are you updateing these states every iteration of the main loop? or even updateing them at all?

are you only calling check_keys() once, then the app closes?

do you have a main loop that keeps the app running?

more info on how you have your app setup up to run?

Share this post


Link to post
Share on other sites
My main loop is something like this: while(!key[KEY_ESC]){(...)}
and about the keys sorry I forgot to mention I am using Allegro.
I am calling the function where I had the code to respond to movement before.

Share this post


Link to post
Share on other sites
Does your IDE have a debugger, might be helpful to watch the man_x value and see how it changes if it does change by stepping through with the debugger.

How are you using the man_x and man_y values?

Share this post


Link to post
Share on other sites
Quote:

I am calling the function where I had the code to respond to movement before.


also can you explain that a little more? what function? what code? the code posted above?

Share this post


Link to post
Share on other sites
this code is now replaced with check_keys()
 if(key[KEY_RIGHT])			
{
man_x ++;
}
if(key[KEY_LEFT])
{
man_x --;
}
if(key[KEY_UP])
{
man_y --;
}
if(key[KEY_DOWN])
{
man_y ++;
}



I have man_x and man_y set in the sprite drawing function like this: draw_sprite(buffer, man, man_x, man_y);

Share this post


Link to post
Share on other sites
when you run the app, is the sprite always drawn at 0,0 ?

is man_x and man_y in the same scope everywhere you're using it?

is the code where these variables are being used long? if not too long try posting it or atleast the code where the variables are declared, initialized, where the draw_sprite call is used?

can you display font to the screen? try displaying the values of man_x and man_y on the screen so you can see they're values at real time.

Share this post


Link to post
Share on other sites
Yes the sprite is always drawn at 0,0. man_x and _y are both called at the beginning of the program. The code where it's used isn't too long but it's really only used in the snippet I posted a minute ago. Here is where it is defined and set: int man_x = 0; // Holds our pictures X coorinate
int man_y = 0; // Holds our picture's Y coordinate

Share this post


Link to post
Share on other sites
I think whats happening is you have two different man_x's and Man_y's, in different scopes.

ie.

in "movment.h" you declare them and you're probably declareing them in annother file and useing those one to place your sprite.

I'm not sure wich language you're using exactly, but in C/C++ you'd do something like. In movement.h declare the man_x and man_y values like so

extern int man_x;
extern int man_y;

that will make them global.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!