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

2012/7/17

Stopping a sound when world changes

1
2
3
gusbus123 gusbus123

2012/7/18

#
@SPower your solution would work if the value that the button must have was sustained throughout the changes of the worlds, but the problem is that only the 2 playing worlds have the 2 button classes which holds the variables. Once the variables are gone there is nothing for the world to check on. that is unless im mistaken of what u are trying to say.
SPower SPower

2012/7/18

#
@gusbus123 I understand. Next time, make a better design by don't let the UI elements hold the data they display: that is how all the UI elements should work.
gusbus123 gusbus123

2012/7/19

#
ok thx guys for the help
gusbus123 gusbus123

2012/7/19

#
ok thx guys for the help EDIT: Double post
gusbus123 gusbus123

2012/7/23

#
@danpost how do u create the secondary world when its through a different actor? this is what i tried in the actor
1
2
Pacman mainworld = (Pacman) getWorld();
Greenfoot.mainworld.setWorld(splay);
it says there is no variable.
just realized the mainworld part should be in the brackets... Edit: ok now all that u said works, but i tried using the same type of code to make it return back to the main world.
1
Pacman main = this;
i first did it with the main = null and in the constructor make the main to be itself and when returning the buttons i used the same things.
1
Greenfoot.setWorld(mainworld.mplay);
but with those two added whenever i click a button in the secondary worlds it says the secondary world cannot cast to the main world. even after adding one of those codes (the ones that saved the secondary worlds onto variables on the main world) a few buttons werent working. the part thats screwing this whole thing up is mainly this code in the other actors.
1
Pacman mainworld = (Pacman) getWorld();
danpost danpost

2012/7/23

#
Start your secondary world as follows, basically (I will call it World2:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
import greenfoot.*;
 
public class World2 extends World
{
    Pacman mainWorld = null;
 
    public World2(Pacman main)
    {
        super(600, 400, 1); // whatever size
        mainWorld = main;
        // etc.
    }
    // etc.
}
You would create the world from an Actor class with:
1
2
Pacman world = (Pacman) getWorld();
Greenfoot.setWorld(new World2(world));
and, you would return to Pacman world with:
1
Greenfoot.setWorld(mainWorld);
If you need to keep the status of World2 in the Pacman world, then you would save the instance of it as follows:
1
2
3
4
5
6
7
World2 world2 = null;
// above is field
// below in Pacman
public void started()
{
    if (world2 == null) world2 = new World2(this);
}
Then in the actor class:
1
2
Pacman pacman = (Pacman) getWorld();
Greenfoot.setWorld(pacman.world2);
gusbus123 gusbus123

2012/7/23

#
would this work with just an actor too?
danpost danpost

2012/7/23

#
1
2
World2 w2 = (World2) getWorld();
Greenfoot.setWorld(w2.mainWorld);
to return from an actor class. If it is the same actor class:
1
2
if (getWorld() instanceof World2) Greenfoot.setWorld(((World2) getWorld()).mainWorld);
else if (getWorld() instanceof Pacman) Greenfoot.setWorld(((Pacman) getWorld()).world2);
gusbus123 gusbus123

2012/7/23

#
hoping im understanding ur code correctly. lol. this code u just said, still saves the whole world as a variable in one of the worlds, am i right? Im trying to avoid saving the whole world, since its just the one/two (im thinking of compressing the two into one class right now if i can do it with this) classes. Right now i still need a way to keep the classes boolean to be saved in the main file so it can be recreated through this with the correct state. Doing this i also need to be able to set the boolean to true when needed before recreating the object (when button state is true). So if the mute is true in the secondary world, it gets recreated with the boolean set to true in the main world, vice versa. With the part of recreating this object with the boolean set to true in the same world i tried this but it kept on saying the variable could not be found:
1
2
Actor mute = new Buttons(6); mute.muteState = true;
getWorld().addObject(mute, 350, 350);
muteState is a boolean within the Buttons.class that is set to false at default. same with the music button, but just muteState is musicState
kiarocks kiarocks

2012/7/23

#
You need to make your Actor a Button instead, like this:
1
2
Buttons mute = new Buttons(6); mute.muteState = true
getWorld().addObject(mute, 350, 350);
danpost danpost

2012/7/23

#
You could just create world constructors to recieve the muteState value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Pacman extends World
{
    boolean muteState = false;
 
    public Pacman()
    {
        super(600, 400, 1); // whatever
        //etc.
    }
 
    public Pacman(boolean muteVal)
    {
        this()
        muteState = muteVal;
    }
 
    // etc.
}
Do the same in the secondary world. Then, you can create new worlds back and forth with the muteState being constant using
1
2
3
Greenfoot.setWorld(new World2(((Pacman) getWorld()).muteState));
// above from Pacman world -- below from World2 world
Greenfoot.setWorld(new Pacman(((World2) getWorld()).muteState));
Use the 'instanceof' if changing worlds at the same place.
danpost danpost

2012/7/23

#
BTW, muteState should really be in the world class as you should only have one instance of that variable. Unless that variable is a class field (static), every button you create will each have its own muteState variable (not really good programming practice). Though, as a static variable, any World, or Actor, sub-class can get its value with 'Button.muteState'. That would negate the need to send it from world to world. Just get its value in the world constructors.
gusbus123 gusbus123

2012/7/24

#
ok ill give them a try.
gusbus123 gusbus123

2012/7/26

#
btw how do u get all actors within a world using the "getWorld().getObjects(.......)" statement?
danpost danpost

2012/7/26

#
getWorld().getObjects(null)
There are more replies on the next page.
1
2
3