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

2021/5/21

SCROLLING BACKGROUND

ronald ronald

2021/5/21

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Scrolling extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
     
    GreenfootImage background;
    int currentX;
    int currentY;
     
    public Scrolling()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 300, 1, false);
        background = new GreenfootImage("galaxy-stars01.jpg");
        getBackground().drawImage(background, 0, 0);
    }
     
    public void act()
    {
        drawBackground();
        checkKeys();
    }
     
    public void checkKeys()
    {
        if (Greenfoot.isKeyDown("left")) changeCurrentXY(-1, 0);
        if (Greenfoot.isKeyDown("right")) changeCurrentXY(1, 0);
        if (Greenfoot.isKeyDown("up")) changeCurrentXY(0, 1);
        if (Greenfoot.isKeyDown("down")) changeCurrentXY(0, -1);
    }
     
    public void drawBackground()
    {
        getBackground().drawImage(background, -currentX, -currentY);
    }
     
    public void changeCurrentXY(int changeX, int changeY)
    {
        currentX += changeX;
        currentY -= changeY;
    }
}
I found a code on a scrolling I'm wondering is this normal that the image becomes blurry once when you go out of the frame thank you
danpost danpost

2021/5/21

#
I do not know where you found that code, but it is no good. Once scrolling goes beyond the extent of the background image, it will create problems (as you have noticed). Please refer to my Scrolling Tutorial scenario for assistance. The Scroller class associated with it is tested, tried and true for ANY type of scrolling (or "camera movement") as the associated demo (link in description) will prove.
ronald ronald

2021/5/21

#
yes I think about your scenario this code, I found it on YouTube Scrolling
ronald ronald

2021/5/21

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
 
    /**
     * Constructor for objects of class MyWorld.
     *
     */
     
    private Background img0, img1;
     
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900, 600, 1, false);
         
        img0 = new Background();
        addObject(img0, getWidth()/2, getHeight()/2);
        img1 = new Background();
        addObject(img1, getWidth() + getWidth()/2, getHeight()/2);
         
    }
     
    public void act()
    {
        //if(Greenfoot.isKeyDown("right"))    
        //{
        //    img0.scroll();
        //    img1.scroll();
        //}
         
        //if(Greenfoot.isKeyDown("left"));
        //{
            img0.scroll();
            img1.scroll();
        //}
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Background here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Background extends Actor
{
    /**
     * Act - do whatever the Background wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        // Add your action code here.
    }
     
    public void scroll()
    {
        if(getX() < -getImage().getWidth()/2)
        {
            setLocation (getX() + getWorld().getWidth()*2, getY());
        }
        setLocation (getX()-5, getY());       
    }
}
one more thing I also find another code on YouTube What is weird, this image is automatically moved without the right key Can you tell me if the code is good or not? I'm going to look at your scenario
danpost danpost

2021/5/21

#
ronald wrote...
I also find another code on YouTube What is weird, this image is automatically moved without the right key Can you tell me if the code is good or not?
It is probably okay for what it is for -- which is a continuous horizontal scroller. That is, it will not scroll vertically and scrolls the image to the left non-stop (giving the appearance of the "camera" being moved to the right non-stop).
ronald ronald

2021/5/21

#
okay thank you I am typing your code of your scenario I'll keep you informed thanks again
danpost danpost

2021/5/21

#
ronald wrote...
I am typing your code of your scenarion
You do not have to type it. Just go to link in description and copy/paste.
ronald ronald

2021/5/21

#
too late I just finished tapping him it makes me training The image is moved vertically perfectly I have to put variables x and y Once I have copied the link I glue where after to see the code ???
danpost danpost

2021/5/21

#
ronald wrote...
Once I have copied the link I glue where after to see the code ???
Please re-phrase. I do not understand the question.
ronald ronald

2021/5/21

#
The only way is to tap the code I do not think I can have the code by the link as on the greenfoot IDE or maybe it's me who misunderstood
You need to login to post a reply.