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

2014/8/24

stopping and then continuing array of images

1
2
davemib123 davemib123

2014/8/24

#
Hi, In my game I am using images that have some text. I have got it so that the images stop at a certain point, but not sure on how to get the images to continue on once I press the enter key. This is what I have so far:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Text here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Text extends Tiles
{
    private GreenfootImage[] Conversation1 = new GreenfootImage[7];
    private GreenfootImage[] Conversation2 = new GreenfootImage[3];
    private String fileName;
    private String fileNameDirectory;
    private int animationDelay = 30;
    private int animationDelayCounter;
    String fileNameSuffix = ".gif";

    public Text(String selection)
    {
        this.selection = selection;
        if (selection == "Conversation1")
        {
            this.fileNameDirectory = "Text/Conversation/1/";
            for (int i = 0; i < Conversation1.length; i++)
            {
                fileName = fileNameDirectory + (i+1) + fileNameSuffix;
                palletTownSign1[i] = new GreenfootImage(fileName);
            }
        }
        else if (selection == "Conversation2")
        {
            this.fileNameDirectory = "Text/Conversation/2/";
            for (int i = 0; i < Conversation2.length; i++)
            {
                fileName = fileNameDirectory + (i+1) + fileNameSuffix;
                palletTownSign2[i] = new GreenfootImage(fileName);
            }
        }
    }

    /**
     * Act - do whatever the Text wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        animationDelayCounter++;
        if(animationDelayCounter == animationDelay)
        {
            animationCounter++;
            animationDelayCounter = 0;
        }
        if (selection == "Conversation1")
        {
            if (animationCounter > Conversation1.length - 6)
            {
                animationCounter = Conversation1.length - 6;
            }      
            setImage (Conversation1[animationCounter]);
        }
    }  
}
    
Artyoum Artyoum

2014/8/24

#
Try using the getKey() method to see if the individual presses enter. If the individual does, delete the old text image and load the new one. Sadly, I am still trying to figure out how to use the get key method. Hopefully this helped you a bit.
Super_Hippo Super_Hippo

2014/8/25

#
Since I don't know where in your code you stop the execution of the images and I don't really like the 'getKey' method, you could have a boolean to save whether or not the images are stopped right now and if they are, you can check if the user is pressing 'enter' right now and if he does, you continue.
if (stopped)
{
    //the code you given comes here I guess
}
else
{
    if (Greenfoot.isKeyDown("enter"))
    {
        stopped = false;
        //continue the execution, probably something similar as your way to stop it
    }
}
danpost danpost

2014/8/25

#
Super_Hippo wrote...
I don't really like the 'getKey' method,
Do not knock the 'getKey' method -- it does have its useful place. Normally, if you are trying to detect a specific key, then using 'isKeyDown' is preferred; and usually that is the case in gameplay. But, when any one of multiple keys are to be detected, such as with string input, the 'getKey' method is the way to go.
davemib123 davemib123

2014/8/25

#
This is how I have it so far:
animationDelayCounter++;
        if(animationDelayCounter == animationDelay)
        {
            animationCounter++;
            animationDelayCounter = 0;
        }
        if (selection == "Conversation1")
        {   
            if (animationCounter > Conversation1.length - 1)
            {
                animationCounter = Conversation1.length - 1;
            }
            else if (Greenfoot.isKeyDown("enter"))
            {
                animationCounter = Conversation1.length - 1;
                delay--;
            }
            setImage (Conversation1[animationCounter]);
            if (delay <= 0 && Greenfoot.isKeyDown("enter"))
            {
                getWorld().removeObject(this);
            }
        }
I want to add Conversation part2 which is basically the same sort of thing. but each time I try this after the object is removed:
getWorld().addObject(new Text("Conversation2"), getWidth() / 2, 309);
the animationCounter has already incremented itself to Conversation2.length - 1 and the final image is shown. How can I get the animationCounter back at 0?
Super_Hippo Super_Hippo

2014/8/25

#
Where is the animationCounter variable declared? Check if it is 'static' and if it is, remove the 'static'. Static means that it is just one variable for the class and not for an object. So if you change a static variable, it is set to this value for every object of the class which is in the world or will be created in the future.
danpost danpost

2014/8/25

#
If your first conversation is working properly, then all you need to do is set up a field to hold which conversation is active (possibly a String array) and have a 'setCoversation' method to change it (and reset the fields to start the conversation). That way, instead of 'Conversation1' and 'Conversation2', you can set 'conversation' to either one and work it in the code.
davemib123 davemib123

2014/8/25

#
@Super_Hippo the animationCounter is in Tiles.class which is a parent class to Text.class. It isn't static, it is declared as:
protected int animationCounter;
@danpost is this what you meant for the string array:
String[] array = new String[] {"Conversation1", "Conversation2"};
not really sure on how to setup the setConversation method
Super_Hippo Super_Hippo

2014/8/25

#
Then I am wondering why it isn't 0 if you create a new instance of 'Text'. Did you change something in the constructor? Does it change anything if you put 'animationCounter=0;' in the constructor of the 'Text' class? If your conversation names will be nothing else than 'ConversationX' with X being a number, then you could also save it as in int.
davemib123 davemib123

2014/8/25

#
if I add them in manually using the interface they work fine. The constructor already has animationCounter as 0.
Super_Hippo Super_Hippo

2014/8/25

#
Did you change the constructor? Because you didn't have it in the first post. Actually this shouldn't matter anyway... It is outside the 'if's, right? What happens if you create another Text with the first conversation or create the second conversation first?
davemib123 davemib123

2014/8/25

#
this is the constructor:
 public Text(String selection)
    {
        this.selection = selection;
        this.animationDelayCounter = 0;
        this.animationCounter = 0;
        this.delay = 10;

        for (int i = 0; i < Conversation1.length; i++)
        {
            this.fileNameDirectory = "Text/Conversation1/";
            fileName = fileNameDirectory + (i+1) + fileNameSuffix;
            Conversation1[i] = new GreenfootImage(fileName);
        }

        for (int i = 0; i < Conversation2.length; i++)
        {
            this.fileNameDirectory = "Text/Conversation2/";
            fileName = fileNameDirectory + (i+1) + fileNameSuffix;
            Conversation2[i] = new GreenfootImage(fileName);
        }
}
Constructor is out the ifs. If its created via the GUI its fine no problems.
danpost danpost

2014/8/25

#
I may have gotten a little ahead of myself, there. I see you have some GreenfootImage arrays already in the class. However, you only initialize the array. You do not ever put any actual images in the arrays. You are only using them for getting the number of images in the animations.
danpost danpost

2014/8/25

#
@Super_Hippo, the field you are trying to find is probably declared in the Tile class (the superclass of the class this code is in).
danpost danpost

2014/8/25

#
@davemib123, I am going to check something out and get back to you.
There are more replies on the next page.
1
2