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

2018/2/18

removing text from World

ImAndrew ImAndrew

2018/2/18

#
I have these two methods which should add and delete after some time a text.The problem is that I want to add text based on the user clicks but if he click again before the previous text was deleted the new text will be added over the old text.
    void addText(String _text)
    {
        if(text.getWorld() != null) // i tried this if but it doesn't work
        {
            text.getWorld().removeObject(text);
        }
        text = new Label(_text, 30);
        getWorld().addObject(text, Dungeon1.WIDTH/2, 60);
        textOnScreen = true;
        textTimer.mark();
    }
    
    void removeText()
    {
        if(textOnScreen && textTimer.millisElapsed() > 1500)
        {
            getWorld().removeObject(text);
            textOnScreen = false;
        }
    }
danpost danpost

2018/2/18

#
You do not need the 'textOnScreen' field -- just set 'text = null' after removing it from world.
void addText(String _text)
{
    if(text != null) getWorld().removeObject(text);
    text = new Label(_text, 30);
    getWorld().addObject(text, Dungeon1.WIDTH/2, 60);
    textTimer.mark();
}

void removeText()
{
    if (text != null && textTimer.millisElapsed() > 1500)
    {
        getWorld().removeObject(text);
        text = null;
    }
}
Your way probably did not work (line 3) because you did not check to see if 'text' was not 'null' first.
ImAndrew ImAndrew

2018/2/19

#
danpost wrote...
You do not need the 'textOnScreen' field -- just set 'text = null' after removing it from world.
void addText(String _text)
{
    if(text != null) getWorld().removeObject(text);
    text = new Label(_text, 30);
    getWorld().addObject(text, Dungeon1.WIDTH/2, 60);
    textTimer.mark();
}

void removeText()
{
    if (text != null && textTimer.millisElapsed() > 1500)
    {
        getWorld().removeObject(text);
        text = null;
    }
}
Your way probably did not work (line 3) because you did not check to see if 'text' was not 'null' first.
I tried but the previous text is still there.
danpost danpost

2018/2/19

#
ImAndrew wrote...
I tried but the previous text is still there.
Please show the class codes in its entirety.
ImAndrew ImAndrew

2018/2/19

#
danpost wrote...
ImAndrew wrote...
I tried but the previous text is still there.
Please show the class codes in its entirety.
Sure
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;

/**
 * Write a description of class Chest here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Chest extends Actor
{
    private boolean zDown = false;
    private boolean openChest = true;
    
    private int noChoices = 3;
    private Label text = new Label("",30);
    private SimpleTimer textTimer = new SimpleTimer();
    
    private MouseInfo mouse;
    public void act() 
    {
        openChest();
        breakChest();
        removeText();
        clickLock();
        
        mouse = Greenfoot.getMouseInfo();
    }
    
    void openChest()
    {
        List<Hero> heroList = getObjectsInRange(70, Hero.class);
        if(getWorld().getObjects(Enemy.class).isEmpty() && !heroList.isEmpty() && openChest)
        {
            if(zDown != Greenfoot.isKeyDown("z"))
            {
                if(!zDown)
                {
                    addLocks();
                    openChest = false;
                }
            }
        }
    }
    
    public void addLocks()
    {
        int locks = 6;
        int goodLockPlace = Greenfoot.getRandomNumber(locks);
        for(int i = 0; i < locks; i++)
        {
            int x = Dungeon1.WIDTH/2 - 55 + 30*i;
            if(i == goodLockPlace)
            {
                getWorld().addObject(new GoodLock(), x, Dungeon1.HEIGHT/2);
            }
            else
            {
                getWorld().addObject(new WrongLock(), x, Dungeon1.HEIGHT/2);
            }
        }
    }
    
    void breakChest()
    {
        if(noChoices <= 0)
        {
            addText("Ai ramas fara incercari.Incuietoarea s-a stricat.");
            getWorld().removeObjects(getWorld().getObjects(WrongLock.class));
            getWorld().removeObjects(getWorld().getObjects(GoodLock.class));
        }
    }
    
    void clickLock()
    {
        MouseInfo mouse = Greenfoot.getMouseInfo();
        if (mouse != null)
        {
            int click = mouse.getButton();
            if(mouse.getActor() instanceof WrongLock && click == 1)
            {
                wrongLock();
                getWorld().removeObject(mouse.getActor());
            }
            else if(mouse.getActor() instanceof GoodLock && click == 1)
            {
                chestReward();
                getWorld().removeObjects(getWorld().getObjects(WrongLock.class));
                getWorld().removeObject(mouse.getActor());
            }
        }
    }
    
    void wrongLock()
    {
        noChoices--;
        if(noChoices > 0)
        {
            addText("Ai gresit lacatul.Incercari ramase: " + noChoices);
            getWorld().addObject(text, Dungeon1.WIDTH/2, 60);
        }
    }
    
    void chestReward()
    {
        int reward = Greenfoot.getRandomNumber(7);
        Hero hero = Dungeon1.hero;
        switch(reward)
        {
            case 0:
                int bonusHealth = Greenfoot.getRandomNumber(20);
                hero.setHealth(hero.getHealth() + bonusHealth);
                addText("Ai primit " + bonusHealth + " puncte de viata");
                break;
                
            case 1:
                hero.setDamage(hero.getDamage() + Greenfoot.getRandomNumber(10));
                addText("Atacul a crescut la " + hero.getDamage());
                break;
                
            case 2:
                Arrow.setDamage(Arrow.getDamage() + Greenfoot.getRandomNumber(10));
                addText("Atacul la distanta a crescut la " + Arrow.getDamage());
                break;
                
            case 3:
                hero.setAttackCooldown(hero.getAttackCooldown() - Greenfoot.getRandomNumber(30));
                addText("Viteza de atac a crescut");
                break;
            case 5:
            
            case 6:
                addText("Ai deschis cufarul, dar nu ai gasit nimic.");
                break;
        }
    }
    
    void addText(String _text)
    {
        if(text != null)
        {
            getWorld().removeObject(text);
        }
        text = new Label(_text, 30);
        getWorld().addObject(text, Dungeon1.WIDTH/2, 60);
        textTimer.mark();
    }
     
    void removeText()
    {
        if (text != null && textTimer.millisElapsed() > 1500)
        {
            getWorld().removeObject(text);
            text = null;
        }
    }
}
danpost danpost

2018/2/19

#
Question: how many Chest objects do you have in the world (at one time)?
ImAndrew ImAndrew

2018/2/19

#
danpost wrote...
Question: how many Chest objects do you have in the world (at one time)?
Just one object.
danpost danpost

2018/2/19

#
Are you removing any Chest objects from any of your classes and, if so, where (show codes)?
ImAndrew ImAndrew

2018/2/19

#
danpost wrote...
Are you removing any Chest objects from any of your classes and, if so, where (show codes)?
I don't remove any chest object.The idea of the class is that the player enters in a room, finds the chest and tries to open it.The chest will be there all the time.
danpost danpost

2018/2/19

#
ImAndrew wrote...
I don't remove any chest object.The idea of the class is that the player enters in a room, finds the chest and tries to open it.The chest will be there all the time.
Okay ... well I copy/pasted the class into a new project, added required actors, methods and a little code. It seems to work as you want it to. That is, I do not get text on top of text at all.
Super_Hippo Super_Hippo

2018/2/19

#
What is line 100 doing there?
ImAndrew ImAndrew

2018/2/19

#
danpost wrote...
ImAndrew wrote...
I don't remove any chest object.The idea of the class is that the player enters in a room, finds the chest and tries to open it.The chest will be there all the time.
Okay ... well I copy/pasted the class into a new project, added required actors, methods and a little code. It seems to work as you want it to. That is, I do not get text on top of text at all.
Oh, well, I don't know why but i get this result when I play the game screenshot
ImAndrew ImAndrew

2018/2/19

#
Super_Hippo wrote...
What is line 100 doing there?
   getWorld().addObject(text, Dungeon1.WIDTH/2, 60);
it adds the text in the world
danpost danpost

2018/2/19

#
ImAndrew wrote...
it adds the text in the world
Yeah ... well, line 145 already does that. However, it does not cause any issue.
danpost danpost

2018/2/19

#
I would like to take a look at the codes in the Hero class to see what is there. Of course, best would be for you to upload the scenario with source provided so that we can physically test it.
You need to login to post a reply.