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
ObjektnoPR ObjektnoPR

2016/1/18

#
Sorry i am not near my PC at the moment, i will try this code tomorrow.:) I would need only one more thing: I would like to fix destination, so the current city can't be chosen as the next destination.
danpost danpost

2016/1/18

#
ObjektnoPR wrote...
I would need only one more thing: I would like to fix destination, so the current city can't be chosen as the next destination.
With the array as given above, the cities will actually be the exact same cities added into your world, even after being removed (they will not be different instances of the same cities). Therefore, you can check the last destination with the possible new one to see if they are actually different or not.
ObjektnoPR ObjektnoPR

2016/1/18

#
Now I get quite a few errors. When creating private Object mesta {...} it say that variable is already used in World class somewhere else so I renamed it to private Object lokacijaMest = {...}. Its okay to put that directly in Start(world) class, not inside of some method if it? When I did this, I'm still having problems with for expression.
for (int i=0; i<lokacijaMest.length; i++){
                addObject((Actor)mesta[i][0], (Integer)mesta[i][1], (Integer)mesta[i][2]);
            }
It underline every
[i] 
saying array expected, arrayList found.
ObjektnoPR ObjektnoPR

2016/1/18

#
Here is the complete Start 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());
    }
    
    private Object[][] lokacijaMest = {
        { 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 }
    };
    
    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"));

            for (int i=0; i<lokacijaMest.length; i++){
                addObject((Actor)mesta[i][0], (Integer)mesta[i][1], (Integer)mesta[i][2]);
            }
            
            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();
    }

}
danpost danpost

2016/1/18

#
ObjektnoPR wrote...
Now I get quite a few errors. When creating private Object mesta {...} it say that variable is already used in World class somewhere else so I renamed it to private Object lokacijaMest = {...}. Its okay to put that directly in Start(world) class, not inside of some method if it? When I did this, I'm still having problems with for expression.
for (int i=0; i<lokacijaMest.length; i++){
                addObject((Actor)mesta[i][0], (Integer)mesta[i][1], (Integer)mesta[i][2]);
            }
It underline every
[i] 
saying array expected, arrayList found.
It is underlining them because you change the name elsewhere -- but not in that line. This array I supplied is to replace your 'mesta' array list -- it was not intended that both exist together. The final code I came up with was this:
import greenfoot.*;
import java.util.Random.*;

public class Start extends World
{
    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 }
    };
    private int nepovratnost;
    Mesto izbranoMesto;
    Destinacija destinacija = new Destinacija();
    Points points = new Points();
    Random rnd = new Random();
    
    public Start()
    {
        super(1000, 600, 1);
        setBackground(new GreenfootImage("mainMenu.jpg"));
        rnd.setSeed(System.currentTimeMillis());
    }
    
    public void act()
    {
        if (nepovratnost == 0)
        {
            if (Greenfoot.isKeyDown("space"))
            {
                setBackground(new GreenfootImage("navodila.jpg"));
            }
            if (Greenfoot.isKeyDown("backspace"))
            {
                setBackground(new GreenfootImage("mainMenu.jpg"));
            }
            if (Greenfoot.isKeyDown("enter"))
            {
                nepovratnost = 1;
                setBackground(new GreenfootImage("Slovenia.jpg"));
                for (int i=0; i<mesta.length; i++)
                {
                    addObject((Actor)mesta[i][0], (Integer)mesta[i][1], (Integer)mesta[i][2]);
                }                
                addObject(destinacija, 300, 25);
                addObject(points, 75, 25);
                izberiMesto();
            }
        }

        if (Greenfoot.isKeyDown("escape"))
        {
            setBackground(new GreenfootImage("escape.jpg"));
            removeObjects(getObjects(null));
        }
    }
    
    public void izberiMesto()
    {
        Mesto mesto = izbranoMesto; // local variable for possible new destination
        while (mesto == izbranoMesto) // loop as long as possible is same as old destination
        {
            mesto = (Mesto)mesta[rnd.nextInt(mesta.length)][0];
        }
        izbranoMesto = mesto;
        destinacija.izpisi();
    }
}
ObjektnoPR ObjektnoPR

2016/1/18

#
You done some nice changes, code looks a lot more readable (I hope that is legit English word). Only thing missing is adding the plane into Start :P
ObjektnoPR ObjektnoPR

2016/1/18

#
And now we have problem with this new code. I only added the plane to the last code that you posted. So now points are not working correctly (no matter if you land in a wrong or right city, it will be -5 points, but if you land outside the city you will get -2 points, so that part is okay). Also when you land, destination is not changing.
danpost danpost

2016/1/18

#
You have yet to post the code to the class for your plane. And you just need to call the 'izberiMesto' method of the Start class when a destination is reached.
ObjektnoPR ObjektnoPR

2016/1/18

#
Code of the plane:
public class Letalo extends Actor
{
    public int moving = 0; 
    int dotik = 0;
    
    public Letalo(){
        setImage("tinyPlane.jpg");
    }
    
    public void act()
    {
        
        if(Greenfoot.isKeyDown("up")){
            moving ++; 
            dotik = 0;
        }
        
        if(moving > 0){
            move(2);
        }
                
        if(Greenfoot.isKeyDown("left")){
            turn(-3);
        }
        
        if(Greenfoot.isKeyDown("right")){
            turn(3);
        }
        
        if(isAtEdge()){
            turn(10);
        }
        
        if(Greenfoot.isKeyDown("down")){
            moving = 0;
        }
        
        //pravo mesto (right city)
        if(Greenfoot.isKeyDown("down") && isTouching(Mesto.class) && dotik==0){
            Mesto mesto = (Mesto)getOneIntersectingObject(Mesto.class);
            
            if(mesto.izbrano == true){
                ((Points)getWorld().getObjects(Points.class).get(0)).points += 10;
                //removeTouching(Mesto.class);
                dotik = 1;
                
                //menja mesta ob pristanku
                Start start = (Start)getWorld();
                start.izberiMesto();
            }
        } 
        
        //napačno mesto (wrong city)
        if(Greenfoot.isKeyDown("down") && isTouching(Mesto.class) && dotik==0){
            Mesto mesto = (Mesto)getOneIntersectingObject(Mesto.class);
            
            if(mesto.izbrano == false){
                ((Points)getWorld().getObjects(Points.class).get(0)).points -= 5;
                //removeTouching(Mesto.class);
                dotik = 1;
            }
        }

        //izven mesta (outside any city)
        if(Greenfoot.isKeyDown("down") && !isTouching(Mesto.class) && dotik==0){
            ((Points)getWorld().getObjects(Points.class).get(0)).points -= 2;
            dotik = 1;
            
        }
    }    
}
I call "izberi mesto" from a plane (Letalo) class, should I call it from Start() instead?
danpost danpost

2016/1/18

#
ObjektnoPR wrote...
I call "izberi mesto" from a plane (Letalo) class, should I call it from Start() instead?
You detect landing from the Mesto class. That is where you call the method from. Wait -- it appears you are trying to detect city landings in both the city and the plane classes. This should only be done in one of the two places. The only situation not covered in the Mesto class is when landing where there is no city. The adjusting of points for landing in a city, right or wrong, can be moved to the Mesto class. Also, with my revised code, there is a reference to the Points object, so you can obtain the object with the following:
((Start)getWorld()).points
and you can adjust the value (for right city, in this case) this way:
((Start)getWorld()).points.points += 10;
This means you can remove lines 38 through 62 from the Letalo class. The last part, lines 65 through 69 (for landing outside) can be moved to your 'down' key check. Also, you are using 'moving' like a boolean field (using its value as being zero or greater than zero, where its actual value is of no consequence). The field also seems to coincide with the value of the 'dotik' field -- which means one can be eliminated. I came up with this for the class:
public class Letalo extends Actor
{
    boolean moving = true; 
     
    public Letalo(){
        setImage("tinyPlane.jpg");
    }
     
    public void act()
    {
        if (Greenfoot.isKeyDown("up")){
            moving = true; 
        }
        if (moving){
            move(2);
        }   
        if (Greenfoot.isKeyDown("left")){
            turn(-3);
        }
        if (Greenfoot.isKeyDown("right")){
            turn(3);
        }
        if (isAtEdge()){
            turn(10);
        }
        if (Greenfoot.isKeyDown("down")){
            if (moving && !isTouching(Mesto.class)){
                ((Start)getWorld()).points.points -= 2;
            }
            moving = false;
        }
    }    
}
Again, adjusting of points when landing at any city and changing destinations can be done in the Mesto class.
ObjektnoPR ObjektnoPR

2016/1/18

#
Code from Mesto: (I added wrong city code, and calling izberiMesto() method if landing in the right one)
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);
            ((Start)getWorld()).points.points += 10;
        }
        
        if (! hoverSound.isPlaying() && cityImage.getWorld() != null) {
            getWorld().removeObject(cityImage);
            ((Start)getWorld()).izberiMesto();
        }
        
        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto != this){
            ((Start)getWorld()).points.points -= 5;
        }
        
    }    
}
Now even if I have "-=5 points" it takes away 10 points if you land in the wrong city.. I can't figure out why
ObjektnoPR ObjektnoPR

2016/1/18

#
I think I would rather stick with moving 0 or 1 (not Boolean true/false), because now you can spam points when you are landed and if you are pressing down key points are adding every time act() is called.
ObjektnoPR ObjektnoPR

2016/1/18

#
+ it started showing "Exception in thread "AWT-EventQueue-0" java.lang.OutOfMemoryError: Java heap space java.lang.OutOfMemoryError: Java heap space" in debugger
ObjektnoPR ObjektnoPR

2016/1/18

#
My code from Mesto:
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"); 
        
        //slika ki jo vidimo na zamljevidu
        setImage("mesto.jpg");
        
        //objekt (ki je samo slika) ki se prikaže cez cel ekran ČE in KO se predvaja zvok
        cityImage = new SimpleActor();
        cityImage.setImage(ime + ".jpg");

    }
    
    public void act(){
        //pravilno mesto
        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto == this && !hoverSound.isPlaying()){
            hoverSound.play();
            //objekt dodamo v svet, na sredino, čez cel ekran
            getWorld().addObject(cityImage, 500, 300);
            ((Start)getWorld()).points.points += 10;
        }
        //ko se himna konča
        if (! hoverSound.isPlaying() && cityImage.getWorld() != null) {
            getWorld().removeObject(cityImage);
            ((Start)getWorld()).izberiMesto();
        }
        //pristanek v napačnem mestu
        if (Greenfoot.isKeyDown("down") && isTouching(Letalo.class) && ((Start)getWorld()).izbranoMesto != this){
            //moving = 0  I NEED HELP WITH THAT ONE
            ((Start)getWorld()).points.points -= 5;
        }
        
    }    
}
I would need to set moving (int from letalo class) to 0. Plese help me with this. I believe if I fix that, then spaming points wont be possible anymore :)
ObjektnoPR ObjektnoPR

2016/1/18

#
public class Letalo extends Actor
{
    public int moving = 0; 
      
    public Letalo(){
        setImage("tinyPlane.jpg");
    }
      
    public void act()
    {
        if (Greenfoot.isKeyDown("up")){
            moving ++; 
        }
        if (moving > 0){
            move(2);
        }   
        if (Greenfoot.isKeyDown("left")){
            turn(-3);
        }
        if (Greenfoot.isKeyDown("right")){
            turn(3);
        }
        if (isAtEdge()){
            turn(10);
        }
        if (Greenfoot.isKeyDown("down")){
            if (moving > 0 && !isTouching(Mesto.class)){
                ((Start)getWorld()).points.points -= 2;
            }
            moving = 0;
        }
    }    
}
Letalo code would help I believe :)
There are more replies on the next page.
1
2
3
4
5