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

2018/2/24

Quiz

Maus2525 Maus2525

2018/2/24

#
The purpose of my game is to appear a few questions when the main actor, Alien encounters foreign planets. So, whenever he touches a planet, a question will appear. I've created the Alien class, the Menu class (which is where the questions are displayed) and another classes which represent the planets. I tried to create the relationship between Alien and Menu classes, but it doesn't work.
// in Alien class
public Actor planet_x;
planet_x = getOneIntersectingObject(Alien.class);
if (planet_x != null)
{
     getWorld().addObject(new Menu(planet_x), 250, 250);
}

// in Menu class
public Menu(Actor planet)
{
     if (planet_x == Mars.class)
          // display question 1
     if (planet_x == Pluto.class)
          // display question 2
     ...
}
Vercility Vercility

2018/2/24

#
You're comparing a variable of type "Actor" with a class. Thats not how it works. "==" checks if 2 references reference the same object. Use something like planet_x.getClass().getName().equals("Mars") By the way, what is the planet in your Menu method for..?
danpost danpost

2018/2/24

#
In line 3, you are having your Alien object look for an intersecting Alien object and placing it in a field you later check to be one of the planets. That just does not make any sense. Another way to check which type an object is goes like this:
if (planet instanceof Mars)
Maus2525 Maus2525

2018/2/25

#
Thanks, I've fixed my code, but I must have avoided something. The program doesn't have syntax errors this time, but instead of showing an image and writing text on it when the alien and planets collide, only the panel shows up, without showing the text.
// alien class
import greenfoot.*;

public class Alien extends Actor
{
    public void act() 
    {
        move(5);
        if (isTouching(Mercur.class))
            getWorld().addObject(new Menu(this, "Mercur"), 1400, 150);
            
        if (isTouching(Venus.class))
            getWorld().addObject(new Menu(this, "Venus"), 1400, 150);

        if (Greenfoot.isKeyDown("left") == true)
            turn(-3);

        if (Greenfoot.isKeyDown("right") == true)
            turn(3);
    }
}

// menu class (where the text must show up)
import greenfoot.*;
import java.awt.Color;

public class Menu extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage back = getImage();

    public String q1_0 = "Craterele de pe Mercur au denumirile unor: \n";
    public String q1_1 = "a. personalitati \n";
    public String q1_2 = "b. planete \n";
    public String q1_3 = "c. zeitati";
    private String q1s[] = {q1_0, q1_1, q1_2, q1_3};

    public String q2_0 = "Numele Venus provine de la zeita romana a: \n";
    public String q2_1 = "a. razboiului \n";
    public String q2_2 = "b. recoltei \n";
    public String q2_3 = "c. frumusetii";
    private String q2s[] = {q2_0, q2_1, q2_2, q2_3};
    
    private Alien myMenu;
    private String planetX = "";
    
    public Menu(Alien myMenu, String planet)
    {
        this.myMenu = myMenu;
        planetX = planet;
    }
    
    public Actor getMenu()
    {
        return myMenu;
    }
    
    public void act()
    {
        write(planetX);
    }

    public void write(String x)
    {
        if (x == "Mercur")          write_string(q1s, 60);
        else if (x == "Venus")      write_string(q2s, 60);
    }

    public void write_string(String x[], int y)
    {
        String text = "";
        GreenfootImage txt;
        GreenfootImage background = new GreenfootImage(back);
        for (int i = 0; i < x.length; i++)
        {
            txt = new GreenfootImage(x[i], 24, Color.BLACK, transparent);
            background.drawImage(txt, 20, y);
            y += 30;
        }
        setImage(background);
    }
}
danpost danpost

2018/2/25

#
Provided the 'back' image is large enough to contain all the texts and provided the texts are not being drawn on a black (or dark) image, the 'write_string' method looks like it would work. These are things you should probably verify.
Vercility Vercility

2018/2/25

#
What is the text String for? You never use it
danpost danpost

2018/2/25

#
What version of Greenfoot are you using?
Maus2525 Maus2525

2018/2/25

#
Version 3.0.4. I think the problem occurs because the text is written behind the image.
Maus2525 Maus2525

2018/2/25

#
I've set the transparency of the image to 0 to check my idea, but it doesn't change.
public void write_string(String x[], int y)
    {
        GreenfootImage txt;
        GreenfootImage background = new GreenfootImage(back);
        background.setTransparency(0);
        for (int i = 0; i < x.length; i++)
        {
            txt = new GreenfootImage(x[i], 24, Color.WHITE, transparent);
            background.drawImage(txt, 20, y);
            y += 30;
        }
        setImage(background);
    }
danpost danpost

2018/2/25

#
Maus2525 wrote...
Version 3.0.4. I think the problem occurs because the text is written behind the image.
What are the dimensions of the backrground image?
Maus2525 wrote...
I've set the transparency of the image to 0 to check my idea, but it doesn't change.
Are you saying the panel still shows; but there is no text?
Maus2525 Maus2525

2018/2/25

#
Yes, the panel is displayed, but I've only seen the end of the last word in white near the image. The dimension of the photo is 384 x 241.
danpost danpost

2018/2/25

#
The the panel is a different actor than this one and has nothing to do with the issue (reason being if you set trans to 0 and panel still shows, it must not be part of this actor). Can you pause the scenario and drag the text part that you see around? What can you say about what you see then?
Maus2525 Maus2525

2018/2/25

#
There are a 13 panels where there the same text is written and 1 blank panel above.
danpost danpost

2018/2/25

#
Enclose lines 9 through 14 of the act method in the Alien class above with:
if (getWorld().getObjects(Menu.class).isEmpty())
{
    // lines 9 - 14 here
}
Then do it again.
Maus2525 Maus2525

2018/2/25

#
I cannot believe that it is finally working!!! Thank you very much!!!
You need to login to post a reply.