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

2017/3/8

Write Multiple Things To A File

2
3
4
5
6
7
mitrolex mitrolex

2017/3/29

#
Managed to fix all those errors and NullPointers.. Just have to find a way to print that out on the screen when clicked. :D
danpost danpost

2017/3/29

#
mitrolex wrote...
Managed to fix all those errors and NullPointers.. Just have to find a way to print that out on the screen when clicked. :D
The simplest way would be to build the appropriate text string and create a GreenfootImage object using said text string and give it to a new actor object to display in the world.
mitrolex mitrolex

2017/3/29

#
What do you mean by appropriate text string?
danpost danpost

2017/3/29

#
mitrolex wrote...
What do you mean by appropriate text string?
I mean the information you wish to display will need to have some type of format. You must have some idea how you want the information displayed. I am not referring to how to display it on the screen, but how to organize the text to be displayed on the screen. Right now, you just have names and scores; but, you probably have some other information you may want to display along with it that might include things like a title, field labels and rankings for each line). Of course a simple display would probably just add the ranking numbers. Use string concatenation to build the text you want to display as you want it displayed -- that would be the appropriate text string.
mitrolex mitrolex

2017/4/2

#
I have a new problem now. When i click on an actor a new actor shows up on the screen and i want to write the names and scores onto that actor and when i click on it it needs to dissapear from the screen but for some reason the "removeObjects(getObjects(actorName.class));" isn't working... The new actor just stays on the screen.
danpost danpost

2017/4/2

#
mitrolex wrote...
I have a new problem now. When i click on an actor a new actor shows up on the screen and i want to write the names and scores onto that actor and when i click on it it needs to dissapear from the screen but for some reason the "removeObjects(getObjects(actorName.class));" isn't working... The new actor just stays on the screen.
You probably have a reference to a duplicate Actor that is not in the world. Make sure you only have one 'new actorName()' in your code and that it is not being executed multiple times. If you are not sure what I am saying, post the class code where 'new actorName()' is located.
mitrolex mitrolex

2017/4/2

#
import greenfoot.*; 
import java.io.*;
public class rank extends Actor
{
    boolean p = false;
    public void act() 
    {
        bg b = new bg();
        rankSlika rs = new rankSlika();
        //rankLista rl = new rankLista();
        if(Greenfoot.mouseClicked(this))
        {
            
            p = true;                        
            while(p==true)
            {
                b.citanje();
                b.rank();
                p=false;
            }    
            getWorld().addObject(rs,b.getWidth()/2,b.getHeight()/2);
        }       
        
        if(Greenfoot.mouseClicked(rs))
        {
             getWorld().removeObject(rs);
        }
        
    }
}
It's in this class.
danpost danpost

2017/4/2

#
Line 8 should get a reference to the current world (not create a new one):
bg b = (bg)getWorld();
Line 9 should be up along with line 5 so that you only create one object of that type. Line 24 should check that 'rs' is not 'null' before checking for a click on it:
if (rs != null && Greenfoot.mouseClicked(rs))
mitrolex mitrolex

2017/4/2

#
Thank you very much, works like a charm.
danpost danpost

2017/4/2

#
mitrolex wrote...
Thank you very much, works like a charm.
That change in line 24 is probably not needed as you are assigning an object to reference when declaring the field; and it does not need to be check whether it is in the world because it cannot be clicked on if not in the world. So,
if (Greenfoot.mouseClicked(rs))
should be sufficient for line 24.
mitrolex mitrolex

2017/4/2

#
Now when i try to write the names on the actor only the last name from the file is written.
import greenfoot.*; 
import java.io.*;
public class rank extends Actor
{
    boolean p = false;
    rankSlika rs = new rankSlika();
    rankListaIme rl = new rankListaIme();
    Color c = new Color(0,0,0,0);
    public void act() 
    {               
        if(Greenfoot.mouseClicked(this))
        {
            bg b = (bg)getWorld(); 
            p = true;                        
            while(p==true)
            {
                b.citanje();
                b.rank();
                p=false;
            } 
            getWorld().addObject(rs,b.getWidth()/2,b.getHeight()/2);
            getWorld().addObject(rl,b.getWidth()/2-120,b.getHeight()/2);
            for (int n=0; n<b.data.length; n++)
            {
                rl.setImage(new GreenfootImage((String)b.data[n][0], 35, Color.BLACK, c));                              
            }            
        }       
        
        if (Greenfoot.mouseClicked(rs))
        {
            getWorld().removeObject(rs);
        }

    }
}
danpost danpost

2017/4/2

#
Each time through the loop, you are setting a different image to the 'rl' object. The last one is the one that will be seen in the scenario. If you are trying to show multiple data sets, then you either need multiple 'rankListaIme' objects or draw the images produces within the loop at different places in a large image to set to 'rs'.
mitrolex mitrolex

2017/4/2

#
Which of those two would be easier for me to do?
danpost danpost

2017/4/2

#
mitrolex wrote...
Which of those two would be easier for me to do?
I think it is just a matter of preference. I probably would put all the data on one image and use one actor to display it; and this may be what you need to do or else you would have to check for mouse clicks on multiple objects.
mitrolex mitrolex

2017/4/2

#
How do i change the location of those images(strings or whatever they are..) within a loop?
There are more replies on the next page.
2
3
4
5
6
7