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

2013/11/2

Can objects turn into other objects?

Aermus Aermus

2013/11/2

#
What i want is the object Car.class too turn into the object StolenCar.class after i use the button "e" to steal it. Is this possible? This is what i have so far...
public class Autodief extends Speler{
    private int count = 0;
    private boolean foundcollison;
    private int character = 0;
    
    public void act(){        
        SpelerBesturing();        
        Stelen();
        bestuurder();
    }
    
    public void Stelen(){
        if (Greenfoot.isKeyDown("e") && character == 0 && bestuurder()){
            character = 1;
            getWorld().removeObject(this);
        }
    }
    
    public boolean bestuurder(){
        Actor StolenCar = getOneIntersectingObject(Burgerauto.class);
        
        if(StolenCar != null){
            return true;
        }        else {
            return false;
        }
    }
    
    public boolean foundcollison(){
        Actor Politieauto_move = getOneObjectAtOffset(0, 0, Burgerauto.class);
        
        if(Politieauto_move != null) {
            return true;
        }        else {
            return false;
        }
    }         
}
Aermus Aermus

2013/11/2

#
And it is supposed to turn into this class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import javax.sound.midi.Sequencer;
import javax.sound.midi.Sequence;

/**
 * Write a description of class GestolenAuto here.
 * 
 * een gestolen auto is een auto die de autodief heeft gestolen. 
 * 
 *Groep J (Kasim, Richard & Wesley)
 * Versie4 2/11/2013
 */
public class GestolenAuto extends Burgerauto{
    private boolean bestuurder;
    
    /**
     * Act - do whatever the GestolenAuto wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(){
        SpelerBesturing();        
    }
    
    
    
    public boolean bestuurder(){
        Actor Autodief = getOneIntersectingObject(Autodief.class);
        
        if(Autodief != null) {
            return true;
        }        else {
            return false;
        }
     }    
}
Yogibaer Yogibaer

2013/11/4

#
Hello Aermus, you wrote:
What i want is the object Car.class too turn into the object StolenCar.class
my hint is to think in this way: "What I want is an object of the car.class should turn the character "Owner" from value "Origin" to value "Rubber". Than connect the value "Rubber" with a special picture (may be only one point for remembering) in a special "rubbered car parking" or any thing else. I hope this helps greetings from Yogibaer
davmac davmac

2013/11/5

#
You can't change the class of an existing object, but you have some other options:
  • remove the Car object and replace it with a StolenCar
  • Do not use two classes, and use logic in the Car to behave different depending on whether it is stolen or not. A boolean field can track whether the car is stolen.
You need to login to post a reply.