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

2014/8/20

getting value of text

1
2
3
Super_Hippo Super_Hippo

2014/8/21

#
As far as I see, you create a rectangle if you press 'enter' when there aren't any rectangles in the world. But in the 'act' method of the 'Rectangle' class, you delete the rectangle again when pressing 'enter' (which you do since you just added some rectangles with clicking it. So it looks like you add and remove them over and over if you hold enter. By the way, in general, you compare strings with the 'isequal' method and not with '=='.
danpost danpost

2014/8/21

#
Super_Hippo is correct with the first part and incorrect with the second part. The correct part is that the rectangles will immediately remove themselves; I will explain later. The incorrect part is that it is not a cycle. The rectangles will appear for one act cycle and then be removed. Because the text was changed when adding the rectangles, more will not be placed in the world after the first group gets removed. Correct part explained, the rectangles are added when the 'enter' key is pressed down. The rectangles, when it is their turn to act will ask are there any rectangles in the world. Well, of course, the rectangle whose turn it is to act is certainly in the world -- so that part is true. It also asks if the 'enter' key is pressed. Well, yes. Unless you are very, very quick and can press and release the key before the rectangles get a chance to act, which is virtually impossible, the 'enter' key will be found still down. So with both parts returning 'true', the rectangle is removed on the first cycle it has a chance to act on. If you want to give the key a chance to be released, change the 'if' statement on line 36 to something like this:
if (timer < 35 && Greenfoot.isKeyDown("space"))
Super_Hippo Super_Hippo

2014/8/22

#
Right, they only appear once and will be removed, that's why you only see this flashing once. You said, without this line of code it works fine, but actually you are in this 'circle' and add and remove them over and over.
danpost danpost

2014/8/22

#
@Super_Hippo, where are you seeing this 'circle'. The Text class act method does the following two things when the 'delay' field is no longer positive, when the 'enter' key is pressed and when the current text is the one specified: * adds the rectangles into the world * changes the current text The second thing, changing the current text, will cause the last condition above to be 'false' and, therefore, no 'circle' is created.
Super_Hippo Super_Hippo

2014/8/22

#
danpost wrote...
@Super_Hippo, where are you seeing this 'circle'.
The circle is there because he tested it without the 'setText' method and if this string isn't changed, the condition won't be false. ↓
davemib123 wrote...
if I run the
 if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2) 
from the act method without:
setText(palletTown_Sign2a);  
the rectangles work fine.
danpost danpost

2014/8/22

#
Super_Hippo wrote...
< Quote Omitted (danpost) > The circle is there because he tested it without the 'setText' method and if this string isn't changed, the condition won't be false. < Quote Omitted (davmib123) >
Oh, I see. Maybe I should have realized that after you posted this:
Super_Hippo wrote...
without this line of code it works fine, but actually you are in this 'circle' and add and remove them over and over.
Sorry, I guess I was looking more on fixing the problem with the line than on the unwanted behavior without the line.
danpost danpost

2014/8/22

#
@davmib123 (1) What all, exactly, are these rectangles supposed to do once they are added into the world? I am getting that they are to exist for about 7 seconds and then fade out of existence unless they are prematurely removed by the 'space' key being pressed. Is that the behavior that you want them to have? (2) Once they have all faded out or been removed, are there to be more groups of rectangles appearing? and if so, immediately after all are gone or after a delay?
davemib123 davemib123

2014/8/22

#
thanks for the replies. the rectangles are to show the text in an almost letter by letter fashion. you can see what I was attempting in this scenario: http://www.greenfoot.org/scenarios/12097 Look at the Text and Rectangle class. When testing the scenario go to the grey sign on the left side.
davemib123 davemib123

2014/8/22

#
i've tried this way:
if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2)  
        {  
            setText(palletTown_Sign2a);  
        }  
        if (Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2a)  
        {  
            C.addRectangles();
        } 
Works fine, but how can the two if statements be combined together?
danpost danpost

2014/8/22

#
Combined, it would just be this:
if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2)
{
    setText(palletTown_Sign2a);
    C.addRectangles();
}
as once the first if is true, the text becomes what the second if checks for and the 'enter' key is still down. I think maybe you should be using 'getKey' instead of 'isKeyDown'. Something like this:
if (delay <= 0 && currentText == palletTown_Sign2 && "enter".equals(Greenfoot.getKey()))
{
    setText(palletTown_Sign2a);
    C.addRectangles();
}
provided you are not using 'getKey' elsewhere in your project.
danpost danpost

2014/8/22

#
Is the text supposed to fade away with the rectangles? Were you trying to have the text, bit by bit, fade with the rectangles?
davemib123 davemib123

2014/8/22

#
i was trying to have the text show bit by bit. the rectangles basically hide the text and then as they are removed a character is shown. it is supposed to work like this: 1) the text is set 2) rectangles added 3) rectangles begin to fade 4) character is shown You will see in the updated scenario that it shows fine when its like this:
        if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2)  
        {  
            setText(palletTown_Sign2a);  
        }  
        if (Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2a)  
        {  
            C.addRectangles();
        } 
Super_Hippo Super_Hippo

2014/8/22

#
So right now, it is working as it should? Or I don't get what's wrong.
davemib123 davemib123

2014/8/22

#
It works to an extent. this is what i have now:
   public void act()
    {
        CreateMap C = (CreateMap)getWorld();   
        if (getWorld().getObjects(Rectangle.class).size() == 0)  
        {  
            delay --;
        } 
        if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2)  
        {  
            setText(palletTown_Sign2a);  
        }  
        if (Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2a)  
        {  
            C.addRectangles();
        } 
        if (delay <= 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2a)  
        {  
            getWorld().removeObject(this);
        }  
        
       }
the C.addRectangles(); i only want appearing once on that statement - would that be through a boolean?
Super_Hippo Super_Hippo

2014/8/22

#
Okay, I think I got what you want. I changed the following: In the 'Rectangle' class:
    public void act() 
    {
        if (timer == 0) fadeOut();
        else timer--;
    }
('private int delay = 50;' on top isn't needed there by the way.) In the 'Text' class:
private boolean justAddedNewText;
CreateMap C;

public void addedToWorld(World CreateMap)
{
    C = (CreateMap) getWorld();
}
Only saved the World so it doesn't have to be checked every act cycle.
    private void updateImage(String text)
    {
        //[...] just add this line at the end
        justAddedNewText = true;
    }
    public void act()
    {  
        
        if (justAddedNewText == true)
        {
            if (Greenfoot.isKeyDown("enter")) return;
            justAddedNewText = false;
        }
        
        if (getWorld().getObjects(Rectangle.class).size() > 0)
        {
            if (Greenfoot.isKeyDown("enter"))
            {
                C.removeObjects(C.getObjects(Rectangle.class));
            }
        }
        else delay--;
        
        if (delay < 0 && Greenfoot.isKeyDown("enter") && currentText == palletTown_Sign2)  
        {  
            C.addRectangles();
            setText(palletTown_Sign2a);  
        }  
    }
There are more replies on the next page.
1
2
3