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

2015/4/8

Getting the object's name of touching

nathan-96 nathan-96

2015/4/8

#
Hello everyone, I have to create a small program in greenfoot so childeren can learn to match text with an image. To text is draggable to an image. This code check if text is dragged to an image and when the mouse is released, it returns true.
    public boolean isWoordGesleept()
    {
       
       if (Greenfoot.mouseDragEnded(this) == true &&this.isTouching(Woord.class))
       {
             return true;
       }
       return false;    
    }
Now I need to now what text is dropped on an image
    public String getWoord()
    {
        return text;
    }
I also need to now the text of the Woord.class.
import greenfoot.*;

public class Woord extends Actor
{
    private String naam;
    private String afbeelding;
    //private int foutGeraden;
    //private boolean geraden;
    
    public Woord(String naam, String afbeelding)
    {
        this.naam = naam;
        this.afbeelding = afbeelding;
        this.setImage(afbeelding);
        this.getImage().scale(100,100);
    }
    
    public String getNaam()
    {
        return naam;
    } 
    
    public void act()
    {
        this.hover();
    }
    
    public void hover()
    {
       if (Greenfoot.mouseDragEnded(this) == false && this.isTouching(SleepbaarTekstvak.class))
       {
           this.getImage().scale(150,150);
       }
       else
       {
           this.getImage().scale(100,100);
       }
    }
}
So I want to know the name of the actor a text is released on. I tried
Woord woord = getoneobjectatoffset(0,0,Woord.class)
but I got java.lang.Object cannot be converted to Woord. Can anyone help me getting the name (naam)?
davmac davmac

2015/4/8

#
You need to fix the spelling of "getOneObjectAtOffset", and you need an appropriate cast:
Woord woord = (Woord) getOneObjectAtOffset(0,0,Woord.class);
nathan-96 nathan-96

2015/4/8

#
I did the spelling correct, I was just in a rush ;) Indeed I forget the bold text: Woord woord = (Woord) getOneObjectAfOffset(0,0,Woord.class); Thanks davmac, its working now, you're a hero (Someone explained casting to me)
You need to login to post a reply.