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

2015/4/30

Stop specific actor

bennordin21 bennordin21

2015/4/30

#
Hello, I have a part of my game that allows you to type in your name. After you type your name and press enter, I want the actor to stop all of the code in it's class. Then after that, the game starts and you move other actors using the key board.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.util.Scanner;
/**
 * Write a description of class P1Input here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class P1Input extends Input
{
    static final int MAX_INPUT_LENGTH = 20;
    String text = "P1 Name";
    boolean used;
    boolean on;
    public static final Color transparent = new Color(0,0,0,0);
    public P1Input()
    {
        updateImage();
        text = "";
        on = true;
    }

    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(15*MAX_INPUT_LENGTH, 30);
        image.setColor(new Color(128, 0, 0));
        image.fill();
        image.setColor(Color.lightGray);
        image.fillRect(3, 3, image.getWidth()-6, 24);
        GreenfootImage textImage = new GreenfootImage(text, 24, Color.black, null);
        image.drawImage(textImage, (image.getWidth()-textImage.getWidth())/2, 15-textImage.getHeight()/2);
        setImage(image);
    }

    public void act()
    {
        changeImage();
        used = false;
        int i = 1;
        if (!used && i == 1)
        {
            String key = Greenfoot.getKey();
            if (key == null)
            {
                return;
            }

            if ("backspace".equals(key) && text.length() > 0) 
            {
                text = text.substring(0, text.length() - 1);
            }
            if ("escape".equals(key)) 
            {
                text = "";
            }
            if ("space".equals(key))
            {
                key = " ";
            }
             if (key.length() == 1 && text.length() < MAX_INPUT_LENGTH) text += key;
            updateImage();
            
        }
        if (Greenfoot.isKeyDown("enter") )
        {
            used = true;
            P2Input p2Input = new P2Input();
            setImage(new GreenfootImage(getText() + "'s Lives:", 30, Color.black, transparent));
            if(on)
            {
            on = false;
            getWorld().addObject(p2Input, 750, 25);
            
        }
            
        }
    }
    public String getText()
    {
        return text;
    }
    
   
    
    
    
    
    
    
    
    
    
    
}
danpost danpost

2015/5/1

#
If it is this class that is to stop acting, insert the following as the first line in the act method:
if (!getWorld().getObjects(P2Input.class).isEmpty()) return;
You need to login to post a reply.