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

2012/10/21

Looping through a string

mickyg mickyg

2012/10/21

#
I'm creating a scenario where I have a list of words in a string. The first word must drop to the bottom of the screen and disappear and then the second word appears at the top and also falls following the same process for the entire string. Unfortunately, I cannot seem to get my loop to work properly as I don't know how to make the iteration of the loop wait and allow the method to complete before cycling through to the next iteration of the loop. Here is what I have so far for code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Words here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Words extends Actor
{

    private String word;    
    private String[] randomWords = {"object"};//, "method", "class", "import", "greenfoot", "tutor", "boolean", "typing", "variable", "inheritance", "world", "actor" };

    /** 
     * 
     */
    public Words(String randomWords)
    {

    } 

    /**
     * Act - do whatever the Words wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        for(int i = 0; i < randomWords.length; i++) {
            word = (randomWords[i]);
            setImage(new GreenfootImage(word,30,Color.WHITE, Color.BLACK));
            if (canMove())
                setLocation(getX(), getY() +2);
            else
                getWorld().removeObject(this);

        }
    }

    /**
     * Test if the letter can move forward. Return true if it can, false otherwise.
     */
    public boolean canMove()
    {
        World myWorld = getWorld();
        int y = getY();
        y++;

        if (y >= myWorld.getHeight()-100)
            return false;
        return true;
    }

}
Can somebody please help me with this. Thank you in advance!
danpost danpost

2012/10/21

#
Instead of creating a new object for each word, have the initial Words object change images for the the next word and relocate back to the top of the screen. Also, you are treating randomWords as an array of String objects, but bringing it in (through the constructor) as a single String object. That will need corrected. Without all the comments:
import greenfoot.*;
import java.awt.Color;

public class Words extends Actor
{
    private int wordNum;    
    private String[] randomWords;

    public Words(String[] randWords)
    {
        randomWords = randWords;
        updateImage();
    } 

    private void updateImage()
    {
        setImage(new GreenfootImage(randomWords[wordNum], 30, Color.WHITE, Color.BLACK));
    }

    public void act() 
    {
        setLocation(getX(), getY() +2);
        if (getY() < getWorld().getHeight() - 100) return;
        wordNum++;
        if (wordNum < randomWords.length) 
        {
            updateImage();
            setLocation(getX(), 100);
        }
        else getWorld().removeObject(this);
    }
}
I removed the canMove method, since I used its location to determine when to change the word and relocate the object (or remove it).
mickyg mickyg

2012/10/22

#
Thank you very much for the quick reply that makes sense. My issue now is adding the object to the world. Due to the parameters in the Words constructor I am getting the following error when using addObject: constructor Words in class Words cannot be applied to given types; ..... I'm not sure what I need in the brackets to make this compile. addObject( new Words(), 100, 50 );
danpost danpost

2012/10/22

#
You Words constructor needs to accept a String array to work off of (line 9 of code above). To send the String array to the constructor we need to create it and then call the constructor by creating a new Words object with the String array.
// add the following instance String array to your world class
String[] words = {"object", "method", "class", "import", "greenfoot", "tutor", 
                  "boolean", "typing", "variable", "inheritance", "world", "actor" };
// then to add the Words object from your world, use
addObject(new Words(words), 100, 50);
mickyg mickyg

2012/10/22

#
Thanks again! It's very much appreciated as I have spent a fair bit of time trying to figure this out.
You need to login to post a reply.