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

2018/3/7

Show only one object of a class after hidden

Rapoo Rapoo

2018/3/7

#
Hello, I have an object called car. This is spawned several times. If I hit my car I hide it with getImage().clear();. Only the car which was hit will be hidden. If I want to show one selected car again every hidden car will be shown(with: setImage("car.png");) is there a possibility to show only one car. All the other cars which were hit should not be shown. Do you now what I mean? I need help:D Thanks!
Super_Hippo Super_Hippo

2018/3/7

#
Show the Car class and explain which car should be shown again.
Rapoo Rapoo

2018/3/7

#
There are several cars on the map. If I press f and I touch a car, this car will not be there anymore(getImage().clear). I repeat this several times. If ´the boolean prüfecrash only one car should be shown again. Am I able to do this?
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Car here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Car extends Actor
{
    /**
     * Act - do whatever the Car wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Mensch.prüfecrash){
        this.setImage("car.png");
        
    }
       if(((Greenfoot.isKeyDown("f")) && (isTouching(Mensch.class) && ((Mensch.true==false)) ))){ //Wenn x gedrückt und Holz von der Klasse Mensch betührt wird, dann

        getImage().clear();//entferne das Bild
        Mensch.prüfecrash=false;
    }  
}
}
danpost danpost

2018/3/7

#
You can set the boolean back to false:
public void act()
{
    if (Mensch.prüfecrash)
    {
        setImage("car.png");
        Mensch.prüfecrash = false;
    }
    // etc. 
Super_Hippo Super_Hippo

2018/3/8

#
What is 'Mensch.true==false' doing?
danpost danpost

2018/3/8

#
Instead of using 'clear' on the image, it might be better to set the image of the actor to 'null'
setImage((GreenfootImage)null);
It can then be easily checked as to whether it is showing or not:
if (getImage() == null)
You need to login to post a reply.