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
5
danpost danpost

2016/1/16

#
ObjektnoPR wrote...
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?
I should have done this one first. No. you do not need subclasses for that. See two posts up.
ObjektnoPR ObjektnoPR

2016/1/16

#
Thank you for all you help I really appreciate it.:) So, now I deleted all subclasses of Mesto and changed Mesto's code to:
public class Mesto extends Actor
{
    public boolean izbrano = false;
    
    public String ime;
    
    ArrayList<Mesto> allmesta;
    
    private GreenfootSound hoverSound;
    
    public void Mesto(){
        //super();
        allmesta = new ArrayList<Mesto>();
        String[] ime = {"Bern", "Tirana", "Budimpesta", "Sofia", "Pristina", "Podgorica", "Ljubljana", "Skopje", "Dunaj", "Zagreb", "Rim", "Beograd", "Sarajevo"}; 
 
        hoverSound = new GreenfootSound(ime.toLowerCase()+".mp3");

        setImage("mesto.jpg");
    }
    
    public void act(){

        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && !hoverSound.isPlaying()){
            hoverSound.play();
        }


    }    
}
It shows an error at .toLowerCase method.
hoverSound = new GreenfootSound(ime.toLowerCase()+".mp3");
Also I'm getting an error for each city constructor in World (Start) class.
Ljubljana ljubljana = new Ljubljana();
addObject(ljubljana, 480, 225);
mesta.add(ljubljana);
Did I make the array the right way? And how to fix that constructors?
ObjektnoPR ObjektnoPR

2016/1/16

#
I see, I forgot to actually make a objects from that ArrayList.. But I could use help with that.
danpost danpost

2016/1/16

#
You do not need to use any Array or List objects. You just need to create Mesto objects, passing them their String parameters for their values of 'ime':
// change the 'public Mesto()' constructor to
public Mesto(String imeValue)
{
    ime = imeValue;
    hoverSound = new GreenfootSound(ime.toLowerCase()+".mp3");
    setImage("mesto.jpg");
}
Then in your world class, create the different cities like this:
Mesto bern = new Mesto("Bern");
ObjektnoPR ObjektnoPR

2016/1/16

#
My mesto class now looks like this (and I think it works okay):
public class Mesto extends Actor
{
    public boolean izbrano = false;
    
    public String ime;
    
    private GreenfootSound hoverSound;
    
    public void Mesto(String imeValue){
        ime = imeValue;
        //String[] ime = {"Bern", "Tirana", "Budimpesta", "Sofija", "Pristina", "Podgorica", "Ljubljana", "Skopje", "Dunaj", "Zagreb", "Rim", "Beograd", "Sarajevo"};

        hoverSound = new GreenfootSound(ime + ".mp3"); //i remover toLowerCase method, and I renamed sound files
        //hoverBakground = new (ime+".jpg"); (i need new background, not image, sorry for not being specific enough)

        setImage("mesto.jpg");
    }
    
    public void act(){

        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && !hoverSound.isPlaying()){
            hoverSound.play();
        }


    }    
}
But creating cities is not working.. it says:
constructor Mesto in class Mesto cannot be applied to given types: required: no arguments found: javaj.lang.String reason: actual and formal argument list differ in length
ObjektnoPR ObjektnoPR

2016/1/16

#
And for now we can just ignore that background change.. I would like to make that sound playing first
danpost danpost

2016/1/16

#
You changed your constructor into a 'void' method. Remove the word 'void' from line 9 above.
ObjektnoPR ObjektnoPR

2016/1/16

#
Ok now the constructor works, but the sound still plays even if you land in the wrong city.
danpost danpost

2016/1/16

#
ObjektnoPR wrote...
Ok now the constructor works, but the sound still plays even if you land in the wrong city.
Okay, one more condition needs to be applied to line 21 above. Change it to:
if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto == this && !hoverSound.isPlaying()){
ObjektnoPR ObjektnoPR

2016/1/16

#
Thank you so much it works now :D So if you don't mind, I could use some help at displaying a new background. When a plane lands, sound plays (thanks to you) and I would also like it to display a new background for the time when the emblem plays. Is that possible? (diffrent image for every capital of course) I already named that images the same as the sound files, only the file extension is jpg. GreenfootSound works, but when I tried to do GreenfootBackground it doesn't recognize it.
danpost danpost

2016/1/16

#
There is no such class as GreenfootBackground. There is a GreenfootImage class that holds images.
ObjektnoPR ObjektnoPR

2016/1/16

#
Any suggestions on how could I solve this? Is it possible to display image for the time when sound is playing?
ObjektnoPR ObjektnoPR

2016/1/16

#
I could also make image smaller (half of screen or so) and I could maybe only display an image in the middle of the screen...
danpost danpost

2016/1/16

#
ObjektnoPR wrote...
Any suggestions on how could I solve this? Is it possible to display image for the time when sound is playing?
Yes. Instead of holding the image itself in a field within the class, hold the actor that displays the image in a field. Then, you can add and remove it as needed. The actors can be created and their images set to them during the construction of the Mesto objects. You already know when you want to add it into the world (when the sound is started); you can check for the sound not playing (and if the image actor is in the world) to remove it.
I could also make image smaller (half of screen or so) and I could maybe only display an image in the middle of the screen...
Whatever you want.
ObjektnoPR ObjektnoPR

2016/1/16

#
I should make subclass of actor, not of Mesto, right?
There are more replies on the next page.
1
2
3
4
5