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 should make subclass of actor, not of Mesto, right?
For the actor to display the image of the cities, yes. You can use a simple class like the following (which is the entire class code):
public class SimpleActor extends greenfoot.Actor {}
In the constructor of the Mesto class, create a SimpleActor object, set its image to the image of the city that Mesto object represents and assign it to the field that is to hold the image actor.
ObjektnoPR ObjektnoPR

2016/1/16

#
I created empty subclass or Actor, named SimpleActor as you tolde me to above. This is only part of the code form Mesto, it's constructor.
public Mesto(String imeValue){
        ime = imeValue;

        hoverSound = new GreenfootSound(ime + ".mp3"); 
        
        setImage("mesto.jpg");
        
        //the code for image showing
        /*
        
        if (Greenfoot.soundIsPlaying){
            SimpleActor bern = new SimpleActor("Bern");
            getWorld().setBackground(new GreenfootImage(ime + ".jpg")); 
        }
        
        if (Greenfoot.!soundIsPlaying){
            getWorld().setBackground("Slovenia.jpg")); //that is the old background 
        }
        
        */
    }
What is the method for checking if any sound is playing?
danpost danpost

2016/1/16

#
ObjektnoPR wrote...
This is only part of the code form Mesto, it's constructor.
public Mesto(String imeValue){
        ime = imeValue;

        hoverSound = new GreenfootSound(ime + ".mp3"); 
        
        setImage("mesto.jpg");
        
        //the code for image showing
        
    }
You should have more in the constructor at this point than that. I do not see any code creating the image object and the actor, setting the image to the actor and saving said actor in a field (the code for 'image showing' as commented should be associated with the 'act' method -- not the constructor).
What is the method for checking if any sound is playing?
If you had check the GreenfootSound class (or reviewed the code already in this discussion thread, you would see the 'isPlaying' method which returns true if the GreenfootSound object the method is called on is already playing (see the 'if' statement we were working with).
danpost danpost

2016/1/16

#
To further explain about sound, you cannot control or check the state of any sound created by calling the static 'playSound' method of the Greenfoot class. You cannot control it in any way when starting the sound in this manner because there is no way to refer to that sound object created. When you create a GreenfootSound object yourself, 'GreenfootSound sound = new GreenfootSound("soundfilename.mp3");', you can then use the non-static methods of the GreenfootSound class on the reference variable, 'sound', to control and make checks on that GreenfootSound instance (object created from the class).
ObjektnoPR ObjektnoPR

2016/1/17

#
I totally forgot about that method, I am so sorry. I believe I fixed it now.
public class Mesto extends Actor
{
    public boolean izbrano = false;
    
    public String ime;
    
    private GreenfootSound hoverSound;
    
    private GreenfootImage hoverImage; 
    
    SimpleActor simpleActor = new SimpleActor();

    public Mesto(String imeValue){
        ime = imeValue;

        hoverSound = new GreenfootSound(ime + ".mp3"); 
        
        setImage("mesto.jpg");

    }
    
    public void act(){

        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto == this && !hoverSound.isPlaying()){
            hoverSound.play();
        }
        
        if (hoverSound.isPlaying() == true) {
            simpleActor.setImage(ime + ".jpg");
            getWorld().addObject(simpleActor, 500, 300);
        }
        else {
            getWorld().removeObject(simpleActor);
        }
        
    }    
}
Music is now playing correctly, correct image shows for the time that music plays, after that, object is removed. Now I have new problem, points are not working anymore. (destination don't change at landing and points are not changing) Also for the time that image is showing, plane is flaying under image, so when SimpleActor is removed plane is already flying around.
danpost danpost

2016/1/17

#
Okay -- let us clean up what you have so far before working on the plane flying around issue. You can remove line 3 (the destination city is held by the World object). You can remove line 9 ('hoverImage' is not being used anywhere in the code). You can rename the SimpleActor field from 'simpleActor' to something more descriptive -- like 'mestoImage', for example. This field can be set as 'private' since it will only be accessed from within the class. Its value can be set in the constructor of the class and the image of the actor can also be set in the constructor. You can, after the last adjustment to the code, move line 30 to after line 25. The actor only needs to be added to the world once -- at the time that the sound is started. You can change line 32:
// from
else {
// to
else if (simpleActor.getWorld() != null) {
This will not change how the scenario visually runs; but, it does prevent the action of constantly trying to remove the actor from the list of objects in the world when the sound is not playing.
ObjektnoPR ObjektnoPR

2016/1/17

#
If I remove line 3, my izbrano Boolean, other classes stop working and it doesn't even compile, i don't think i can remove that. I agree on deleting line 9, i didn't see that, thank you :) And i will rename classes and actors at very end, because i think its a lot easier for you to help me that way, but in the end I must change all of them in my language, but if i will be careful not to forget anything i don't think that should be the problem. Here are the changes:
public class Mesto extends Actor
{
    public boolean izbrano = false;
    
    public String ime;
    
    private GreenfootSound hoverSound;
    
    private SimpleActor cityImage; 

    public Mesto(String imeValue){
        ime = imeValue;

        hoverSound = new GreenfootSound(ime + ".mp3"); 
        
        setImage("mesto.jpg");
        
        cityImage = new SimpleActor();

    }
    
    public void act(){

        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto == this && !hoverSound.isPlaying()){
            hoverSound.play();
            getWorld().addObject(cityImage, 500, 300);
        }
        
        if (hoverSound.isPlaying() == true) {
            cityImage.setImage(ime + ".jpg");
        }
        else if (cityImage.getWorld() != null) {
            getWorld().removeObject(cityImage);
        }
        
    }    
}
Problem: points are not working since i included that image showing and also destination is not changing
ObjektnoPR ObjektnoPR

2016/1/17

#
And to be exact.. i just figured if i press the key down slowly (or maybe normally for some people) then it works (points and new destination), but if i press it really shortly, then it doesn't work.
ObjektnoPR ObjektnoPR

2016/1/17

#
And after fixing that.. there will be only 1 more thing that could use some changes. After you land, and new destination is chosen, there is a possibility, that the same city (where you are at the moment) will be chosen as a next destination. How could i make a loop that will be checking if new destination == our current city and if that is true, then it will recall public void izberiMesto() method from world Start, which function is to chose new destination, when we complete the old one.
danpost danpost

2016/1/17

#
Move line 30 to line 19 (once the image is set for that actor, it will not need to be set again). After that move, you can then replace the lines numbered 29 through 34 with the following:
if (! hoverSound.isPlaying() && cityImage.getWorld() != null) {
    getWorld().removeObject(cityImage);
}
You should probably change the destination city at the same time you start the sound and add the city image into the world. You could call your 'izberiMesto' method in the Start class there. That method, however, should look nothing like what was shown earlier in this discussion. You have not yet shown the changes made to the Start class. Posting its revised code will be helpful. As far as the izbrano boolean field -- your other classes should be using the value of the Start class izbranoMesto field. It is so much easier to control the value of one field as opposed to more than a dozen.
ObjektnoPR ObjektnoPR

2016/1/17

#
There was not really much changing inside of Start World, i only changed constructors for each city, here is the code.
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"));

            Mesto ljubljana = new Mesto("Ljubljana");
            addObject(ljubljana, 480, 225);
            mesta.add(ljubljana);
            
            Mesto budimpesta = new Mesto("Budimpesta");
            addObject(budimpesta, 715, 120);
            mesta.add(budimpesta);
            
            Mesto rim = new Mesto("Rim");
            addObject(rim, 390, 520);
            mesta.add(rim);
            
            Mesto bern = new Mesto("Bern");
            addObject(bern, 135, 150);
            mesta.add(bern);
            
            Mesto dunaj = new Mesto("Dunaj");
            addObject(dunaj, 560, 70);
            mesta.add(dunaj);
            
            Mesto zagreb = new Mesto("Zagreb");
            addObject(zagreb, 550, 250);
            mesta.add(zagreb);
            
            Mesto beograd = new Mesto("Beograd");
            addObject(beograd, 770, 315);
            mesta.add(beograd);
            
            Mesto sarajevo = new Mesto("Sarajevo");
            addObject(sarajevo, 650, 360);
            mesta.add(sarajevo);
            
            Mesto sofija = new Mesto("Sofija");
            addObject(sofija, 920, 460);
            mesta.add(sofija);
            
            Mesto tirana = new Mesto("Tirana");
            addObject(tirana, 745, 540);
            mesta.add(tirana);
            
            Mesto podgorica = new Mesto("Podgorica");
            addObject(podgorica, 705, 470);
            mesta.add(podgorica);
            
            Mesto skopje = new Mesto("Skopje");
            addObject(skopje, 840, 520);
            mesta.add(skopje);
            
            Mesto pristina = new Mesto("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/17

#
And the code of Mesto with new changes:
public class Mesto extends Actor
{
    public boolean izbrano = false;
    
    public String ime;
    
    private GreenfootSound hoverSound;
    
    private SimpleActor cityImage; 

    public Mesto(String imeValue){
        ime = imeValue;

        hoverSound = new GreenfootSound(ime + ".mp3"); 
        
        setImage("mesto.jpg");
        
        cityImage = new SimpleActor();
        cityImage.setImage(ime + ".jpg");

    }
    
    public void act(){

        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto == this && !hoverSound.isPlaying()){
            hoverSound.play();
            getWorld().addObject(cityImage, 500, 300);
        }
        
        if (! hoverSound.isPlaying() && cityImage.getWorld() != null) {
            getWorld().removeObject(cityImage);
        }

    }    
}
ObjektnoPR ObjektnoPR

2016/1/17

#
Also, last changes fixed issue with points not working and destination not changing. :)
ObjektnoPR ObjektnoPR

2016/1/17

#
Could I also move the code for creating new cities to prepare(), they also don't need to be called every second. I would leave only lines as:
addObject(bern, 135, 150);
inside of act() ?
danpost danpost

2016/1/17

#
ObjektnoPR wrote...
Could I also move the code for creating new cities to prepare(), they also don't need to be called every second. I would leave only lines as:
addObject(bern, 135, 150);
inside of act() ?
If you do that, you could run into difficulties when adding them into the world (linking the correct coordinates with each city). Better might be to create an array to hold the information (cities and coordinates):
private Object[][] mesta = 
{
    { new Mesto("Ljubljana"), 480, 225 },
    { new Mesto("Budimpesta"), 715, 120 },
    { new Mesto("Rim"), 390, 520 },
    { new Mesto("Bern"), 135, 150 },
    { new Mesto("Dunaj"), 560, 70 },
    { new Mesto("Zagreb"), 550, 250 },
    { new Mesto("Beograd"), 770, 315 },
    { new Mesto("Sarajevo"), 650, 360 },
    { new Mesto("Sofija"), 920, 460 },
    { new Mesto("Tirana"), 745, 540 },
    { new Mesto("Podgorica"), 705, 470 },
    { new Mesto("Skopje"), 840, 520  },
    { new Mesto("Pristina"), 800, 465 }
};
You can then add the cities into the world with code like this:
for (int i=0; i<mesta.length; i++)
{
    addObject((Actor)mesta[i][0], (Integer)mesta[i][1], (Integer)mesta[i][2]);
}
There are more replies on the next page.
1
2
3
4
5