This site requires JavaScript, please enable it in your browser!
Greenfoot back
Shire
Shire wrote ...

2023/1/4

Why does my background start glitching when i go left or up?

Shire Shire

2023/1/4

#
I made some code to make it look like my game is focused on my actor, and the background moves accordingly when I press the arrow keys. It works fine when I move down and to the right, but in the other two directions, it glitches and starts to repeat the texture a lot. I wanted to know why this happens and how to fix it, i inserted the code that I'm using
public void act()
    {
        scrollBackground();
    }
    public void scrollBackground()
    {
        GreenfootImage bg = new GreenfootImage(getBackground());
        
        if(Greenfoot.isKeyDown("right"))
        {
            getBackground().drawImage(bg, -10, 0);
            getBackground().drawImage(bg, getWidth()-100, 0);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            getBackground().drawImage(bg, -10, 0);
            getBackground().drawImage(bg, getWidth()+100, 0);
        }
        if(Greenfoot.isKeyDown("up"))
        {
            getBackground().drawImage(bg, 0, +10);
            getBackground().drawImage(bg, 0, getHeight()+100);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            getBackground().drawImage(bg, 0, -10);
            getBackground().drawImage(bg, 0, getHeight()-100);
            
        }
    }
danpost danpost

2023/1/4

#
Shire wrote...
I made some code to make it look like my game is focused on my actor, and the background moves accordingly when I press the arrow keys. It works fine when I move down and to the right, but in the other two directions, it glitches and starts to repeat the texture a lot. I wanted to know why this happens and how to fix it, i inserted the code that I'm using << Code Omitted >>
Line 16 should use a plus 10, not a minus 10. Lines 17 and 22 use coordinates that are outside the bounds of the world. No positive coordinate should be beyond the dimension of the image to be drawn on. I think you need 10-getWidth() or 10-getHeight() for those lines. Lines 12 and 27 seem to have a problem in their coordinates as well. One should use -10-getWidth() and the other should use -10-getHeight().
You need to login to post a reply.