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

2016/1/14

Accessing another class (inside of class, not world)

1
2
3
4
ObjektnoPR ObjektnoPR

2016/1/14

#
Hello, I am working on a project but I'm having some troubles with accessing of the string "ime" of class "destinacija", from another class called "Bern" (that is subclass from towns ). Here is code from class Bern:
public class Bern extends Mesto
{
    
    public Bern()
    {
        super();
        ime = "Bern";
        setImage("mesto.jpg");
    }

    public void act() 
    {
        Mesto mesto = (Mesto)getOneIntersectingObject(Mesto.class);
        
        //here is suposed to be access code to ime (string inside of destinacija class)

        if(Greenfoot.isKeyDown("down") && isTouching(Letalo.class)){
            if ("Bern" == /* same ime as chosen in destinacija */){
                Greenfoot.playSound("bern.mp3");
            }
            
        } 
    }    
}
And the code from Destinacija class (means destination):
public class Destinacija extends Actor
{
    public void act() {
        //
    }
    
    public void izpisi(){
        
        Start mojsvet = (Start)getWorld();
        String ime = mojsvet.izbranoMesto.ime;
        
        setImage(new GreenfootImage("Destinacija: " + ime , 24, Color.BLACK, Color.WHITE));
    }
       
}
I would really appreciate all the help :)
danpost danpost

2016/1/15

#
Is the Mesto class is a subclass of the Destinacija class? or is there only one Destinacija object in the world that the value of the String "ime" field needs to be gotten from (and not intersecting with the Bern actor)?
ObjektnoPR ObjektnoPR

2016/1/15

#
Subclasses of Actor are: Destinacija, Mesto (subclasses of Mesto are: Bern and 12 others; they are all the same, only names are different) and some other classes. If it would help for me to sort of upload them all, it's not a problem. :)
ObjektnoPR ObjektnoPR

2016/1/15

#
Mesto in my language means Town/City so Bern is a capital of Switzerland (Europe) and there is also 12 other capitals.
danpost danpost

2016/1/15

#
ObjektnoPR wrote...
Subclasses of Actor are: Destinacija, Mesto (subclasses of Mesto are: Bern and 12 others; they are all the same, only names are different) and some other classes. If it would help for me to sort of upload them all, it's not a problem. :)
No -- sorting and upload all those classes is not necessary. If you are to show some code, I would suggest your World subclass called Start. Knowing how 'izbranoMesto' is declared and assigned a value would definitely help. The order of processing within the class could also be helpful. Either could be causing issues in accessing the value.
ObjektnoPR ObjektnoPR

2016/1/15

#
Sure thing. I think I should tell what my project is about, so I would make it easier to understand. It's a game for kids to learn geography. Game randomly choose city (destination), and player need to fly a plane to the chosen destination. Here is a code of a world, called Start.
public class Start extends World
{
    private int nepovratnost = 0;
    ArrayList<Mesto> mesta;
    
    Mesto izbranoMesto;
    
    Destinacija destinacija;
    
    Random rnd = new Random();
   
    public Start()
    {    
        // Create a new world with 1000x600 cells with a cell size of 1x1 pixels.
        super(1000, 600, 1); 
        setBackground(new GreenfootImage("mainMenu.jpg"));
        mesta = new ArrayList<Mesto>();
        
        rnd.setSeed(System.currentTimeMillis());
    }
    
    public void act(){
        if(Greenfoot.isKeyDown("space") && nepovratnost == 0) {
            setBackground(new GreenfootImage("navodila.jpg"));
        }
        
        if(Greenfoot.isKeyDown("backspace") && nepovratnost == 0) {
            setBackground(new GreenfootImage("mainMenu.jpg"));
        }
        
        if(Greenfoot.isKeyDown("escape")) {
            setBackground(new GreenfootImage("escape.jpg"));
            List objects = getObjects(null);
            removeObjects(objects);
        }
        
        if(Greenfoot.isKeyDown("enter") && nepovratnost == 0) {
            nepovratnost = 1;
            
            setBackground(new GreenfootImage("Slovenia.jpg"));

            Ljubljana ljubljana = new Ljubljana();
            addObject(ljubljana, 480, 225);
            mesta.add(ljubljana);
            
            Budimpesta budimpesta = new Budimpesta();
            addObject(budimpesta, 715, 120);
            mesta.add(budimpesta);
            
            Rim rim = new Rim();
            addObject(rim, 390, 520);
            mesta.add(rim);
            
            Bern bern = new Bern();
            addObject(bern, 135, 150);
            mesta.add(bern);
            
            Dunaj dunaj = new Dunaj();
            addObject(dunaj, 560, 70);
            mesta.add(dunaj);
            
            Zagreb zagreb = new Zagreb();
            addObject(zagreb, 550, 250);
            mesta.add(zagreb);
            
            Beograd beograd = new Beograd();
            addObject(beograd, 770, 315);
            mesta.add(beograd);
            
            Sarajevo sarajevo = new Sarajevo();
            addObject(sarajevo, 650, 360);
            mesta.add(sarajevo);
            
            Sofia sofia = new Sofia();
            addObject(sofia, 920, 460);
            mesta.add(sofia);
            
            Tirana tirana = new Tirana();
            addObject(tirana, 745, 540);
            mesta.add(tirana);
            
            Podgorica podgorica = new Podgorica();
            addObject(podgorica, 705, 470);
            mesta.add(podgorica);
            
            Skopje skopje = new Skopje();
            addObject(skopje, 840, 520);
            mesta.add(skopje);
            
            Pristina pristina = new Pristina();
            addObject(pristina, 800, 465);
            mesta.add(pristina);
            
            
            
            
            Letalo potniskoLetalo = new Letalo();
            addObject(potniskoLetalo, 480, 225);
            
            destinacija = new Destinacija();
            addObject (destinacija, 300, 25);
            
            Points points = new Points();
            addObject(points, 75, 25);
            
            izberiMesto();
            
        }
        
    }
 
    public void prepare(){
        
    }   
    
    
    public void izberiMesto()
    {
            int index = rnd.nextInt(mesta.size()-1);
            izbranoMesto = mesta.get(index);
            izbranoMesto.izbrano = true;
            
            destinacija.izpisi();
    }
     
    
}
ObjektnoPR ObjektnoPR

2016/1/15

#
I should also mention: private int nepovratnost Its job is to ensure, that player on touch only get his point once, not all the time of touching objects (game calls act multiple times in a second). ArrayList Mesta stores all cities (subclasses of Mesto), so random choosing is even possible.
ObjektnoPR ObjektnoPR

2016/1/15

#
I could also paste a code from a plane (subclass of actor) that is the moving object in game. Inside of it is the code for choosing different destination when you complete fist one, if that would help. :)
danpost danpost

2016/1/15

#
ObjektnoPR wrote...
I could also paste a code from a plane (subclass of actor) that is the moving object in game. Inside of it is the code for choosing different destination when you complete fist one, if that would help. :)
I already presumed that the player (the plane) was represented by the Letalo class; but, I do not think the code for it is necessary. Let us get some general stuff out of the way, first. For one thing, you are not taking advantage (at all) of how you can make use of subclassing. You have obviously created a separate class for each capital; where, you really only need one general class for capitals. You already have that class -- the Mesto class. You only need to move the code for one city into the Mesto class and generalize the code. The distinguishing factor for the cities is in the 'ime' field. Unless each city is to have different behaviors or states, there is no need for subclassing the Mesto class. As is, all cities have a name, a location in the world, and if it is the chosen destination, will cause the same actions to happen. The only change to the Mesto class is to add a String parameter to the constructor for the value of 'ime'. Then, create Mesto object passing the String value for each city. Another thing, you create a list of all the cities in your Start class.
ArrayList Mesta stores all cities (subclasses of Mesto), so random choosing is even possible.
A list of actors in the world is already hidden within the World class and a list of specific type of actors in the world can be easily gotten by way of the 'getObjects' method. Finally, a city does not need to know if it is the current destination, as the Start world class can keep that information. You can have the city check its world value of 'izbranoMesto' to see if it is currently set to it specifically. Things will be a lot easier to deal with once these changes have been realized. However, for now, try changing the String literal, "Bern", at line 18 of your initial post to the field name 'ime' so that the items compared may actually be the same (instead of two different String objects containing the same set of characters).
ObjektnoPR ObjektnoPR

2016/1/15

#
I have subclasses of Mesto because when a player lands a plane, anthem of that specific country plays. For that I do need subclasses right?
ObjektnoPR ObjektnoPR

2016/1/15

#
And I'm also planning to show a image on landing - image of each city, so I think I need subclasses?
ObjektnoPR ObjektnoPR

2016/1/15

#
And to be exact, why I even need to do all this: I want to play anthem ONLY if player lands in correct city, right now, its playing no matter if you land in a wrong town.
ObjektnoPR ObjektnoPR

2016/1/15

#
danpost wrote...
Things will be a lot easier to deal with once these changes have been realized. However, for now, try changing the String literal, "Bern", at line 18 of your initial post to the field name 'ime' so that the items compared may actually be the same (instead of two different String objects containing the same set of characters).
Like this?
if(Greenfoot.isKeyDown("down") && isTouching(Letalo.class)){
            if ("ime" == "Bern"){
                Greenfoot.playSound("bern.mp3");
            }
            
        } 
(with this code, anthem is not playing)
danpost danpost

2016/1/16

#
Try replacing what you just posted with this: < Code Omitted > No. That will not work as you want. The music will start on multiple acts (similar to the other problem you had). Add an instance field to hold the music that is to play. Assign its value in the constructor of the Mesto class:
// instance field in Mesto class
private GreenfootSound hoverSound;
// in constructor of Mesto class ( 'public Mesto' ) after assigning value of 'ime'
hoverSound = new GreenfootSound(ime.toLowerCase()+".mp3");
Then replace the code above with:
if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && !hoverSound.isPlaying())
{
    hoverSound.play();
}
danpost danpost

2016/1/16

#
ObjektnoPR wrote...
And I'm also planning to show a image on landing - image of each city, so I think I need subclasses?
Again, if all cities are to have these images, then you do not need more than one class. You can load the image into a field just like the sounds. A boolean field can be used to track the state of the image (whether it is showing or not). You can create one extra class for the Actor to show the image (but not one for each city). Actually, a SimpleActor class can be used for them (see my tutorial scenario Value Display Tutorial to see the code and its use).
There are more replies on the next page.
1
2
3
4