Is there an alternative?

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Conflictus
Posts: 53
Joined: January 7th, 2013, 8:09 am

Is there an alternative?

Post by Conflictus » February 3rd, 2013, 2:57 am

I'm trying to work around the local scope of a function. I want to increment the loop each time the function is called without it re-initializing. I wrote a simple console app to try and come up with a solution. It appears to work as intended for the most part, but i'm having some difficulty incorporating the idea into my original program. I was wondering what else I might try that would provide a similar solution.

Code: Select all

#include <stdio.h>
#include <conio.h>

static int index = 1;

bool function();

int main()
{	
	while( _getch() )
	{
		function();
		while( _getch() )
		{		
			index++;
			break;
		}
	}
return 0;
}

bool function()
{			
	for( ;index <= 10; )
	{	
		printf( "%d",index );		
		return true;			
	}
}

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: Is there an alternative?

Post by indus » February 3rd, 2013, 9:31 am

I dont really understand what are you trying to do. But I'll point out some error in the given code.
First of all your function is a bool. This means that you have to return a bool value on exit. If your index gets greater than 10 you dont enter the for loop and dont return anything from the function.
Since I dont really see any reason for returning anything in the function at all you could just make it void and remove the return true line.
The other thing is the for loop. Why for loop?. All you do is check if index <=10. You dont have any kind of looping there. Its a simple if-check statement.

Conflictus
Posts: 53
Joined: January 7th, 2013, 8:09 am

Re: Is there an alternative?

Post by Conflictus » February 3rd, 2013, 1:51 pm

I was attempting collision on an array of tiles. Prior to trying to find a solution the initial problem was that it was only checking collision with the first element in the array. I assume this is because the index of the array is limited to the scope of the collision function.

With the way it is now it's not detecting collision with anything and the moment I hit any of the window borders the directions get inverted and it starts moving at a much faster rate. If I happen to hit the window borders again it crashes.

I've included my project in hopes it clarifies what i'm trying to accomplish.

Edited:

I accidentally mentioned symptoms of an earlier test. Instead, what happens is there is initially no collision with any of the tiles. Upon hitting any of the window borders the tile at 0,0 will be the only one to detect collision and if I happen to occupy the same space when hitting the window border i'm forced just outside of the tile. The movement keys aren't inverted and at first glance the window borders may appear to be working, but if you hold down the keys for approximately 20-30 seconds or so while against any of the borders it'll crash.
Attachments
Terrain.zip
(538.08 KiB) Downloaded 153 times

User avatar
LuX
Posts: 1492
Joined: April 22nd, 2012, 12:33 pm
Location: Finland

Re: Is there an alternative?

Post by LuX » February 3rd, 2013, 7:52 pm

Ok... so first off I don't see you declaring the character position so I just put it to ( 100, 100 ).
Secondly I don't really understand what you were attempting with that infinite for loop. It checks the first element, alright.

If you want to loop through all the tiles use "for( int n = 0; n < TERRAIN_ARRAY; n ++ )" and replace all the "terrain[gIndex]" with "terrain[n]"

And you should only check for true inside the loop and have return false outside the loop.
for( int n = 0; n < TERRAIN_ARRAY; n ++ )
{
if( character.x + CHAR_W > terrain[n].x && character.x < terrain[n].x + TERRAIN_W &&
character.y + CHAR_H > terrain[n].y && character.y < terrain[n].y + TERRAIN_H
|| character.x < 0 || character.y < 0 || character.x > (SCREEN_WIDTH - CHAR_W) || (character.y > SCREEN_HEIGHT - CHAR_H) )
return true; // collision found atleast once; no point on continuing
}
return false; // no collision found in the loop of all elements
ʕ •ᴥ•ʔ

Conflictus
Posts: 53
Joined: January 7th, 2013, 8:09 am

Re: Is there an alternative?

Post by Conflictus » February 4th, 2013, 2:55 am

Thanks LuX! That worked out perfectly. :D

Post Reply