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

2018/12/30

My gif wont play

1
2
Pando Pando

2018/12/30

#
So i saw several posts about gifs and how to use them. However, whenever i attempt to replicate them my gif just wont work. My gif is a blinking cursor for typing. When im dragging it onto the world it is blinking, but as soon as i let go/or press play it just stays as a solid black line. Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Cursor extends Actor
{
    GifImage gif = new GifImage("Cursor.gif");
     
    public Cursor()
    {
        int scalePercent = 25;
        for (GreenfootImage image : gif.getImages())
        {
            int wide = image.getWidth()*scalePercent/100;
            int high = image.getHeight()*scalePercent/100;
            image.scale(wide, high);
        }
    }
     
    /**
     * Act - do whatever the Cursor wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        setImage(gif.getCurrentImage());
    }   
}
danpost danpost

2018/12/30

#
Pando wrote...
So i saw several posts about gifs and how to use them. However, whenever i attempt to replicate them my gif just wont work. My gif is a blinking cursor for typing. When im dragging it onto the world it is blinking, but as soon as i let go/or press play it just stays as a solid black line. Here's my code: << Code Omitted >>
I am a bit confused. I would think your scenario would have to be running for the Cursor object to blink (animate itself). You make it sound like the scenario is paused when you are dragging it into your world -- and it is blinking at that time?
Pando Pando

2018/12/30

#
Yes thats what i would think as well, that the object would only blink if the scenario was running. But it only blinks while im dragging it onto the world, but as soon as i place it, it never blinks again, whether the scenario is running or not.
danpost danpost

2018/12/30

#
Pando wrote...
Yes thats what i would think as well, that the object would only blink if the scenario was running. But it only blinks while im dragging it onto the world, but as soon as i place it, it never blinks again, whether the scenario is running or not.
Please upload the scenario with source for review (if you do not mind).
Pando Pando

2018/12/30

#
Posting it right now, its not even close to being finished at the moment.
Pando Pando

2018/12/30

#
danpost wrote...
Pando wrote...
Yes thats what i would think as well, that the object would only blink if the scenario was running. But it only blinks while im dragging it onto the world, but as soon as i place it, it never blinks again, whether the scenario is running or not.
Please upload the scenario with source for review (if you do not mind).
And it seems on any computer i play the scenario i posted it is stuck on initialing... is that because the scenarios too big?
danpost danpost

2018/12/30

#
Pando wrote...
as soon as i place it, it never blinks again, whether the scenario is running or not.
I did a close inspection of the images in the gif for the cursor. Both images appear to be the same visible cursor image. I cleared the second (of the two) image of the gif and the cursor then blinks. You can add the following line at the end of the Cursor class constructor:
1
gif.getImages().get(1).clear();
Pando Pando

2018/12/31

#
danpost wrote...
Pando wrote...
as soon as i place it, it never blinks again, whether the scenario is running or not.
I did a close inspection of the images in the gif for the cursor. Both images appear to be the same visible cursor image. I cleared the second (of the two) image of the gif and the cursor then blinks. You can add the following line at the end of the Cursor class constructor:
1
gif.getImages().get(1).clear();
Thanks for the help, it still did't work by i ended up just using transparency to blink the line. However, i have another question. When i press down the key "a" a black dot is suppose to pop up (to hide the letter) and then adds the letter to an arraylist with a max of 10 letters. However, when i press "a" once 10 black dots pop up and i assume the arraylist is 10 letters "a"s as well. Any tips on getting it to add one black dot and one letter to the arraylist when i click "a"?
Pando Pando

2018/12/31

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public void getWord()
    {
        while (word.size() < 10 && dotReq == false)
        {
            getWorld().addObject(cursor,353,118);
             
            if (Greenfoot.isKeyDown("a"))
            {
                word.add("a");
                getWorld().addObject(dots.get(y), x,118);
                y = y + 1;
                x = x + 40;
            }
            if (Greenfoot.isKeyDown("backspace"))
            {
                if (word != null && word.isEmpty())
                {
                    word.remove(word.size() - 1);
                }
            }
             
            if (Greenfoot.isKeyDown("enter"))
            {
                dotReq = true;
            }
        }
    }
danpost danpost

2018/12/31

#
Pando wrote...
Thanks for the help, it still did't work by i ended up just using transparency to blink the line.
Well, I am sure it did work. If you delete all objects from the world and then add a Cursor object, when run is clicked, it will blink.
When i press down the key "a" a black dot is suppose to pop up (to hide the letter) and then adds the letter to an arraylist with a max of 10 letters. However, when i press "a" once 10 black dots pop up and i assume the arraylist is 10 letters "a"s as well. Any tips on getting it to add one black dot and one letter to the arraylist when i click "a"? << Code Omitted >>
Before dealing with the key input, you should probably note that you are adding an abundance of Cursor objects into your world (line 5 is within the while loop and should not be). For the input, nstead of using isKeyDown, use getKey. The isKeyDown method is more for game play while the getKey method is more for text input. Something like this inside your loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
String key = Greenfoot.getKey();
if (key != null)
{
    if ("a".equals(key)
    {
        // ...
    }
    if ("backspace".equals(key)
    {
        // ...
    }
    // etc.
}[/quote]
I really do not think you need a [i]while [/i]loop at all.  Also, it ties the system down due to needing user input during the loop.  That is, once the loop starts, the user cannot pause the scenario until the loop is broke out of.  Generally speaking, you are dealing with two phases -- the input phase and the solving (game play) phase.  Use a [i]boolean [/i]to track which phase is currently running and let the act method do the looping for you.  Then, your act method would be like:
[code]if (solving) solve(); else getWord();
and the getWorld method would be more like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void getWord()
{
    String key = Greenfoot.getKey();
    if (key == null) return;
    if ("enter".equals(key) && !world.isEmpty())
    {
        solving = true;
        // prep solving phase
    }
    else if ("escape".equals(key)) Greenfoot.stop();
    else if ("backspace".equals(key) && !word.isEmpty())
    {
        word.remove(word.size()-1);
        // remove dot
        // move cursor left
    }
    else if (key.length() == 1 && word.size() < 10)
    {
        word.add(key);
        // add dot
        // move cursor right
    }
}
Instead of a boolean (solving), you could just see if a cursor is in the world. If so, get word; if not, solve (I would think the cursor would be removed once the word is entered)
Pando Pando

2018/12/31

#
Oh sweet thanks so much! Another question, my constructor is filled with new objects being created and added to their arraylist (10 dots and 10 lines), but it looks very crowded. Is there anyway to create a object and assign it to the arraylist within a loop? I attempted to myself but couldn't figure it out:/
danpost danpost

2018/12/31

#
Pando wrote...
Oh sweet thanks so much! Another question, my constructor is filled with new objects being created and added to their arraylist (10 dots and 10 lines), but it looks very crowded. Is there anyway to create a object and assign it to the arraylist within a loop? I attempted to myself but couldn't figure it out:/
Do you mean this?:
1
2
ArrayList<> arrayList = new ArrayList<>();
for (int i=0; i<10; i++) arrayList.add(new ObjectName());
Pando Pando

2019/1/1

#
danpost wrote...
Pando wrote...
Oh sweet thanks so much! Another question, my constructor is filled with new objects being created and added to their arraylist (10 dots and 10 lines), but it looks very crowded. Is there anyway to create a object and assign it to the arraylist within a loop? I attempted to myself but couldn't figure it out:/
Do you mean this?:
1
2
ArrayList<> arrayList = new ArrayList<>();
for (int i=0; i<10; i++) arrayList.add(new ObjectName());
kinda ya, does the object you are creating within the loop not have to have a specific name?
Pando Pando

2019/1/1

#
nvm i used what you just posted and it seems to work (give me no errors), I havent ran the code yet but seems good!
Pando Pando

2019/1/1

#
Is there a way to make an array of objects from DIFFERENT classes? Example. If i had multiple classes A, B, C... Z. Could i have an array that has something like
1
2
alphabet[0] = new A();
alphabet[1] = new B();
There are more replies on the next page.
1
2