Trying to use functions

Started by
16 comments, last by In_Yack_Mode 15 years, 4 months ago
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?
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?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
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.
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?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
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?
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
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);
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.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
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
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.

[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
Ok I did that and now it says:`man_x' has both `extern' and initializer and the same for man_y. What do they mean?

This topic is closed to new replies.

Advertisement