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!
if(segundoMapa){
if(getY() > 459 && Greenfoot.isKeyDown("right")){
((Episodio_1_2)getWorld()).scroll();
}
}
I already add:
public void setLocation(int x, int y) { }// 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);
}
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);
}
}
Thanks for the help!int dsx = 0; int dsy = 0; if (getX() < bLineX) dsx = getX()-bLineX; if (getX() > aLineX) dsx = getX()-aLineX;
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;
}
}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);
}
}
Then, when I walk to the right, and back to the left, the "walls" stay like this:
How I can fix that?
Thanks!super(800, 622, 1);
super(800, 622, 1, false);
super(800, 622, 1);
super(800, 622, 1, false);
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!