Gameboy Development Forum

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.

Ads

#1 2020-07-27 18:21:57

Spaceman2001
New member
Registered: 2020-07-27
Posts: 1

Stop sprite from going off screen

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

 

#2 2020-07-28 06:40:38

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: Stop sprite from going off screen

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

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson