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

2019/12/1

Trouble with background

1
2
3
Al3x7373 Al3x7373

2019/12/1

#
I've been trying to solve an issue with my background for sometime but i can't find an answer so i hope u guys can help me. So i've been basically trying to do a background like in a typical Mario game but every video that I've watched showed me either how to do a Scrolling Background or a infinit Scrolling background and I'm really confused on how to do this.
danpost danpost

2019/12/1

#
Al3x7373 wrote...
I've been trying to solve an issue with my background for sometime but i can't find an answer so i hope u guys can help me. So i've been basically trying to do a background like in a typical Mario game but every video that I've watched showed me either how to do a Scrolling Background or a infinit Scrolling background and I'm really confused on how to do this.
Please refer to my Scrolling Tutorial scenario.
Al3x7373 Al3x7373

2019/12/1

#
danpost wrote...
Al3x7373 wrote...
I've been trying to solve an issue with my background for sometime but i can't find an answer so i hope u guys can help me. So i've been basically trying to do a background like in a typical Mario game but every video that I've watched showed me either how to do a Scrolling Background or a infinit Scrolling background and I'm really confused on how to do this.
Please refer to my Scrolling Tutorial scenario.
Thanks I appreciate your help. I used your Tutorial as main reference and I still encountered a few Issues.( I'm trying to do an Actor follow Scrolling code) I can't find a symbol for both the Scroller, Player() and the Scroll. My scrollactor is Megaman import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Sky here. * * @author (your name) * @version (a version number or a date) */ public class Sky extends World { public static final int WIDE = 1200; public static final int HIGH =540; public static final int CELL =1; Scroller scroller; Actor Megaman; public Sky() { super(WIDE, HIGH, CELL, false); addObject( new Megaman(), 0, 100); addObject( new Boost(), 600, 300); scroller = new Scroller(); GreenfootImage bg = new GreenfootImage("Unknown.jpeg"); int bgWide = bg.getWidth(); int bgHigh = bg.getHeight(); scroller= new Scroller(this, bg, bgWide, bgHigh); Megaman = new Player(); addObject( Megaman, bgWide/2, bgHigh/2); scroll(); } public void act() { if( Megaman!=null)scroll(); } { int dsx = Megaman.getX()-WIDE/2; int dsy = Megaman.getY()-HIGH/2; scroller.scroll(dsx,dsy); } } That's my code if that may help you.
danpost danpost

2019/12/1

#
The following should be closer to what you need:
// static fields
Scroller scroller;
Actor megaman;

public Sky()
{
    super, WIDE, HIGH, CELL, false);
    GreenfootImage bg = new GreenfootImage("Unknown.jpeg");
    bgWide = bg.getWidth();
    bgHigh = bg.getHeight();
    scroller = new Scroller(this, bg, bgWide, bgHigh);
    megaman = new Megaman();
    addObject(megaman, bgWide/2, bgHigh/2);
    addObject(new Boost, 600, 300);
    scroll();
}

public void act()
{
    if (megaman != null && megaman.getWorld() == this) scroll();
}

private void scroll()
{
    int dsx = megaman.getX()-WIDE/2;
    int dsy = megaman.getY()-HIGH/2;
    scroller.scroll(dsx, dsy);
}
Al3x7373 Al3x7373

2019/12/1

#
danpost wrote...
The following should be closer to what you need:
// static fields
Scroller scroller;
Actor megaman;

public Sky()
{
    super, WIDE, HIGH, CELL, false);
    GreenfootImage bg = new GreenfootImage("Unknown.jpeg");
    bgWide = bg.getWidth();
    bgHigh = bg.getHeight();
    scroller = new Scroller(this, bg, bgWide, bgHigh);
    megaman = new Megaman();
    addObject(megaman, bgWide/2, bgHigh/2);
    addObject(new Boost, 600, 300);
    scroll();
}

public void act()
{
    if (megaman != null && megaman.getWorld() == this) scroll();
}

private void scroll()
{
    int dsx = megaman.getX()-WIDE/2;
    int dsy = megaman.getY()-HIGH/2;
    scroller.scroll(dsx, dsy);
}
I'm still having a problem with the bgWide, bgHigh and the Scroller. It can't find the Symbols.
danpost danpost

2019/12/1

#
Al3x7373 wrote...
I'm still having a problem with the bgWide, bgHigh and the Scroller. It can't find the Symbols.
Sorry -- lines 9 and 10 were both supposed to start with 'int'.
Al3x7373 Al3x7373

2019/12/1

#
danpost wrote...
Al3x7373 wrote...
I'm still having a problem with the bgWide, bgHigh and the Scroller. It can't find the Symbols.
Sorry -- lines 9 and 10 were both supposed to start with 'int'.
Oh okay thanks that resolves all problems except for the Scroller it still hasn't got a symbol.
danpost danpost

2019/12/1

#
Al3x7373 wrote...
Oh okay thanks that resolves all problems except for the Scroller it still hasn't got a symbol.
Did you add my Scroller class to your scenario?
Al3x7373 Al3x7373

2019/12/1

#
danpost wrote...
Al3x7373 wrote...
Oh okay thanks that resolves all problems except for the Scroller it still hasn't got a symbol.
Did you add my Scroller class to your scenario?
Okay i found it thanks. But I can now launch my game but it doesn't follow my megaman. ( Thanks for taking so much time for me by the way). Thats my code for the bg : import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Sky here. * * @author (your name) * @version (a version number or a date) */ public class Sky extends World { public static final int WIDE = 1200; public static final int HIGH =540; public static final int CELL =1; Scroller scroller; Actor Megaman; public Sky() { super( WIDE, HIGH, CELL, false); GreenfootImage bg = new GreenfootImage("Unknown.jpeg"); int bgWide = bg.getWidth(); int bgHigh = bg.getHeight(); scroller = new Scroller(this, bg, bgWide, bgHigh); Megaman = new Megaman(); addObject(Megaman, bgWide/2, bgHigh/2); scroll(); } public void act() { if (Megaman != null && Megaman.getWorld() == this) scroll(); } private void scroll() { int dsx = Megaman.getX()-WIDE/2; int dsy = Megaman.getY()-HIGH/2; scroller.scroll(dsx, dsy); } } and i took this one for the Scroller import greenfoot.*; /** * CLASS: Scroller (extends Object) * AUTHOR: danpost (greenfoot.org username) * DATE: November 11, 2016 * MODIFIED: December 22, 2016 (fixed 'scroll' method for limited no-image scrolling) * MODIFIED: February 21, 2017 (fixed scroll offsets for unlimited no-image scrolling) * * DESCRIPTION: This is a support class for a scrolling world. It contains two constructors; * one for unlimited scrolling and one for limited scrolling. Both constructors have an 'image' * parameter. Because image manipulation can hog up CPU time, it is important to remember that * it is better not to have a scrolling background image (having an Actor for the background is * probably worse than having the background scroll). For unlimited scrolling using a background * image, the smaller that background image to be tiled, the better. Making the viewport (the * size of the world that is visible) smaller can help in CPU expense, also. Scrolling worlds * should be unbounded, allowing actors to move beyond the visible area. Ensuring that actors * are removed from the world if no longer needed when out of view will help to prevent lag, * as well. * * It is the responsibility of the World object that creates a Scroller object to determine when * to scroll and by how much. */ public class Scroller { private World world; // view window world private GreenfootImage scrollImage; // scrolling image private boolean limited; // flag to indicate whether scrolling is limited or not private int scrolledX, scrolledY; // current scrolled distances private int wide, high; // if limited, dimensions of scrolling area else of image to wrap /** * This constructor is for an unlimited scrolling world; * If 'image' is null, the background will not change; else the given image is wrapped * * @param viewWorld the world that scrolling will be performed on * @param image the background image that will be tiled, if needed, and wrap with scrolling */ public Scroller(World viewWorld, GreenfootImage image) { world = viewWorld; scrollImage = image; if (image != null) { wide = image.getWidth(); high = image.getHeight(); } scroll(0, 0); // sets initial background image } /** * This constructor is for a limited scrolling world; * If 'image' is smaller than the given total scrolling area, it will be tiled * If 'image' is null, the background will not change * * @param viewWorld the world that scrolling will be performed on * @param image the background image that will be tiled, if needed, to fill the scrolling area * @param wide the width of the visible area encompassed through scrolling; * the given value must be at least equal to the width of 'viewWorld' and * is given in world cells (not in pixels) * @param high the height of the visible area encompassed through scrolling; * the given value must be at least equal to the height of 'viewWorld' and * is given in world cells (not in pixels) */ public Scroller(World viewWorld, GreenfootImage image, int wide, int high) { this.wide = wide; this.high = high; limited = true; world = viewWorld; if (image != null) { // create an image as large as scrolling area; tiled, if needeed scrollImage = new GreenfootImage(wide*world.getCellSize(), high*world.getCellSize()); for (int x=0; x<wide*world.getCellSize(); x+= image.getWidth()) for (int y=0; y<high*world.getCellSize(); y+=image.getHeight()) scrollImage.drawImage(image, x, y); // set initial background image scroll(0, 0); } } /** * performs scrolling on 'world' by the given distances along the horizontal and vertical; * if 'limited' is false, requested distances are actual scrolling distances; * if 'limited' is true, the distances may be adjusted due to the limits of scrolling * * @param dsx the requested distance to shift everything horizontally * @param dsy the requested distance to shift everything vertically */ public void scroll(int dsx, int dsy) { // adjust scroll amounts and scroll background image if (limited) { // calculate limits of scrolling int maxX = wide-world.getWidth(); int maxY = high-world.getHeight(); // apply limits to distances to scroll if (scrolledX+dsx < 0) dsx = -scrolledX; if (scrolledX+dsx >= maxX) dsx = maxX-scrolledX; if (scrolledY+dsy < 0) dsy = -scrolledY; if (scrolledY+dsy >= maxY) dsy = maxY-scrolledY; // update scroll positions scrolledX += dsx; scrolledY += dsy; // scroll background image if (scrollImage != null) { world.getBackground().drawImage ( scrollImage, -scrolledX*world.getCellSize(), -scrolledY*world.getCellSize() ); } } else // unlimited image wrapping { // update scroll positions scrolledX += dsx; scrolledY += dsy; // scroll background image if (scrollImage != null) { // create working variables of scroll positions int imageX = scrolledX*world.getCellSize(); int imageY = scrolledY*world.getCellSize(); // get near-zero starting positions for drawing 'scrollImage' imageX = imageX%wide; imageY = imageY%high; // adjust negative values as needed if (imageX < 0) imageX += wide; if (imageY < 0) imageY += high; // create image of appropriate size and tile fill 'scrollImage' onto it GreenfootImage hold = new GreenfootImage(scrollImage); hold.drawImage(scrollImage, -imageX, -imageY); if (imageX > 0) hold.drawImage(scrollImage, wide-imageX, -imageY); if (imageY > 0) hold.drawImage(scrollImage, -imageX, high-imageY); if (imageX > 0 && imageY > 0) hold.drawImage(scrollImage, wide-imageX, high-imageY); // set image to background of 'world' world.setBackground(hold); } } // adjust position of all actors (that can move with 'setLocation') for (Object obj : world.getObjects(null)) { Actor actor = (Actor) obj; actor.setLocation(actor.getX()-dsx, actor.getY()-dsy); } } /** * getter method for the current total scrolled distance horizontally * * @return the current total offset of horizontal scrolling */ public int getScrolledX() { return scrolledX; } /** * getter method for the current total scrolled distance vertically * * @return the current total offset of vertical scrolling */ public int getScrolledY() { return scrolledY; } }
danpost danpost

2019/12/1

#
Maybe your world dimension(s) are too large. Try setting WIDE to 600 and HIGH to 400.
Al3x7373 Al3x7373

2019/12/1

#
danpost wrote...
Maybe your world dimension(s) are too large. Try setting WIDE to 600 and HIGH to 400.
It still doesn't work. Maybe the problem comes from the bgWide and the bgHigh because my Megaman doesn't spawn in the middle of the map (addObject(Megaman, bgWide/2, bgHigh/2);)
danpost danpost

2019/12/1

#
Al3x7373 wrote...
It still doesn't work. Maybe the problem comes from the bgWide and the bgHigh because my Megaman doesn't spawn in the middle of the map (addObject(Megaman, bgWide/2, bgHigh/2);)
That would not be it. Do this -- inspect world, inspect scroller, and report back the values of wide and high.
Al3x7373 Al3x7373

2019/12/2

#
danpost wrote...
Al3x7373 wrote...
It still doesn't work. Maybe the problem comes from the bgWide and the bgHigh because my Megaman doesn't spawn in the middle of the map (addObject(Megaman, bgWide/2, bgHigh/2);)
That would not be it. Do this -- inspect world, inspect scroller, and report back the values of wide and high.
i got for the world i got 1200 for Wide, 540 for High. and for the scroller i got no fields
danpost danpost

2019/12/2

#
Al3x7373 wrote...
i got for the world i got 1200 for Wide, 540 for High. and for the scroller i got no fields
There has got to be scroller fields.
Al3x7373 Al3x7373

2019/12/2

#
danpost wrote...
Al3x7373 wrote...
i got for the world i got 1200 for Wide, 540 for High. and for the scroller i got no fields
There has got to be scroller fields.
Do you maybe know why i got no fields in the Scroller ? I can send u my code if it might help you. I got 2 Actors Megaman and Boost and I got 1 world Sky is the name of the world.
There are more replies on the next page.
1
2
3