1. ## Help Beginner

I already have the collision function working fine...now in the collision function i used target[i] (which at the time of testing i set i=0in which loads the image target[0] for my collision detection( I have 6 images target[0],target[1],target[2] etc). But now i want to use the collision function for all my array of images so i was wondering if i could get ("i" //variable) to equal to 0,1,2,3,4,5 which corresponds with all my images target[0],target[1],target[2] etc.So in the end all i will have in my collision function is target[i] which will cover collision for my 6 images.

its driving me nuts

2. you want to use a for-loop

Code:
```for(i = 0; i < 6; i++) {
}```

3. i am not sure...i have been learning on my own so not much experience on how things work..how can i use the loop to get all objects of my target[] array to equal to (i)?

4. the loop i posted will run 6 times - i will be 0 the first time (since it's initialized by i = 0; ) and incremented by 1 every run (i++; )

So you will be able to use target[i] inside of the loop to access all items of your array.

There is a nice tutorial on this site: here

5. int x;

for(x = 0; x < 6; x++) {
i=x;
is this rite?

6. it's missing a } at the end
If "i" is an int too, you would not need to use x - you can use i instead.

7. OK thanks I'll give it a try now

8. Originally Posted by IceBall
it's missing a } at the end
Code:
```If you only have {one command within brackets},
you can optionally leave them out:

for(x = 0; x < 6; x++) i=x;```

9. yeah MK27 thats correct
i was reffering to b-k24's code --> an opening-brace requires always a closing-brace.

10. There was no problem -- I was just mentioning a permissible alternative. Which that maddening avatar should not be. I may have to kill you if you don't come up with something more pleasant

11. still not working

here is my code
am coding for the psp.

Code:
```#include <oslib/oslib.h>
#include <stdio.h>

//Necessary to create eboot
PSP_MODULE_INFO("OSLib Sample", 0, 1, 1);

//declaration of the pointers of our images
OSL_IMAGE *background,*crosshair,*bg,*target[5],*gun,*hud;

//variables
#define RIGHT  0

float j=0,sty,collision=0,collide=0;
int counter,target_choice=0,stretchx,stretchy,width,height;
int gun_march=0,gun_position;
int i;
int p;
void Hud();
void GAME();
void keys();
void target_AI();
void GunAnimate();

bool collisionHS( float targetposX, float targetposY, float crosshairposX, float crosshairposY)
{
int collision,collide=0;
collision = 0;
int *strhx=&stretchx;

target[i]->y=targetposY;
target[i]->x=targetposX;
crosshair->x=crosshairposX;
crosshair->y=crosshairposY;

float targetwidth = 35+*strhx;
float targetheight = sty;
float crosshairwidth = 10;
float crosshairheight = 10;

if ((targetposX + targetwidth > crosshairposX) &&
(targetposX < crosshairposX + crosshairwidth) &&
(targetposY + targetheight+20 > crosshairposY) &&
(targetposY < crosshairposY + crosshairheight))
{
collision = 1; collide=2;
}

return collision;
}

bool collisionBD( float targetposX1, float targetposY1, float crosshairposX1, float crosshairposY1)
{
int collision1;
collision1 = 0;
int *strhx1=&stretchx;

target[i]->y=targetposY1;
target[i]->x=targetposX1;
crosshair->x=crosshairposX1;
crosshair->y=crosshairposY1;

float targetwidth1 = 35+*strhx1;
float targetheight1 = sty;
float crosshairwidth1 = 10;
float crosshairheight1 = 10;

if((targetposX1 + targetwidth1 > crosshairposX1) &&
(targetposX1 < crosshairposX1 + crosshairwidth1) &&
(targetposY1+25 + targetheight1+50 > crosshairposY1) &&
(targetposY1+25 < crosshairposY1 + crosshairheight1)){ collision1 = 1;}

return collision1;
}

int GetRandomNum(int lo, int hi)
{
SceKernelUtilsMt19937Context ctx;
sceKernelUtilsMt19937Init(&ctx, time(NULL)); //SEED TO TIME
u32 rand_val = sceKernelUtilsMt19937UInt(&ctx);
rand_val = lo + rand_val % hi;
return (int)rand_val;
}

void keys(){

//crosshair movement
if(osl_keys->held.right)crosshair->x+=4;
if(osl_keys->held.left)crosshair->x-=4;
if(osl_keys->held.up)crosshair->y-=4;
if(osl_keys->held.down)crosshair->y+=4;
//
if (osl_keys->held.circle){ target[i]->stretchX++;
target[i]->stretchY++;
stretchx++;
stretchy++;
sty+=.80;} //zoom target

if (osl_keys->held.cross){ target[i]->stretchX--;
target[i]->stretchY--;
stretchx--;
stretchy--;

sty-=.80;} //zoom target

}

void GAME(){

///darw images
oslDrawImage(bg);
oslDrawImage(target[0]);
oslDrawImage(target[1]);
oslDrawImage(crosshair);
oslDrawImage(hud);
oslDrawImage(gun);

////keys
keys();
////
GunAnimate();
//
Hud();
//
target_AI();

}

void target_AI(){
//

if (collisionHS( target[i]->x , target[i]->y, crosshair->x, crosshair->y ))oslPrintf_xy(250,60,"headshot");
if (collisionBD( target[i]->x , target[i]->y, crosshair->x, crosshair->y ))oslPrintf_xy(250,70,"bodyshot");

}
void GunAnimate()
{

//Moves the sprite constantly with no static position
oslSetImageTileSize(gun,(gun_march * 36),gun_position,36,60);

//resets the sprite movement in that row
if(crosshair->x>145 && crosshair->x<345 && gun->y>86)gun_march = 0;
if(crosshair->y<86 && crosshair->x<145)gun_march = 1;
if(crosshair->x>345 && crosshair->y<86)gun_march = 2;
if(crosshair->x<145 && crosshair->y>86)gun_march = 3;
if(crosshair->x>345 && crosshair->y>86)gun_march = 4;

}

void Hud(){

//
if(hud->x<0)hud->x+=8;

}

int main(int argc, char **argv)
{
//Initialization of the Oslib library
oslInit(0);

//Initialization of the graphics mode
oslInitGfx(OSL_PF_8888, 1);

oslInitConsole();				//Text

oslSetTransparentColor(RGB(255,0,255));

//positions
crosshair->x =100;
crosshair->y =150;

target[0]->x =200;
target[0]->y =60;
target[1]->x =250;
target[1]->y =120;

gun->x =300;
gun->y =220;
hud->x =-200;
hud->y =237;

//for loop
for(p=0; p < 6; p++ )i=p;

//main while loop
while (!osl_quit)
{
//To be able to draw on the screen
oslStartDrawing();

//Clear the screen
oslCls();

//initiate the PSP's buttons
///
GAME();

//Ends drawing mode
oslEndDrawing();

//Synchronizes the screen
oslSyncFrame();
}

//Terminate the program
oslEndGfx();
oslQuit();
return 0;
}```
I tried this and the collision only worked for one of my targets(target[1])and not target[0]

//for loop
for(p=0; p < 2; p++ ){
if(p==0)i=0;
if(p==1)i=1;
}

12. your problem - using of too many global vars

get rid of them and pass the data the function should work with as a parameters

13. You're going to want i and p to be local variables, so you can use them freely inside any function.

Code:
```  //for loop
for(p=0; p < 2; p++ ) {
if(p==0)i=0;
if(p==1)i=1;
}```
It seems like the above is doing this:

Code:
```   //for loop
for(p=0; p < 2; p++ ) {
i = p;
}```
But since there is no other code in that for loop, it just results in:

Code:
`  i = 1;`
Which probably isn't what you want.

Working with i and p as local variables may seem more of a PITA right now, but it will definitely improve your programming - and we're right here if you need to ask something about it.

14. plz explain more about this local variables of which u speak..and yea the for loop (i)is always equal to 1.when i want (i)value to equal 0then1then0then1 etc.