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

2017/3/27

How do you add objects that come onto the screen later in a moving background?

Hamza_Abdulla03 Hamza_Abdulla03

2017/3/27

#
My background scrolls horizontally and i need to add objects that come after into the screen but you cant see them before. I will be happy to share my coding. Please help
danpost danpost

2017/3/27

#
If the scrolling implementation was done optimally, then you should be able to just add the actor, 'actor', with one of the following statement types:
addObject(actor, -30, Greenfoot.getRandomNumber(getHeight());
// or
addObject(actor, getWidth()+30, Greenfoot.getRandomNumber(getHeight());
Your world constructor should be one of: super(int, int, int, false);
Hamza_Abdulla03 Hamza_Abdulla03

2017/3/27

#
invalid method declaration
Hamza_Abdulla03 Hamza_Abdulla03

2017/3/27

#
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 Background extends World { private static final String bgImageName = "space1.jpg"; private static final double scrollSpeed = 2.5; private static final int picWidth = (new GreenfootImage(bgImageName)).getWidth(); private GreenfootImage bgImage, bgBase; private int scrollPosition = 0; public Background() { super(800, 400, 1); setBackground(bgImageName); bgImage = new GreenfootImage(getBackground()); bgBase = new GreenfootImage(picWidth, getHeight()); bgBase.drawImage(bgImage, 0, 0); prepare(); } public void act() { scrollPosition -= scrollSpeed; while(scrollSpeed > 0 && scrollPosition < -picWidth) scrollPosition += picWidth; while(scrollSpeed < 0 && scrollPosition > 0) scrollPosition -= picWidth; paint(scrollPosition); } private void paint(int position) { GreenfootImage bg = getBackground(); bg.drawImage(bgBase, position, 0); bg.drawImage(bgImage, position + picWidth, 0); } private void prepare() { Airplane airplane = new Airplane(); addObject(airplane,75,203); } }
Super_Hippo Super_Hippo

2017/3/27

#
Please use code tags, so you can say in which line you get your error. Change
super(800, 400, 1);
to
super(800, 400, 1, false);
You need to login to post a reply.