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

2015/5/22

Removing an object in another world

GP2 GP2

2015/5/22

#
Hello Greenfoot community. I know it's super late but if anyone could help me right now that could be awesome because this project is due tomorrow. I am making a game where when a monster attacks the player, the player has to answer a question and if he/she gets it right the monster is removed from the world. Now I have the questions in another world from the game overworld itself so I tried storing the world, player and monster in the question world and then removing the monster from there but no luck. Here is my code for the question world class
import greenfoot.*;
import java.util.*;
import javax.swing.*;
/**
 * Write a description of class Question here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class QuestionWorld extends World
{
    private ArrayList<question> QuestionList;
    private Player currentPlayer;
    private monster currentMonster;
    private int playerX;
    private int playerY;
    private int monsterX;
    private int monsterY;
    private World currentWorld;
    private Counter questionTimer;
    private Counter restoreTimer;
    private int secondKeeper;
    private question q;
    /**
     * Constructor for objects of class Question.
     * 
     */
    public QuestionWorld()
    {    
        super(800, 800, 1);
        q = new question(new GreenfootImage("question1.PNG"), new GreenfootImage("question1r.PNG"), new GreenfootImage("question1w.PNG"), 'd');
        secondKeeper = 0;
        populateWorld();
    }
    public QuestionWorld(World w, Player p, monster m)
    {
        super(800, 800, 1);
        currentWorld = w;
        currentPlayer = p;
        currentMonster = m; 
        secondKeeper = 0;
        q = new question(new GreenfootImage("question1.PNG"), new GreenfootImage("question1r.PNG"), new GreenfootImage("question1w.PNG"), 'd');
        populateWorld();
    }
    public void populateWorld()
    {
        
        questionTimer = new Counter("Seconds Left:");
        restoreTimer = new Counter("Restoring you to your world in:");
        questionTimer.setValue(120);
        restoreTimer.setValue(5);
        addObject(q,400,400);
        addObject(questionTimer,700,500);
        addObject(restoreTimer,200,500);
    }
    public void act()
    {
        secondKeeper++;
        if(q.getRightOrWrong() == 0)
         subtractSecond();
        if(q.getRightOrWrong() > 0)
          subRestoreSecond();
        checkAutomaticWrong();
        if(restoreTimer.getValue() ==0)
        checkAnswer();
    }
    public void subtractSecond()
    {
        if(secondKeeper % 70 == 0)
        {
            questionTimer.setValue(questionTimer.getValue() - 1);
        }
    }
    public void subRestoreSecond()
    {
        if(secondKeeper % 70 == 0)
        {
            restoreTimer.setValue(restoreTimer.getValue() - 1);
        }
    }
    public void checkAutomaticWrong()
    {
        if(questionTimer.getValue() == 0)
        {
            q.automaticWrong();
        }
    }
    public void checkAnswer()
    {
        if(q.getRightOrWrong() == 1)
        {   // this is the line that doesn't work the way I think it should: 
            currentWorld.removeObject(currentMonster);
            Greenfoot.setWorld(currentWorld);}
        else if(q.getRightOrWrong() == 2)
        {
            Greenfoot.stop();
            JOptionPane.showMessageDialog(null, "You lose");
            //Greenfoot.setWorld(currentWorld);
        }
    }
}
danpost danpost

2015/5/22

#
The execution of the issue code depends on the 'getRightOrWrong' method of the question class. Please post that class code.
You need to login to post a reply.