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

2024/11/5

Making an action happen when an actor lands on another actor's specific string?

trulydevious trulydevious

2024/11/5

#
I'm not sure if this is even possible, but basically I am making a board game type game (I also made another post about it but got help on that thing) and I need specific words to appear on the screen when the player lands on the "Pink" string I'm stuck. Do I need to make an entirely different class for the Pink squares in order for it to happen? It needs to be random text, not the same text every time. I believe the code for a Pink class would be very similar to my Move class, which is the code that tells you where to move. If it helps, here's some of the code. Move
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.Random;

/**
 * Write a description of class Move here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Move extends Actor {
    private String[] texts = {"Move -1", "Move 1", "Move 2", "Move 3", "Don't Move"};
    private boolean isVisible = false;

    public Move() {
        updateText("");
    }

    public void showRandomText() {
        if (!isVisible) {
            Random rand = new Random();
            String currentText = texts[rand.nextInt(texts.length)];
            updateText(currentText);
            isVisible = true;
            Greenfoot.delay(60);
            hideText();
            isVisible = false;
        }
    }

    private void hideText() {
        updateText("");
    }

    private void updateText(String text) {
        GreenfootImage image = new GreenfootImage(150, 50);
        image.setColor(new Color(0, 0, 0, 0));
        image.clear();
        image.setColor(Color.BLACK);
        image.drawString(text, 10, 30);
        setImage(image);
    }
}
How I added Move in the Board
public void act(){
        //Make it to where when you press the "space" key, it shows the Move text
        if (Greenfoot.isKeyDown("space")) {
            move.showRandomText();
        }
    }
The strings for the colors (Pink has it's own)
public static String[] colors = {"Red", "Purple", "Yellow", "Blue", "Orange", "Green", "Cyan"};
    public static String[] pink = {"Pink"};
A snippet showing how I added the spaces and their strings
Space space105 = new Space("Purple");
        addObject(space105, 15, 17);
        Space space106 = new Space("Red");
        addObject(space106, 14, 17);
        Space space107 = new Space("Pink");
        addObject(space107, 13, 17);
        Space space108 = new Space("Green");
        addObject(space108, 12, 17);
        Space space109 = new Space("Orange");
        addObject(space109, 11, 17);
        Space space110 = new Space("Blue");
        addObject(space110, 10, 17);
        Space space111 = new Space("Yellow");
danpost danpost

2024/11/5

#
trulydevious wrote...
Do I need to make an entirely different class for the Pink squares in order for it to happen? It needs to be random text, not the same text every time. I believe the code for a Pink class would be very similar to my Move class, which is the code that tells you where to move.
I would just modify the Move class to accommodate that Pink has its own set of texts to use.
trulydevious trulydevious

2024/11/7

#
Ohhh thank you that makes a lot of sense
You need to login to post a reply.