Problem with passing sprite pointers in function
Posted: Fri Jan 30, 2009 5:27 pm
Hello everyone, I'm having a problem with array pointers in a function to manage my sprite during movements.. What I have is a function that checks which button's been pressed then adjusts the x and y and sets the frame to the object with the correct one (it's a walking character which has 3 frames animation for each direction -total 12 frames-). What I can't get right is the passing of parameters in the function. I don't want to have a messy code with a function inside the while loop or a global variable that keeps stored the current frame animation. I think it's a problem with pointer passing or something like that, this is the code:
the if check inside the handleInput function is just to check what frame the animation is at so it can put the other one.
Though when I try to run this code I can only see the spriteBank[0] frame....
MySprite curr is initialized as this:
If I put the function inside the while loop (like, directly copy+paste it) or put curr as a global variable at the beginning of the program (outside the main function) everything works fine, but it's messy.
What am I doing wrong? can somebody please help? thanks a lot
Code: Select all
//function to read input and store sprite into curr (current sprite frame)
void handleInput(MySprite* curr,MySprite* spriteBank) {
scanKeys();
if (keysHeld() & KEY_UP) {
if(curr->id==0)
curr = &spriteBank[2];
else if(curr->id==2)
curr = &spriteBank[0];
else
curr = &spriteBank[0];
std::cout << "up pressed";
posy-=step;
} else if (keysHeld() & KEY_DOWN) {
if(curr->id==6)
curr = &spriteBank[8];
else if(curr->id==8)
curr = &spriteBank[6];
else
curr = &spriteBank[6];
std::cout << "down pressed";
posy+=step;
} else if (keysHeld() & KEY_LEFT) {
if(curr->id==9)
curr = &spriteBank[11];
else if(curr->id==11)
curr = &spriteBank[9];
else
curr = &spriteBank[9];
std::cout << "left pressed";
posx-=step;
} else if (keysHeld() & KEY_RIGHT) {
if(curr->id==3)
curr = &spriteBank[5];
else if(curr->id==5)
curr = &spriteBank[3];
else
curr = &spriteBank[3];
std::cout << "right pressed";
posx+=step;
} else {
if((curr->id==0)||(curr->id==2))
curr = &spriteBank[1];
else if((curr->id==3)||(curr->id==5))
curr = &spriteBank[4];
else if((curr->id==6)||(curr->id==8))
curr = &spriteBank[7];
else if((curr->id==9)||(curr->id==11))
curr = &spriteBank[10];
std::cout << "stopped";
}
}
//-----///
....
//inside while loop:
swiWaitForVBlank();
oamUpdate(&oamMain);
oamClear(&oamMain,0,0);
if(i%10==0){ //i is just a counter to slow down the frame changing speed
handleInput(&curr,sprite);
consoleClear();
}
i++;
}
Though when I try to run this code I can only see the spriteBank[0] frame....
MySprite curr is initialized as this:
Code: Select all
MySprite curr = sprite[0];
What am I doing wrong? can somebody please help? thanks a lot