Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)
You are not logged in.
Hello!
I recently started learning GBDK and I'm trying to make a sprite stop whenever it touches one of the four screen borders, but so far nothing has worked. The code doesn't really work well. The sprite is still going off screen and after some time it stops somewhere around the space.
This is the code:
if(joypad() == J_LEFT){
x--;
currentX = x;
if(currentX > 0){
scroll_sprite(0, x, 0);
delay(10);
}
else{
printf("LEFT BORDER\n");
}
}
if(joypad() == J_RIGHT){
x++;
currentX = x;
if(currentX < 160){
scroll_sprite(0, x, 0);
delay(10);
}
else{
printf("RIGHT BORDER\n");
}
}
if(joypad() == J_UP){
y--;
currentY = y;
if(currentY > 0){
scroll_sprite(0, 0, y);
delay(10);
}
else{
printf("UPPER BORDER\n");
}
}
if(joypad() == J_DOWN){
y++;
currentY = y;
if(currentY < 144){
scroll_sprite(0, 0, y);
delay(10);
}
else{
printf("LOWER BORDER\n");
}
}
Offline
1. you must x-- only if x > 0; also x++ only if x < 160.
2. put the sprite by absolute coords, use move_sprite(n, x, y)
ps: also never use delay()
Last edited by toxa (2020-07-28 06:42:44)
Offline