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

2011/4/25

Object Hiding a string, Can't acsses a method from World.

Mattan.H Mattan.H

2011/4/25

#
i have two problems with my game 1.an object is hiding a string, i drew the string with Drawstring Method, how to i make it seen ? 2.i'm trying to use a method from world (earth) in an object :
 Earth.countDes();

the code in the world (earth)
    public void countDes() //count the destroy of a meteor
    {
        counters.add(20);    
    }
getting this error :
non-static method countDes() cannot be referenced from a static context
am i doing something worng?
Builderboy2005 Builderboy2005

2011/4/25

#
Try using
((Earth)getWorld()).countDes();
davmac davmac

2011/4/26

#
Regarding the first problem, what are you drawing the string onto? Maybe post your code.
Mattan.H Mattan.H

2011/4/26

#
thanks Builderboy2005, it worked davmac, i drew the string into onto the background :
       GreenfootImage background = getWorld().getBackground();
        if(Greenfoot.getRandomNumber(100) < 1) {
                background.setColor(Color.RED);
                int x=Greenfoot.getRandomNumber(600);
                Meteor meteor = new Meteor();
                getWorld().addObject(meteor, x, 25);   
              background.drawString(String.format("%c",meteor.getLetter()),x,25);
              
            }
mik mik

2011/4/27

#
The solution is to draw the string not on the background, but on another actor. Create an actor called, say, "Label" that has an invisible (completely transparent) image, and draw the string onto that one. Then you can use the World method setPaintOrder to ensure that your label always stays on top. To make your life a little easier, "Label' classes already exist that you can just copy and use, for example here.
Mattan.H Mattan.H

2011/4/27

#
Thanks! But after the first object draws the string onto the second object(Meteor) X location, the second object(The Meteor) does this every act:
 public void act() 
    {
        GreenfootImage background = getWorld().getBackground();
        background.setColor(Color.BLACK);
       background.drawString( String.format("%c",getLetter()),getX(), getY());
       setRotation(90);
        move();
        background.setColor(Color.RED);
       background.drawString( String.format("%c",getLetter()),getX(), getY());
    }  
    
background.setColor(Color.BLACK); wont painting it black every act will hide the Meteor anyway ? (that's the cuurent code without the changes.) p.s - you can see my source code here : http://www.greenfootgallery.org/scenarios/2850
davmac davmac

2011/4/27

#
Drawing on the background won't hide the actors. The actors always appear over the top of the background.
mik mik

2011/4/27

#
Ah, now I see what you're doing. You draw the letter onto the background at the position of the 'letter' actor. This is to show the letter moving. There is a much easier way to do this. Draw the letter onto the letter's own image (just once, in a constructor when the letter actor is created). And then just move the actor (using your 'move' method). No painting on background necessary, no labels, no second actors. When you move an actor, that actor's image moves automatically. You don't need to repaint it yourself.
Mattan.H Mattan.H

2011/4/27

#
Oh thanks! i did not know that greenfoot draws everything again every act! that's helpfull! thanks again, Mattan.
You need to login to post a reply.