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

2017/4/3

images

1
2
3
adlafforest adlafforest

2017/4/3

#
The image works. I was able to use it in my game with this code: GreenfootImage image = new GreenfootImage("France.png"); getBackground().setFont(new Font("", true, false, 50)); The problem is that it was set as a background. So the same image would show up for each question.
adlafforest adlafforest

2017/4/3

#
I just restarted Greenfoot and I'm now able to open the editor. However, the image still doesn't show up.
Nosson1459 Nosson1459

2017/4/3

#
Nosson1459 wrote...
The code that you did works for me. The only things that I can think of are that either it's being blocked by other actors, or the image "France.png" isn't what you thought it was. Do you get errors or you just don't see the image?
I don't have any new ideas. Maybe try doing a few tests.
  • Do setPaintOrder and have this imageActor be the first thing in the list
  • Try using a different image. For this maybe you should (also) try
    setImage(new GreenfootImage(50, 50));
    getImage().setColor(Color.RED);
    getImage().fillOval(0, 0, 50, 50);
  • Try using a whole different instance altogether, meaning a new 'private Actor testActor = ...' with a new setImage(..., and a new addObject(...
adlafforest adlafforest

2017/4/3

#
I tried all three solutions. Nothing worked. Thank you for trying to solve my coding problem. I'm not sure why it still does not want to work.
Nosson1459 Nosson1459

2017/4/3

#
Could you upload your scenario so that I can maybe try to figure it out.
danpost danpost

2017/4/3

#
Add the following to the Answer class:
protected void addedToWorld(World world)
{
    if (correct)
    {
        ((QuizWorld)world).setimageActor(text+".png");
    }
}
Then, in QuizWorld, add the following:
public void setImageActor(String filename)
{
    imageActor.setImage(filename);
}
adlafforest adlafforest

2017/4/3

#
yeah I could. I'm not sure how to upload my entire scenario though..
danpost danpost

2017/4/3

#
I did not get a change to correct the code in my last post. In line 5, 'setimageActor' should be 'setImageActor'. Also the code for the Answer class is sketchy as far as exactly what it should be -- 'correct' and 'text + ".png"' may need adjusting.
adlafforest adlafforest

2017/4/3

#
The 'setImageActor' is underlined in red. It says it 'cannot find symbol.'
Super_Hippo Super_Hippo

2017/4/3

#
Did you add the method to your world class? (danpost provided two methods) (You can upload your scenario when you click Export in the top right corner. You can only publish a compiled scenario, so if it doesn't compile, you need to comment out the parts which are not compiling.) @Nosson: setPaintOrder will not work I believe because the imageActor is just an Actor, not a new class, so you can't set it as a parameter for the method. Anyways, I don't think there will be anything which could hide the actor.
adlafforest adlafforest

2017/4/3

#
I have published the quiz to Greenfoot. It's called "Geography quiz."
danpost danpost

2017/4/3

#
adlafforest wrote...
I have published the quiz to Greenfoot. It's called "Geography quiz."
Do the following: In the Answer class, add the following line to the 'addedToWorld' method:
if (correct) ((QuizWorld)world).getImageActor().setImage(new GreenfootImage(text+".png"));
Add the following method to the QuizWorld class:
public Actor getImageActor()
{
    return imageActor;
}
Add the following line to the end of the 'showQuestion' method:
addObject(imageActor, 350, 200);
Make sure all your images are saved as '.png' files. Make sure all true text matches the image name (without the '.png').
danpost danpost

2017/4/3

#
And please do not start multiple discussion threads on the same issue. Go here and Destroy it, please.
Super_Hippo Super_Hippo

2017/4/3

#
You can change the showQuestion method in the QuizWorld class to this:
private void showQuestion()
{
    clear();
    addObject(questions.get(questionNum), 250, 50);
    getBackground().clear();
    getBackground().fill();
    String answer = questions.get(questionNum).getExplanation().getText().substring(23);
    answer = answer.substring(0, answer.length()-1);
    getBackground().drawImage(new GreenfootImage(answer+".png"), getWidth()/3, getHeight()/4);
}
Remove the space at the end of the first explanation string (line 42) and add this method to the Explanation class:
public String getText() {return text[0];}
adlafforest adlafforest

2017/4/3

#
It works! Thank you so much.
There are more replies on the next page.
1
2
3