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

2024/12/5

How to call this method in a class from another class.

Shadowolf Shadowolf

2024/12/5

#
Hello I want to make this object change its image when I let go of the mouse, but I set up all the images it could be set to and made a method but in the other class it only adds the object to the world and doesn't change the image. The class with the method that I want to change: public class TriangleAnswer extends Actor { public void randomize() { int image = (int)(Math.random() * 20) + 1; if(image == 1) { setImage("1.png"); } if(image == 2) { setImage("2.png"); } if(image == 3) { setImage("3.png"); } if(image == 4) { setImage("4.png"); } if(image == 5) { setImage("5.png"); } if(image == 6) { setImage("6.png"); } if(image == 7) { setImage("7.png"); } if(image == 8) { setImage("8.png"); } if(image == 9) { setImage("9.png"); } if(image == 10) { setImage("10.png"); } if(image == 11) { setImage("11.png"); } if(image == 12) { setImage("12.png"); } if(image == 13) { setImage("13.png"); } if(image == 14) { setImage("14.png"); } if(image == 15) { setImage("15.png"); } if(image == 16) { setImage("16.png"); } if(image == 17) { setImage("17.png"); } if(image == 18) { setImage("18.png"); } if(image == 19) { setImage("19.png"); } else { setImage("20.png"); } } } The class I want to call the method in: public class Magic8Ball extends Actor { //Leave this field alone, this will be the Triangle on the screen private TriangleAnswer triangle = new TriangleAnswer(); public void act() { if(Greenfoot.mouseDragged(this)) { <---- // I want to put it here MouseInfo m = Greenfoot.getMouseInfo(); setLocation(m.getX(),m.getY()); if(isTouching(TriangleAnswer.class)) { removeTouching(TriangleAnswer.class); } } if(Greenfoot.mouseDragEnded(this)) { int xMove = (int)(Math.random() *201)-100; int yMove = (int)(Math.random() *201)-100; getWorld().addObject(triangle, getX() + xMove, getY() + yMove); } } } thank you
danpost danpost

2024/12/5

#
Shadowolf wrote...
Hello I want to make this object change its image when I let go of the mouse, but I set up all the images it could be set to and made a method but in the other class it only adds the object to the world and doesn't change the image. << Code Omitted >>
This should (at least) call the method (on the object):
MouseInfo m = Greenfoot.getMouseInfo();
if (m != null && Greenfoot.mouseDragged(this))
{
    setLocation(m.getX(), m.getY());
    if (intersects(triangle))
    {
        triangle.randomize();
        getWorld().removeObject(triangle);
    }
}
Since all actions require a mouse op, you could start with:
MouseInfo m = Greenfoot.getMouseInfo();
if (m == null) 
{
    return;
}
if (Greenfoot.mouseDragged(this))
{
    ...
danpost danpost

2024/12/5

#
Your TriangleAnswer class can be much more simply written as follows:
public class TriangleAnswer extends Actor
{
    public void randomize()
    {
        int image = Greenfoot.getRandomNumber(20)+1;
        setImage(""+image+".png");
    }
}
You need to login to post a reply.