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

2014/6/15

Europe Map

1
2
Sbaliyev Sbaliyev

2014/6/15

#
Well as I said, Iam busy with a project. I need to program a europe map with all 28 capitals, greenfoot should ask: where is Amsterdam? And when the user clicks on Amsterdam it should say Good or False. I made 28 new objects in world
addObject(new Locatie(), 297, 354); //Amsterdam
So.. how is greenfoot suppose to know if the user clicked the good object?
danpost danpost

2014/6/15

#
How you have things organized and working plays an important part in how you would code this. You should show codes for Locatie class and your subclass of World (at least).
Sbaliyev Sbaliyev

2014/6/15

#
public class Achtergrond extends World
{
    public Achtergrond()
    {    
        super(980, 785, 1); 
        populateWorld();
    }
    
    public void populateWorld()
    {
        addObject(new Locatie(), 297, 354); //Amsterdam
        addObject(new Locatie(), 148, 310); //Dublin 
        addObject(new Locatie(), 221, 363); //Londen
        addObject(new Locatie(), 288, 395); //Brussel
        addObject(new Locatie(), 243, 442); //Parijs
        addObject(new Locatie(), 306, 419); //Luxemburg
        addObject(new Locatie(), 317, 492); //Bern
        addObject(new Locatie(), 525, 340); //Warschau
        addObject(new Locatie(), 414, 353); //Berlijn
        addObject(new Locatie(), 20, 635);  //Lissabon
        addObject(new Locatie(), 117, 616); //Madrid
        addObject(new Locatie(), 378, 592); //Rome
        addObject(new Locatie(), 565, 288); //Vilnius, Litouwen
        addObject(new Locatie(), 535, 243); //Riga, Letland
        addObject(new Locatie(), 572, 200); //Tallinn, Estland 
        addObject(new Locatie(), 476, 517); //Zagreb, Kroatie
        addObject(new Locatie(), 444, 510); //Ljublana, Slovenie
        addObject(new Locatie(), 515, 475); //Budapest, Hongarije
        addObject(new Locatie(), 492, 448); //Bratislava, Slowakije
        addObject(new Locatie(), 461, 461); //Wenen, Oostenrijk
        addObject(new Locatie(), 446, 427); //Praag, Tsjechie
        addObject(new Locatie(), 613, 503); //Boekarest, Roemenie
        addObject(new Locatie(), 625, 582); //Sofia, Bulgarije
        addObject(new Locatie(), 581, 646); //Griekenland
        addObject(new Locatie(), 792, 715); //Cyprus
        addObject(new Locatie(), 417, 753); //Malta
        addObject(new Locatie(), 555, 151); //Finland
        addObject(new Locatie(), 433, 213); //Zweden
    }
    
    public void act()
    {
        MouseChange.setImage();
    }
}
public class Locatie extends Actor  
{  
    private boolean onThis = false;    
      
    public void act()   
    {  
        if(Greenfoot.mouseMoved(null)){  
            onThis = Greenfoot.mouseMoved(this);    
        }  
        if(onThis)  {  
            setImage("locatieRood.png");    
        }  
        else  {  
            setImage("locatieZwart.png");     
        }  
    }      
}  
It's not that much... I don't know where and how to start
danpost danpost

2014/6/16

#
You should probably start by adding an instance String field to the Locatie class to hold which 'capital' each object of that class is for. The value can be passed to the constructor from the 'populateWorld' method of the Achtergrond class. For example, line 11 would be
addObject(new Locatie("Amsterdam"), 297, 354);
The Locatie class will need a constructor to receive the String:
// if you added this instance field to the Locatie class
private String capital;
// then your constructor could be
public Locatie(String name)
{
    capital = name;
}
// add a 'get' method for the name would be
public String getName() // or 'getCapital'
{
    return capital;
}
That should cover setting up a field so each Locatie object knows what capital it represents; and the 'get' method will allow disclosure of that information to other classes (if needed).
Sbaliyev Sbaliyev

2014/6/16

#
See.. I can't get further without any help :(
danpost danpost

2014/6/17

#
Sbaliyev wrote...
See.. I can't get further without any help :(
You must inform whether the help provided above worked or not; explain what you are having trouble with now; and show the updated code for those classes. If you can right click on a Locatie object (after compiling and before starting) and click on 'getName' and have it show you the name, then that is a start. Maybe you should explain what you are trying to do from here and show the updated classes.
Sbaliyev Sbaliyev

2014/6/17

#
Well, what I have now:
public class Achtergrond extends World
{
    public Achtergrond()
    {    
        super(980, 785, 1); 
        populateWorld();
    }
    
    public void populateWorld()
    {
        addObject(new Locatie("Amsterdam"), 297, 354); //Amsterdam
        addObject(new Locatie("Dublin"), 148, 310); //Dublin 
        addObject(new Locatie("Londen"), 221, 363); //Londen
        addObject(new Locatie("Brussel"), 288, 395); //Brussel
        addObject(new Locatie("Parijs"), 243, 442); //Parijs
        addObject(new Locatie("Luxemburg"), 306, 419); //Luxemburg
        addObject(new Locatie("Bern"), 317, 492); //Bern
        addObject(new Locatie("Warschau"), 525, 340); //Warschau
        addObject(new Locatie("Berlijn"), 414, 353); //Berlijn
        addObject(new Locatie("Lissabon"), 20, 635);  //Lissabon
        addObject(new Locatie("Madrid"), 117, 616); //Madrid
        addObject(new Locatie("Rome"), 378, 592); //Rome
        addObject(new Locatie("Vilnius"), 565, 288); //Vilnius, Litouwen
        addObject(new Locatie("Riga"), 535, 243); //Riga, Letland
        addObject(new Locatie("Tallinn"), 572, 200); //Tallinn, Estland 
        addObject(new Locatie("Zagreb"), 476, 517); //Zagreb, Kroatie
        addObject(new Locatie("Ljublana"), 444, 510); //Ljublana, Slovenie
        addObject(new Locatie("Budapest"), 515, 475); //Budapest, Hongarije
        addObject(new Locatie("Bratislava"), 492, 448); //Bratislava, Slowakije
        addObject(new Locatie("Wenen"), 461, 461); //Wenen, Oostenrijk
        addObject(new Locatie("Praag"), 446, 427); //Praag, Tsjechie
        addObject(new Locatie("Boekarest"), 613, 503); //Boekarest, Roemenie
        addObject(new Locatie("Sofia"), 625, 582); //Sofia, Bulgarije
        addObject(new Locatie("Athene"), 581, 646); //Athene, Griekenland
        addObject(new Locatie("Nicosia"), 792, 715); //Nicosia, Cyprus
        addObject(new Locatie("Valletta"), 417, 753); //Valletta, Malta
        addObject(new Locatie("Helsinki"), 555, 151); //Helsinki, Finland
        addObject(new Locatie("Stockholm"), 433, 213); //Stockholm, Zweden
    }
    
    public void act()
    {
        MouseChange.setImage();
    }
}

public class Locatie extends Actor  
{  
    private boolean onThis = false;    
    private String hoofdstad; 
    
    public void act()   
    {  
        if(Greenfoot.mouseMoved(null)){  
            onThis = Greenfoot.mouseMoved(this);    
        }  
        if(onThis)  {  
            setImage("locatieRood.png");    
        }  
        else  {  
            setImage("locatieZwart.png");     
        }  
    }      
    
    public Locatie(String name)  
    {  
        hoofdstad = name;  
    }  
    
    public String getName()
    {  
        return hoofdstad;  
    } 
    
    public void hoofdsteden()
    {
        System.out.println("Deze hoofdstad is: " + getName());
    }
}  
I need to program the following: Greenfoot should ask: Where is Amsterdam? And I need to click on right capital, amsterdam in this case, If I clicked the right capital is should say good! +1 score and wrong! if i clicked on the wrong capital... So i need to have a method that checks if i clicked the right String "amsterdam", right? and another method that displays good and wrong and another one that counters the score
danpost danpost

2014/6/17

#
We need a field to store the Locatie object that is to be clicked on. I was thinking the best place to store it might be in the Locatie class (because it would be easier to check for mouse clicks on them there); but, we would still need to put the score somewhere, and the Locatie class does not seem like the right place for that. So, since we need to end up doing something in the world class, we might as well store the chosen Locatie object there also. The rest of the code, including mouse click detection could go directly in the world class. Add the 'int score' field and a 'Locatie selected' field in the world class. Then you need to add an 'act' method to the world class that chooses a random Locatie object, stores it in the field, checks for mouse clicks on any Locatie object and does appropriate actions. Choosing one could be done like this:
if (selected == null) selected = (Locatie)getObjects(Locatie.class).get(Greenfoot.getRandomNumber(getObjects(Locatie.class).size());
(once a correct click is detected, change the value of 'selected' back to 'null', so this will choose another).
Sbaliyev Sbaliyev

2014/6/17

#
Well what i have so far: It asks a question: where is amsterdam? If i click on amsterdam my score goes 1 up but if i click on an other capital, nothing happens.. i waits till i click on the right one (I dont want this, it should go to the other) and it asks the same capital every time i dont want this neither..
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Cursor;
import java.util.*;
import java.awt.Color;

public class Achtergrond extends World
{    
    boolean vraagActief=false;
    Locatie actieveLocatie;
    Vragen vragen;
    private int score = 0;
    private Score s;
    
    
    public Achtergrond()
    {    
        super(980, 785, 1); 
        
       vragen = new Vragen();
       addObject(vragen, 100, 20);

       s = new Score();
        
        populateWorld();
    }
    
    public void populateWorld()
    {
       
        addObject(new Locatie("Amsterdam"), 297, 354); //Amsterdam
        addObject(new Locatie("Dublin"), 148, 310); //Dublin 
        addObject(new Locatie("Londen"), 221, 363); //Londen
        addObject(new Locatie("Brussel"), 288, 395); //Brussel
        addObject(new Locatie("Parijs"), 243, 442); //Parijs
        addObject(new Locatie("Luxemburg"), 306, 419); //Luxemburg
        addObject(new Locatie("Bern"), 317, 492); //Bern
        addObject(new Locatie("Warschau"), 525, 340); //Warschau
        addObject(new Locatie("Berlijn"), 414, 353); //Berlijn
        addObject(new Locatie("Lissabon"), 20, 635);  //Lissabon
        addObject(new Locatie("Madrid"), 117, 616); //Madrid
        addObject(new Locatie("Rome"), 378, 592); //Rome
        addObject(new Locatie("Vilnius"), 565, 288); //Vilnius, Litouwen
        addObject(new Locatie("Riga"), 535, 243); //Riga, Letland
        addObject(new Locatie("Tallinn"), 572, 200); //Tallinn, Estland 
        addObject(new Locatie("Zagreb"), 476, 517); //Zagreb, Kroatie
        addObject(new Locatie("Ljublana"), 444, 510); //Ljublana, Slovenie
        addObject(new Locatie("Budapest"), 515, 475); //Budapest, Hongarije
        addObject(new Locatie("Bratislava"), 492, 448); //Bratislava, Slowakije
        addObject(new Locatie("Wenen"), 461, 461); //Wenen, Oostenrijk
        addObject(new Locatie("Praag"), 446, 427); //Praag, Tsjechie
        addObject(new Locatie("Boekarest"), 613, 503); //Boekarest, Roemenie
        addObject(new Locatie("Sofia"), 625, 582); //Sofia, Bulgarije
        addObject(new Locatie("Athene"), 581, 646); //Athene, Griekenland
        addObject(new Locatie("Nicosia"), 792, 715); //Nicosia, Cyprus
        addObject(new Locatie("Valletta"), 417, 753); //Valletta, Malta
        addObject(new Locatie("Helsinki"), 555, 151); //Helsinki, Finland
        addObject(new Locatie("Stockholm"), 433, 213); //Stockholm, Zweden
        
    }
    
    public void act()
    {
        if(vraagActief==false)
        {
            this.actieveLocatie=getLocatie();
            this.vragen.toonVraag(actieveLocatie.getName());
            vraagActief=true;
        }
        
        Locatie actieveLocatie=getActieveLocatie();
        if(actieveLocatie!=null)
        {
            vraagActief=false;
        }
       
        
        MouseChange.setImage();
        score();
    }
    
    private Locatie getActieveLocatie()
    {
        List locaties=getObjects(Locatie.class);
        for(int i=0;i<locaties.size();i++)
        {
            Locatie l=(Locatie)locaties.get(i);
            if (l.getName()==actieveLocatie.getName() && (l.getActief()==true))
            {   
                verhoogScore();
                return l; 
            }
        }
        return null; 
    }
        
    public void score()
    {
        GreenfootImage img = new GreenfootImage(200, 60);
        img.drawString("Score: " + score, 64, 33);
        s.setImage(img);
        addObject(s, 78, 37);
    }
    
    public void verhoogScore()
    {
        score++;
    }
    
    private Locatie getLocatie()
    {
        List locaties=getObjects(Locatie.class);
        int aantal=locaties.size();
        int nr=Greenfoot.getRandomNumber(aantal);
        return (Locatie)locaties.get(nr);
    }
}

import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.awt.Color;
import java.lang.Object;
/**
 * Write a description of class Locatie here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Locatie extends Actor  
{  
    private boolean onThis = false;    
    private String hoofdstad; 
    
    public void act()   
    {  
        if(Greenfoot.mouseMoved(null)){  
            onThis = Greenfoot.mouseMoved(this);    
        }  
        if(onThis)  {  
            setImage("locatieRood.png");    
        }  
        else  {  
            setImage("locatieZwart.png");     
        }  
    }      
    
    public boolean getActief()
    {
        if(onThis==true && Greenfoot.mouseClicked(this))
        {
            return onThis;
        }
        return false;
    }
    
    public Locatie(String name)  
    {  
        hoofdstad = name;  
    }  
    
    public String getName()
    {  
        return hoofdstad;  
    } 
    
    public void hoofdsteden()
    {
        System.out.println("Deze hoofdstad is: " + getName());
    }
}  
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Vragen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Vragen extends Actor
{
    
    public Vragen()
    {
        GreenfootImage img = new GreenfootImage(120, 30);
        img.drawString("Welkom" , 2, 20);
        setImage(img);
    }
    
    public void toonVraag(String vraag)
    {
        getImage().clear();
        getImage().drawString("Waar is "+vraag, 2,20);
    }
    
    public void act() 
    {
        // Add your action code here.
    }    
}
danpost danpost

2014/6/17

#
First, you do not need to import anything except the greenfoot package in any of these classes (you are not using any of the others in any of your classes). The one exception is your use of the List class in your Achtergrond class; however, that can be adjusted so as not to need to import 'java.util.*' as well. Change your 'getLocatie' method to this:
private Locatie getLocatie()
{
    int aantal = getObjects(Locatie.class).size();
    int nr = Greenfoot.getRandomNumber(aantal);
    return (Locatie)getObjects(Locatie.class).get(nr);
}
Next, I do not think that 'onThis' and the mouse clicking should be intermixed. Let 'onThis' deal specifically with the image and mouse clicks deal specifically with scoring and moving on. I would change the act method of the Locatie class to:
public void act()
{
    if (onThis && Greenfoot.mouseMoved(null) && !Greenfoot.mouseMoved(this))
    {
        onThis = false;
        setImage("locatieZwart.png");
        return;
    }
    if (!onThis && Greenfoot.mouseMoved(this))
    {
        onThis = true;
        setImage("locatieRood.png");
    }
    if (Greenfoot.mouseClicked(this))
    {
        ((Achtergrond)getWorld()).clickedLocatie(this);
    }
}
and remove the 'getActief' method from the Locatie class. In the world class (oh, boy), remove the 'getActieveLocatie' method and add the following method:
public void clickedLocatie(Locatie clickedOn)
{
    if (clickedOn == actieveLocatie)
    {
        verhoogScore();
        score();
    }
    getLocatie();
}
and remove the act method of the Achtergrond class. Also, add the following line at the end of the Achtergrond constructor (after calling 'populateWorld();'):
getLocatie();
to start you off. You may want to add a counter to the Achtergrond class to count the number of Locatie objects clicked on and stop the game after so many.
Sbaliyev Sbaliyev

2014/6/17

#
At first, thanks for everything! second, if I use the code which you gave me, my questions: "Where is amsterdam", "Where is London" wont stop, so that means it doesnt goes on false..
Sbaliyev Sbaliyev

2014/6/17

#
no excuse me, if i do it the right way (i forgot to delete the act) it doesn't ask anything
danpost danpost

2014/6/17

#
Oh. Sorry. Will get back to make it better shortly. Will change 'getLocatie' to 'setLocatie' and adjust code within the method.
danpost danpost

2014/6/17

#
Change the 'getLocatie' method to this:
private void setLocatie()
{
    int aantal = getObjects(Locatie.class).size();
    int nr = Greenfoot.getRandomNumber(aantal);
    actieveLocatie = (Locatie)getObjects(Locatie.class).get(nr);
    vragen.toonVraag(actieveLocatie.getName());
}
and change the call from 'getLocatie' in the 'clickedLocatie' method to 'setLocatie'.
Sbaliyev Sbaliyev

2014/6/17

#
It works now thanks. I have one question. Every capital must asked 1 time not more how am i suppose to do this? is it like for loop and then a counter which counts each capital(.size) something like that?
There are more replies on the next page.
1
2