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

2016/4/21

Scrolling world

1
2
3
4
GB309 GB309

2016/4/21

#
I don't really understand the coding I would have to use if I want to use a scrolling world. I have used a very long image as my background and only a partial amount can be seen (which is fine) but what I want to do is have the image scroll as my main character reaches near the end of the image that we can see originally. How would I do this?
danpost danpost

2016/4/21

#
The basic idea is to control where to draw the large image on the background image of the world when needed. There are several support classes on the site that perform scrolling in various forms. I have several myself. The one most suited for your needs might be the one in my Bee Quick (ImgScroll Demo) game. You can check it out to see how it is done. Then, you can even import the ImgScroll class into your project and use it for scrolling the world background.
GB309 GB309

2016/4/21

#
Thanks!! I found an error that I cant seem to solve as it asks me to return type for 'Public ImgScroll' . I don't understand how to fix this.
danpost danpost

2016/4/21

#
GB309 wrote...
I found an error that I cant seem to solve as it asks me to return type for 'Public ImgScroll' . I don't understand how to fix this.
Please indicate what class the error reside and post the code of the method (at least) where the error occurs.
GB309 GB309

2016/4/24

#
public class Backgroundscroll { private World scrollWorld; // the world the background image scrolls in private GreenfootImage scrollImages; // the drawing panels (background image broken down) private int scrollWidth, scrollHeight; // the dimensions of the scrolling area private int worldWidth, worldHeight; // the dimensions of the world window private int xScrAmt, yScrAmt; // the overall horizontal and vertical scrolled amounts /** * calls the main constructor of the class using the dimensions of the given image for * the dimensions of the scrolling area * * @param scrWorld the world the given background image is to scroll in * @param scrImage the scrolling background image to be used in the given world */ public Backgroundscroll(World scrWorld, GreenfootImage scrImage) { this(scrWorld, scrImage, scrImage.getWidth(), scrImage.getHeight()); } /** * this main constructor sets field values and initial world background image * * @param scrWorld the world the given background image is to scroll in * @param repImage the image that is tiled to create the scrolling background image of the given world * @param wide the horizontal dimension of the scrolling area * @param high the vertical dimension of the scrolling area */ public Backgroundscroll(World scrWorld, GreenfootImage repImage, int wide, int high) { // the field values scrollWorld = scrWorld; worldWidth = scrWorld.getWidth(); worldHeight = scrWorld.getHeight(); scrollWidth = wide; scrollHeight = high; // the scrolling image if (repImage == null) repImage = scrWorld.getBackground(); GreenfootImage scrImage = new GreenfootImage(wide, high); scrImage.setColor(java.awt.Color.white); scrImage.fill(); // ensures no transparent pixels in background for (int i=0; i<wide; i+=repImage.getWidth()) for (int j=0; j<high; j+=repImage.getHeight()) scrImage.drawImage(repImage, i, j); // the drawing panels (to help reduce lag by reducing the number of pixels drawn when scrolling) int x = 1+scrollWidth/worldWidth; // number of panels across int y = 1+scrollHeight/worldHeight; // number of panels down scrollImages = new GreenfootImage; // creates the array for (int j=0; j<y; j++) for (int i=0; i<x; i++) // fills the array { scrollImages = new GreenfootImage(worldWidth, worldHeight); scrollImages.drawImage(scrImage, -i*worldWidth, -j*worldHeight); } scrollBackground(); // sets initial world background image } /** sets the world background image determined by the current scroll values */ private void scrollBackground() { int x = (-xScrAmt)/worldWidth; // panel x index int y = (-yScrAmt)/worldHeight; // panel y index int dx = -((-xScrAmt)%worldWidth); // drawing x offset int dy = -((-yScrAmt)%worldHeight); // drawing y offset GreenfootImage bg = scrollWorld.getBackground(); // gets local reference to world background bg.drawImage(scrollImages, dx, dy); // draw top-left image if (dx != 0) // draw top-right image if needed bg.drawImage(scrollImages, dx+worldWidth, dy); if (dy != 0) // draw bottom-left image if needed bg.drawImage(scrollImages, dx, dy+worldHeight); if (dx != 0 && dy != 0) // draw bottom-right image if needed bg.drawImage(scrollImages, dx+worldWidth, dy+worldHeight); }
GB309 GB309

2016/4/24

#
tells me it does not recognize certain terms and that The 'Public Backgroundscroll' had to be returned
danpost danpost

2016/4/24

#
GB309 wrote...
tells me it does not recognize certain terms and that The 'Public Backgroundscroll' had to be returned
Other than the 'import' statement (for 'greenfoot.*') and the closing squiggly bracket at the end, the class is complete and compiles. I do not get any "does not recognize" messages or anything about a return needed with 'public Backgroundscroll'.
GB309 GB309

2016/4/25

#
ok. so what do I need to do to fix this? (thanks a million for this btw)
GB309 GB309

2016/4/25

#
it says it doesn't recognize Greenfoot image or world but before it said that it was alright and that the only thing to be fixed was that 'public Backgroundscroll' as it had to be returned. however I think that's ok now.
GB309 GB309

2016/4/25

#
very sorry dan. My coding is apparently fine with no errors but when I close the source code, it says there are errors on the actual variable 2. The game is able to run but the screen wont scroll still, I think this might be because I need a main character added to the source??
danpost danpost

2016/4/25

#
GB309 wrote...
The game is able to run but the screen wont scroll still
Okay, where is your 'scroll' method, which is supposed to call the 'scrollBackground' method?
GB309 GB309

2016/4/26

#
I don't think I have one... (sorry I'm not that good at greenfoot as you can probably tell)
danpost danpost

2016/4/26

#
GB309 wrote...
I don't think I have one... (sorry I'm not that good at greenfoot as you can probably tell)
Well, it was part of the class you copied from -- and it is a necessary part of the class. It is the method your world will call from its act method to perform the scrolling.
GB309 GB309

2016/4/26

#
ok Ill look for it thanks
GB309 GB309

2016/5/1

#
so I have sorted out the scrolling world but the only thing is that I want to spawn objects from the top of the screen down(which I have kind of solved), but when I call that method I cannot spawn them anywhere else but at the start of the map. What do I do?!
There are more replies on the next page.
1
2
3
4