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

2015/1/8

Making a loading bar for game, help!!

Stoeptegel Stoeptegel

2015/1/8

#
I have different ships which dock in a harbor. They stand still, and when they're done, the cargo is gone and they go back to the river to leave the map. When they're unloading their cargo, i want to have a "loading bar", which is made up of 4 squares stacked on each other. When all the 4 squares are red , then it's done unloading. The ship will leave, the image will dissappear. But i can't seem to do it. So far i can only spawn the empty loader. I need to be able to change the image (fill the squares) until they are full. I made an actor for the loading bar. But i can't access all the variables and counters i use in the boat class (which i need to trigger an image change) + it NEEDS to work for every individual ship that is docked. Does anyone have an idea how i can access variables and stuff from other actors? And how to make such a thing work? Thanks in advance,
danpost danpost

2015/1/8

#
All my questions seem to require knowledge of what code you currently have in the class of the loading bar. Please post the code for that class (use the 'code' link below the reply box to insert code into your post).
Stoeptegel Stoeptegel

2015/1/8

#
This is the code which i have now in the boat class. This is responsible for docking and undocking. The class for the loader is called unloadCounter. I don't have any code in the unloadCounter class. Because i don't know how i can get the dockTimer from the boat class. And i can't change the image for unloadCounter in the boat class.. As the dockTimer goes up, the image changes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    public void dockBoat() {
    if (this.getY() >= botBorder - 16) {
 
        boatSpeed = 0;
        isDocked = true;
        int boatX = this.getX();
        int boatY = this.getY();
        unloadCounter unloadcounter = new unloadCounter();
        getWorld().addObject(unloadcounter, boatX, boatY);
    if (dockTimer < 500) {
            dockTimer++;
    }
    if (dockTimer == 125) {
        // change image
    }
    if (dockTimer == 250) {
        // change image
    }
    if (dockTimer == 375) {
        // change image
    }
    if (dockTimer == 500) {
        setRotation(270);
        boatSpeed = 1;
        isUnloaded = true;
        isDocked = false;
        // delete image
    }
}
}
danpost danpost

2015/1/8

#
First, if you call that method from the act method of the boat class, you will end up with one unloadcounter on top of another. The boat stops at the location and will continue to be there at that location for a while. Each act cycle will see it is at that location and spawn another counter. You need multiple conditions for spawning a counter and also you need multiple conditions for running the timer:
1
if ( this.getY() >= botBorder-16 && ! isUnloaded && ! isDocked )
might be good for spawning a counter; and
1
if ( ! isUnloaded && isDocked )
might be ok for running the timer. Next, each boat needs to know which unloadcounter belongs to it. An instance object field could be used so each boat can hold a reference to its counter:
1
private unloadCounter unloadcounter;
then, line 8 above can just be this:
1
unloadcounter = new unloadCounter();
Finally, to change the images, you only need to reference the counter and call a method for it to change itself:
1
unloadcounter.nextImage();
Also, removal will be a breeze with this:
1
getWorld().removeObject(unloadcounter);
Stoeptegel Stoeptegel

2015/1/8

#
I will try that! I will post it if it's solved. Thanks Just one question: What are the exclamation marks for in "if ( this.getY() >= botBorder-16 && ! isUnloaded && ! isDocked )"?
danpost danpost

2015/1/8

#
The exclamation marks are boolean operators that negate the value given. The following four lines are equivalent:
1
2
3
4
5
6
7
if ( isDocked == false )
// same as
if ( ! isDocked == true )
// same as
if ( ! isDocked )
// same as
if ( isDocked == ! true )
Stoeptegel Stoeptegel

2015/1/8

#
Everything works! Thanks so much. :) I see you posting on every thread basically, you have my everlasting gratitude for doing that. Cheers
You need to login to post a reply.