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

2020/2/22

Displaying an Image when a key is pressed

DaRafster DaRafster

2020/2/22

#
I've got a ufo beam actor in my world, and I would like it so when I press the "space" key, the image is displayed, sort of like an on and off switch. When you press on space, the actor appears, when you press on space again the actor disappears, how would I do this? Here is my code that is displaying my actor(s) in the world (world class):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (------------) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public MyWorld()
    {    
        
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(700, 500, 1); 
        
        // Adjusting the size of the image so it scales exactly to the world
        GreenfootImage bg = new GreenfootImage("Flat Terraria Landscape.jpg"); 
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
        
        martianSaucer(getWidth()/2, getHeight()/10);
        ufoBeam(347, getHeight()/2);
        
        //backgroundMusic();
    }
    
    public void martianSaucer(int width, int height)
    {
        
        MartianSaucer msPlacement = new MartianSaucer();
        addObject(msPlacement, width, height);
   
    }
    
    public void ufoBeam(int width, int height)
    {

           UFOBeam ufoBeamPlacement = new UFOBeam();
           addObject(ufoBeamPlacement, width, height);
        
    }
    
    public void backgroundMusic()
    {
        
        GreenfootSound backgroundMusic = new GreenfootSound("Martian Madness.mp3");
        backgroundMusic.setVolume(15);
        backgroundMusic.playLoop();
        
    }
    
    
}
Here is my code for the actor I want to appear and disappear every time space is pressed:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class UFOBeam here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class UFOBeam extends Actor
{
    /**
     * Act - do whatever the UFOBeam wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public UFOBeam()
    {
        
       GreenfootImage image = getImage();  
       image.scale(70, 350);
       setImage(image);
         
    }
    
    public void act() 
    {
        movingWithArrows();// Add your action code here.
    }    
    
    public void movingWithArrows()
    {
        
        int x = getX();
        
        if(Greenfoot.isKeyDown("left")) 
        {
            move(-3);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            move(3);
        }
        
    }
}
I know I would need to create an if statement to use the space key, but I just do not know where to put it, and what code should be in it. (I'm still at a beginner level of knowledge, keep that in mind)
Archerfish Archerfish

2020/2/22

#
Hi DaRafster! Here is a code demo for toggling between a "bee" and "no bee":
import greenfoot.*;

public class Bee extends Actor
{
    GreenfootImage bee = new GreenfootImage("bee.png"); //Image variable for image with bee
    GreenfootImage noBee = new GreenfootImage("noBee.png"); //Image variable for image without bee
    boolean displayImage = true; //Variable that defines whether to display a bee or no bee

    String activationKey = "1"; //Key that needs to be pressed to toggle on and off
    boolean keyPressed = false; //Variable for whether the activation key is currently pressed
    boolean prevKeyPressed = false; //Variable for whether the activation key was pressed in the previous iteration of the act method

    //ACT METHOD
    public void act()
    {
        keyEvents(); //Calls keyEvents()
        display(); //Calls display()
    }

    //KEY EVENTS
    public void keyEvents() {
        keyPressed = false; //By default, we consider that the activation key is not pressed
        if(Greenfoot.isKeyDown(activationKey)) //If the activation key is down...
            keyPressed = true; //Then indicate that the activation key has now been pressed

        /*If the key is down now and it wasn't down before, then we know that it was just pressed
         *NOTE: we only toggle the image if the key was just pressed
         *If we toggle it anytime the activation key is down, then we would have a weird flickering effect (since it continuously toggles)
         */
        if(keyPressed && keyPressed != prevKeyPressed)
        {
            displayImage = !displayImage; //Toggle whether to display bee or not
        }
        prevKeyPressed = keyPressed; //Set the current ket activity as past key activity
    }

    //DISPLAY
    public void display()
    {
        if(displayImage) //If we need to display the bee...
        {
            setImage(bee); //Set the image to bee
        }
        else //Otherwise...
        {
            setImage(noBee); //Set the image to no bee
        }
    }
}
Cheers, Archerfish
footpickle footpickle

2020/2/22

#
put this:
// world class variables (or fields)
UFOBeam ufobeam; // to hold the object
int ufobeamX, ufobeamY; // to hold the removal location of the object
// when creating UFOBeam object (in constructor of world)
ufobeam = new UFOBeam();
addObject(ufobeam, whateverX, whateverY);
// when removing 'ufobeam'
ufobeamX = ufobeam.getX();
ufobeamY = ufobeam.getY();
removeObject(ufobeam);
// when re-adding 'ufobeam'
addObject(ufobeam, ufobeamX, ufobeamY);
and replace the "whateverX" and "whateverY" with the coordinates of the ufobeam
footpickle footpickle

2020/2/22

#
By the way i copied this from an old discussion so if it doesnt work then sorry but i dont know. (i'm also new )
DaRafster DaRafster

2020/2/22

#
thank you guys for your help, will keep this future knowledge in mind
You need to login to post a reply.