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

2013/12/9

Background Scroll

1
2
danpost danpost

2013/12/13

#
At line 44 in your ScrollWereld class (in the 'scroll' method), you are trying to execute a method of a field or object reference that returns 'null' (either the field is not set to any object or a method call is return a null object).
GaspTW GaspTW

2013/12/13

#
public void scroll() { int ds = boot.getX()-560; if (ds > 0) { int scrolledAmt = scroll.changeScroll(ds); for (Object obj : getObjects(null)) { Actor actor = (Actor)obj; actor.setLocation(actor.getX()-scrolledAmt, actor.getY()); } } } } line 44: int ds = boot.getX()-560; I just copied your scroll method and changed buggy to boot
danpost danpost

2013/12/13

#
Do you set 'boot' to any value in your ScrollWereld class constructor?
GaspTW GaspTW

2013/12/13

#
I don't think it does.. My current code in ScrollWereld now is: import greenfoot.*; /** * Write a description of class ScrollWereld here. * * @author (your name) * @version (a version number or a date) */ public class ScrollWereld extends World { Scroll scroll; Boot boot; /** * Constructor for objects of class ScrollWereld. * */ public ScrollWereld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 600, 1, false); GreenfootImage bgScroll = new GreenfootImage("image.png"); scroll = new Scroll(bgScroll, this); spawn(); } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void spawn() { Boot boot = new Boot(); addObject(boot, 106, 303); } public void act() { scroll(); } public void scroll() { int ds = boot.getX()-560; if (ds > 0) { int scrolledAmt = scroll.changeScroll(ds); for (Object obj : getObjects(null)) { Actor actor = (Actor)obj; actor.setLocation(actor.getX()-scrolledAmt, actor.getY()); } } } }
danpost danpost

2013/12/13

#
In your 'spawn' method, remove the first 'Boot' from the first line. With it, you are creating a local field 'boot' that holds a Boot object and setting it to your new Boot object; without it, you are setting your instance class field 'boot' to your new Boot object.
GaspTW GaspTW

2013/12/13

#
It works! Thanks m8! Now I'm trying to set the scroll before it reaches the wall. I just changed int ds = boot.getX()-560; to -260 and it's about 60% of the map before it starts scrolling so perfect to me :) Only thing mentioning my eye is the lagg it obtains when running. Is it because of the big image size? Or how can I make it more fluently?
GaspTW GaspTW

2013/12/13

#
Maybe I have a possible solution. My actor is moving towards the worlds edge now so the backgrounds scrolls. Is it possible to make the background scroll itself so the actor is moving without an described act. My actor uses the 'move' method now. Is that possible, so when I click 'run' the background scrolls automatically, so not when my actor is in edge range?
danpost danpost

2013/12/13

#
To scroll automatically, change the 'scroll' method in your ScrollWereld world class to this:
public void scroll()
{
    int scrolledAmt = scroll.changeScrollX(/* scroll speed */);
    for (Object obj : getObjects(null))
    {
        Actor actor = (Actor)obj;
        // if (actor instanceof Buggy.class && actor instanceof Wheel.class)
        actor.setLocation(actor.getX()-scrolledAmt, actor.getY());
    }
}
It may very well be that the extreme size of your background image is causing the lag.
GaspTW GaspTW

2013/12/16

#
It's not the extreme size of the background. It's 2,25MB I made the file much bigger, same image, quality way higher (12.4MB). Same lag.
danpost danpost

2013/12/16

#
I was referring to the size of the Scroll world (10000x600). Try reducing its size. You could try using multiple Scroll worlds that overlap and change which Scroll world to scroll with as necessary. I have not tried anything like this, yet; but, it should be possible. Also, it should reduce the lag, since the 'current' scrolling image will not be so large. The multiple Scroll worlds could be created at the start:
// instance fields
Scroll[] scrollWorlds = new Scroll[9];
int scrollWorld;
// in your world constructor
GreenfootImage bgScroll = new GreenfootImage("image.png");
for (int n=0; n<9; n++)
{
    GreenfootImage image = new GreenfootImage(2000, 600);
    image.drawImage(bgScroll, -n*1000, 0);
    scrollWorlds[n] = new Scroll(image, this);
}
Then it is just a matter of changing the Scroll world object referenced by 'scroll' at the appropriate times. This may require adding a method to the Scroll class to return the scroll offset value and checking it to determine when the Scroll world needs changed.
GaspTW GaspTW

2013/12/17

#
There are not really any bells ringing. Do you have an email so I can send you my current code including explanations and images I used?
danpost danpost

2013/12/17

#
GaspTW wrote...
There are not really any bells ringing. Do you have an email so I can send you my current code including explanations and images I used?
You can upload it on the site as a 'work in progress' checking the 'Publish source code' checkbox, if you do not mind.
GaspTW GaspTW

2013/12/20

#
It was indeed the size of the background image, I reduced it and works fine now, at least way better. I have another problem now. I use a collision code on this website for transparency pictures so it my actor collides with other actors as soon as a pixel hits. With other words, the code avoids the transparency, so the collision is pixel perfect. My question is, why is it not working. The code itself works perfectly fine in my actor Boat. When it hits a wall it collides as soon as het hits it, not in the transparent border surrounding it. BUT, this code only works in level 1. As soon as level 2 starts it already collides in front of the wall. The code is exactly the same copied, other actor names and variables. The whole code works multiple times but as soon as level 2 starts it won't. I tried to make an second actor named Boat2 with almost the exact methods etc but still no solution. I tried to cut out the images again in Photoshop, but again, with the same method used in level 1 it won't work in level 2. What can I do to fix this?
You need to login to post a reply.
1
2