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

2016/12/9

Scrolling Map - Large Image

1
2
Kazee Kazee

2016/12/9

#
Hi! I have another question, How I can Scroll the background of my game? This is the entire : http://prnt.sc/dh4jep And this is how in game look the first part: http://prnt.sc/dh4jhn So that's it. Thanks for the help!
danpost danpost

2016/12/9

#
My Scrolling Tutorial scenario has all the answers.
Kazee Kazee

2016/12/9

#
@danpost Thanks! But I cant download it :(
danpost danpost

2016/12/9

#
Kazee wrote...
Thanks! But I cant download it :(
The Scroller class code can be found here. It is well documented; so, it should not be too difficult to follow. You would be using a (limited = true) scrolling world.
Kazee Kazee

2016/12/9

#
danpost wrote...
Kazee wrote...
Thanks! But I cant download it :(
The Scroller class code can be found here. It is well documented; so, it should not be too difficult to follow. You would be using a (limited = true) scrolling world.
Ok thanks! I already can scroll the background with this in my actor:
       if(segundoMapa){
       if(getY() > 459 && Greenfoot.isKeyDown("right")){
           ((Episodio_1_2)getWorld()).scroll();
        }
       }
I'll be looking at how to make the scroll go back when the actor goes to the left and goes forward when the actor goes right. But i have a question, How I cant make the "walls" dont move? Like I already add:
public void setLocation(int x, int y) { }
On the wall's class. Thanks for the help!
danpost danpost

2016/12/9

#
I think what you are trying to say is that you do not want the walls to maintain their positions with respect to the screen, which means you want them to scroll with the background. In that case, you do not want the 'setLocation' method in the class of the wall. That line is only placed into classes that create object that you want to maintain the same position on the screen at all times.
danpost danpost

2016/12/9

#
Kazee wrote...
I already can scroll the background with this in my actor: < Code Omitted > I'll be looking at how to make the scroll go back when the actor goes to the left and goes forward when the actor goes right.
Instead of calling the Episodio_1_2 scroll method from the actor class, it would be better called from the act method of the Episodio_1_2 class. Maintain a reference to the actor that is to be maintain in the center of the screen and determine the amount to scroll by the offset amounts of its position from the center:
// instance reference field in Episodio_1_2 class
Actor scrollActor = null; // assigned an actor in the constructor of the class

// in act
scroll();

// the scroll method
private void scroll()
{
    if (scrollActor == null || scrollActor.getWorld() != this) return;
    int dsx = scrollActor.getX()-WIDE/2; // horizontal offset from center screen
    int dsy = scrollActor.getY()-HIGH/2; // vertical offset from center screen
    scroller.scroll(dsx, dsy);
}
Kazee Kazee

2016/12/10

#
Thanks! About the "walls", I want them to be kept in the same position as they are. In this image the "walls" are objects for block the walk of the character:
private void moveLeft()
    {
        if(getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallHorizontal.class) != null){
            setLocation(getX() + 15, getY()); 
        } else {
            setLocation(getX() - speed, getY()); 
        }
    }   
    
    private void moveRight()
    {
        if(getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallHorizontal.class) != null){
           setLocation(getX() - 15, getY()); 
        } else {
           setLocation(getX() + speed, getY());
        }
    }
    
    private void moveDown()
    {
        if(getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallHorizontal.class) != null){
           setLocation(getX(), getY() - 15); 
        } else {
        setLocation(getX(), getY() + speed);
        }
    }
    
    private void moveUp()
    {
        if(getOneIntersectingObject(Wall.class) != null || getOneIntersectingObject(WallHorizontal.class) != null){
           setLocation(getX(), getY() + 15); 
        } else {
        setLocation(getX(), getY() - speed);
        }
    }     
And about the walk, I trying when the actor cross the A line, the scroll goes to right, and if the actor cross the B line, the scroll goes left. Thanks for the help!
danpost danpost

2016/12/10

#
Kazee wrote...
about the walk, I trying when the actor cross the A line, the scroll goes to right, and if the actor cross the B line, the scroll goes left. < Image Omitted >
Replace lines 11 and 12 of my last code post to:
int dsx = 0;
int dsy = 0;
if (getX() < bLineX) dsx = getX()-bLineX;
if (getX() > aLineX) dsx = getX()-aLineX;
An easy movement code for the actor might be:
int speed = 1; // either pre-defined or set here
int dx = 0;
int dy = 0;
if (Greenfoot.isKeyDown("right")) dx++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("down")) dy++;
if (Greenfoot.isKeyDown("up")) dy--;
for (int i=0; i<speed; i++)
{
    setLocation(getX()+dx, getY()+dy);
    if (isTouching(Wall.class))
    {
        setLocation(getX()-dx, getY()-dy);
        break;
    }
}
Kazee Kazee

2016/12/10

#
Thanks for the movement code! It's much better than mine. About the scroll, where I define bLineX and aLineX? Also, the getX() appear "cannot find symbol method",
public class Episodio_1_2 extends World
{
    private Scroller scroller;
    GreenfootSound bgMusic = new GreenfootSound("bgm/run.mp3");
    Actor scrollActor = null;
    
    public Episodio_1_2()
    {    
        super(800, 622, 1); 
        
        scrollActor = new Kaze("right");
        
        
        
        
        addObject(new WallHorizontal(100,50), 41, 416);
        addObject(new WallHorizontal(100,50), 42, 302);
        addObject(new WallHorizontal(200,50), 132, 464);
        addObject(new WallHorizontal(), 271, 163);
        addObject(new WallHorizontal(), 478, 556);
        addObject(new Wall(), 72, 82);
        addObject(new Wall(), 119, 38);
        addObject(new Wall(130,400), 456, 221);
        addObject(new Wall(50,100), 210, 539);
               
        scroller = new Scroller(this, new GreenfootImage("map_exterior.PNG"), 4311, 622);
        bgMusic.playLoop();
    }
    
    public void act(){
        scroll();
    }
    
    public void scroll()
    {
        if (scrollActor == null || scrollActor.getWorld() != this) return;
        int dsx = 0;
        int dsy = 0;
        if (getX() < bLineX) dsx = getX()-bLineX;
        if (getX() > aLineX) dsx = getX()-aLineX;
        scroller.scroll(dsx, dsy);
    }
}
Thanks!
Super_Hippo Super_Hippo

2016/12/11

#
aLineX and bLineX are the x coordinates of these red lines. Add a 'scrollActor.' before all 'getX()' instances.
Kazee Kazee

2016/12/11

#
Super_Hippo wrote...
aLineX and bLineX are the x coordinates of these red lines. Add a 'scrollActor.' before all 'getX()' instances.
Thanks! All is OK! But I still have a problem with the walls. First, this is the positions of them: Then, when I walk to the right, and back to the left, the "walls" stay like this: How I can fix that? Thanks!
Super_Hippo Super_Hippo

2016/12/11

#
Maybe you could try to change...
super(800, 622, 1);
...to...
super(800, 622, 1, false);
Then the walls should be able to leave the visible part of the world.
Kazee Kazee

2016/12/11

#
Super_Hippo wrote...
Maybe you could try to change...
super(800, 622, 1);
...to...
super(800, 622, 1, false);
Then the walls should be able to leave the visible part of the world.
Thanks!! It works! Now I have another question, how I can make walls for all the map? I have the large wall up, but i need create another wall below. Like: And: In first, is easy because the world is created with the walls, but while the actor walk to the right, how I can create new walls? In the positions of those red square? I tried but the X and Y change according as the actor move. Thanks!
danpost danpost

2016/12/12

#
Kazee wrote...
In first, is easy because the world is created with the walls, but while the actor walk to the right, how I can create new walls? In the positions of those red square?
They are part of the same world, you can add them when the world is created as well.
There are more replies on the next page.
1
2