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

2017/1/14

Why can't i add a 3th background in scrolling??

Jochends Jochends

2017/1/14

#
Hi, I am looking for this problem now for hours and it must be something easy and stupid.. I got 1 background that i can use 2 times in my scrolling, but when i try it for a 3th time, it doesnt seem to work?? I can scroll from img0 into img1 but than again he returns to img0 and in a loop..., i just want him to run into img0, than img1, than img2, and than again into img0, img1,.....
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    private Background img0, img1, img2;

    // Gebruik maken van een array voor het plaatsen van de X-coördinaten van de bomen
    private int[] GeleBoomX = { 300, 700, 1200, 2000, 3000, 3500, 4000, 4500 }; // de Coordinaten waar de bomen zich gaan bevinden.
    private int[] SteenX = { 340, 600, 870, 1400, 1800, 3400 }; // de Coordinaten waar de stenen zich gaan bevinden.
    private int[] BlockX = { 1000 }; 
    private int[] Monster1X = { 380 };
    private int manX;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        // Create a new world with 1000x400 cells with a cell size of 1x1 pixels. False zorgt ervoor dat het mannetje de rand niet raakt.
        super(1000, 400, 1, false); // True is bounded en false is unbounded

        img0 = new Background(); // maakt een achtergrond aan
        addObject( img0, getWidth()/2, getHeight()/2 ); // hierdoor wordt de image mooi gecentreerd in de wereld

        img1 = new Background(); // maakt een achtergrond aan MAAR aan de rechterkant van de afbeelding die niet zichtbaar is maar gescrollt
        addObject( img1, getWidth() + getWidth()/2 , getHeight()/2 );

        img2 = new Background(); // maakt een achtergrond aan MAAR aan de rechterkant van de afbeelding die niet zichtbaar is maar gescrollt
        addObject( img2, getWidth()*2 + getWidth()/2 , getHeight()/2 );

        addObject(new Hoofdplatform(), 65, 385);
        addObject(new Hoofdplatform(), 193, 385);
        addObject(new Hoofdplatform(), 321, 385);
        addObject(new Hoofdplatform(), 449, 385);
        addObject(new Hoofdplatform(), 577, 385);
        addObject(new Hoofdplatform(), 705, 385);
        addObject(new Hoofdplatform(), 833, 385);
        addObject(new Hoofdplatform(), 961, 385);
        addObject(new Hoofdplatform(), 1089, 385);
        addObject(new Hoofdplatform(), 1217, 385);
        addObject(new Hoofdplatform(), 1345, 385);
        addObject(new Hoofdplatform(), 1473, 385);
        addObject(new Hoofdplatform(), 1601, 385);

        for(int t=0; t<GeleBoomX.length; t++) { // GeleBoom start bij 0 en gaat altijd 1 omhoog( zie lijst bovenaan ). als de boom kleiner is dan de lengte van de lijst blijft hij opbouwen tot hij het einde bereikt.
            addObject(new GeleBoom(), GeleBoomX[t], 331);
        }

        for(int t=0; t<SteenX.length; t++) { // GeleBoom start bij 0 en gaat altijd 1 omhoog( zie lijst bovenaan ). als de boom kleiner is dan de lengte van de lijst blijft hij opbouwen tot hij het einde bereikt.
            addObject(new Steen(), SteenX[t], 357);
        }

        for(int t=0; t<Monster1X.length; t++) { // GeleBoom start bij 0 en gaat altijd 1 omhoog( zie lijst bovenaan ). als de boom kleiner is dan de lengte van de lijst blijft hij opbouwen tot hij het einde bereikt.
            addObject(new Monster1(), Monster1X[t], 347);
        }

        for(int t=0; t<BlockX.length; t++) { // GeleBoom start bij 0 en gaat altijd 1 omhoog( zie lijst bovenaan ). als de boom kleiner is dan de lengte van de lijst blijft hij opbouwen tot hij het einde bereikt.
            addObject(new Block(), BlockX[t], 320);
        }

        addObject(new mannetje(), 80, 346); // Deze moet onderaan staan, zodat het mannetje niet vermenigvuldigd wordt in de methode hierboven. Hierdoor gaat het mannetje VOOR de bomen komen en niet erachter lopen.
    }

    public void act()
    {
        if(!getObjects(mannetje.class).isEmpty()){ // Als de wereld NIET leeg is, gaat het mannetje de getX aanroepen van het mannetje.
            Actor man = getObjects(mannetje.class).get(0);
            manX = man.getX(); 
            if (Greenfoot.isKeyDown("right") && manX > 400) {
                img0.scroll();
                img1.scroll();
                img2.scroll();

                List<GeleBoom> Gelebomen = getObjects(GeleBoom.class);
                for( int t=0; t<Gelebomen.size(); t++) {
                    GeleBoom geleboom = Gelebomen.get(t); // Alles in java = Object
                    geleboom.scroll();
                }

                List<Steen> Stenen = getObjects(Steen.class);
                for( int t=0; t<Stenen.size(); t++) {
                    Steen steen = Stenen.get(t); // Alles in java = Object
                    steen.scroll();
                }

                List<Monster1> Monsters1 = getObjects(Monster1.class);
                for( int t=0; t<Monsters1.size(); t++) {
                    Monster1 monster1 = Monsters1.get(t); // Alles in java = Object
                    monster1.scroll();
                }

                List<Block> Blocken = getObjects(Block.class);
                for( int t=0; t<Blocken.size(); t++) {
                    Block block = Blocken.get(t); // Alles in java = Object
                    block.scroll();
                }
            }
        }
    }
}
I added the following code into my background
    private void scrolling()
    {
        if( getX() < -getImage().getWidth()/2 ) {
            setLocation( getWorld().getWidth() + 295, getY() );
        }
        setLocation( getX()-5, getY() );
    }
danpost danpost

2017/1/14

#
I presume by the placement of the images that they are all the same width of 1000. However, line 4 of the 'scrolling' method only wraps the image by 295, which is an awfully strange value. I would think the image would need to move 3000 pixels to the right -- 1000 to top the adjacent image, 1000 to top the other image and 1000 to cover the next open area.
Jochends Jochends

2017/1/14

#
Hi Danpost, Its only 1 image that follows on another so the background stays the same. I thought the following code would help to let my img1 follow to img2 and add it exactly after the position of img1. This seems to work for img0 to scroll over to img1 but why not to img2? What am i missing?
 private Background img0, img1, img2;
img0 = new Background();
        addObject( img0, getWidth()/2, getHeight()/2 );

        img1 = new Background(); 
        addObject( img1, getWidth() + getWidth()/2 , getHeight()/2 );

        img2 = new Background(); 
        addObject( img2, getWidth()*2 + getWidth()/2 , getHeight()/2 );
  if (Greenfoot.isKeyDown("right") && manX > 400) {
                img0.scroll();
                img1.scroll();
                img2.scroll(); 
danpost danpost

2017/1/14

#
Try changing line 4 in your 'scrolling' method of the Background class to:
move(getImage().getWidth()*3);
Jochends Jochends

2017/1/14

#
Hi Danpost, First of all thanks for helping so far! About the following code
    public void scroll()
    {
    if( getX() < -getImage().getWidth()/2 ) {
        move(getImage().getWidth()*3);
    }
    setLocation( getX()-5, getY() );
    }
the code you said to put inside my background class was inside my Scroller superclass and not background class ( i was wrong.. ). However if i try to add this code into my background class, my other classes can't find the method inside the background because it's not linked to that.
                List<Steen> Stenen = getObjects(Steen.class);
                for( int t=0; t<Stenen.size(); t++) {
                    Steen steen = Stenen.get(t); // Alles in java = Object
                    steen.scroll();
                }
steen.scroll as example he can't find if i switch the code from Scroller superclass to Background class. Would you please be kind to check my greenfoot game and see where the bug is? I made a zip file of it. http://www.filedropper.com/eindwerk30-kopie-kopie
danpost danpost

2017/1/14

#
I am looking into it.
danpost danpost

2017/1/14

#
Okay. You do not have to put a 'scroll' method in all the classes that are scrollers. You only need to put one in the Scroller class:
// add to Scroller class
public void scroll()
{
    setLocation(getX()-5, getY());
}
Now, for the Background objects (which need to wrap around each other), keep the 'scroll' method in that class; then:
// change this line
setLocation( getX()-5, getY() );

// to this
super.scroll();
For other actors that need to wrap (I presume the Hoofdplatform objects may need to wrap), you can add a similar type method as what is in the Background class. And finally, the big one -- let us make use of the Scroller class. Since all scrolling objects are of type Scroller, you can do this:
//replace 'act' method in MyWorld class with this:
public void act()
{
    if (!getObjects(mannetje.class).isEmpty()){
        Actor man = (Actor)getObjects(mannetje.class).get(0);
        manX = man.getX(); 
        if (Greenfoot.isKeyDown("right") && manX > 400) {
            for (Object obj : getObjects(Scroller.class)) {
                ((Scroller)obj).scroll();
            }
        }
    }
}
Jochends Jochends

2017/1/15

#
Thanks alot Danpost! It finally works. Could you at last tell me how it comes that in the code in the 'scroller' superclass i can't change the -5 to 0 ( if i try to do that he keeps running against an invisible wall @ 400x position) I can just stay on the -5 but would like to understand how it comes.
    public void scroll()
    {
        setLocation(getX()-5, getY());
    }
Also, why did you use the
super.scroll
code? What does the super do in this context? Thanks alot for the help! Jochen
danpost danpost

2017/1/15

#
Jochends wrote...
Could you at last tell me how it comes that in the code in the 'scroller' superclass i can't change the -5 to 0 ( if i try to do that he keeps running against an invisible wall @ 400x position) I can just stay on the -5 but would like to understand how it comes.
When you change it to zero, the Scroller objects will not move when told to scroll. Currently the amount of scrolling is a constant -- the '-5'. This can be made to be variable. If you wish to know how -- ask.
why did you use the
super.scroll
code? What does the super do in this context?
The 'super' here tells the compiler that I mean to run the 'scroll' method in the Scroller class. If the 'super' was not used here, the method being executed in the subclass (with the same name) would be called instead, again (and again, and again). That will quickly create a StackOverflowException error. Anytime a subroutine is called (a method or a constructor), the instruction pointer is saved on a stack before branching to the subroutine so execution can return to that point when the subroutine is done. Only a certain amount of memory is devoted to the stack and if the pointer exceeds its allocated memory, this error will ensue.
Jochends Jochends

2017/1/16

#
Thanks! I look further into it
Jochends Jochends

2017/1/17

#
It looks like my scroll works perfectly fine now! Thanks for that! The scroll itself is fine, but i got 1 last problem about putting an object in it on the Y coördinate. With this code i can put my object INSIDE my scrolling on the X-coördinate without any problems.
    private int[] GeleBoomX = { 300, 700, 1200, 2000, }; // de Coordinaten op de X-as waar de bomen zich gaan bevinden die IN de scroll zitten.
    private int[] SteenX = { 340, 600, 870, 1402 };
    private int[] BlockX = { 1300 }; 
    private int[] Monster1X = { 380 };
    private int[] PlatformX = { 1450, 1578, 1706, };
    private int[] PlatformblockX = { 1815, 1905 };
With the following code i can put my object also INSIDE my scrolling world on the Y-Coördinate '357' and on top of the 4th 'steen' with the '(t==3), so that is fine also. But with what code can i put an object on a RANDOM Y-coördinate INSIDE my scrolling?
        for(int t=0; t<SteenX.length; t++) { // GeleBoom start bij 0 en gaat altijd 1 omhoog( zie lijst bovenaan ). als de boom kleiner is dan de lengte van de lijst blijft hij opbouwen tot hij het einde bereikt.
            addObject(new Steen(), SteenX[t], 357);
            if (t==3) {
                addObject(new Steen(), SteenX[t], 325);
            }
        }
                List<Steen> Stenen = getObjects(Steen.class);
                for( int t=0; t<Stenen.size(); t++) {
                    Steen steen = Stenen.get(t); // Alles in java = Object
                    steen.scroll();
                }
With this code i can only put the 'Steen' on an 'X and Y' coördinate that is inside my screen '1000 pixels on 400 pixels, and not in the scrolling( outside the 1000 pixels ).
addObject(new Steen(), 1500, 385);
danpost danpost

2017/1/17

#
Jochends wrote...
With this code i can only put the 'Steen' on an 'X and Y' coördinate that is inside my screen '1000 pixels on 400 pixels, and not in the scrolling( outside the 1000 pixels ).
addObject(new Steen(), 1500, 385);
You have an unbounded world -- you can put any object anywhere regardless of whether on-screen or not.
i can put my object also INSIDE my scrolling world on the Y-Coördinate '357' and on top of the 4th 'steen' with the '(t==3), so that is fine also. But with what code can i put an object on a RANDOM Y-coördinate INSIDE my scrolling?
You can get a random number using the Greenfoot class method 'getRandomNumber.'. For example:
addObject(new Steen(), SteenX[t], 100+Greenfoot.getRandomNumber(258));
See the documentation on the method here.
You need to login to post a reply.