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

2013/10/27

How do i switch from character

Aermus Aermus

2013/10/27

#
Is saw this GTA game were a thief steals a car. With button "E" the opbject thief disappeard and you had control over the car.. and when you pressed E again yu get out of the car. I want to us this code for our game aswell.. Which code should i use? Actor -> Thiefs -> civiliancars So by pressing E, you get into the civiliancars but have the same controls as the thief.. please help me..
Sniper Sniper

2013/10/27

#
Here's what I might do: Make them both a subclass of let's call it Control. In control:
public int character = 0;
in Thiefs add a paramater for the character = 0 like so:
if(Greenfoot.isKeyDown("up") && character == 0){
move(4);
blah blah blah...
}

if(Greenfoot.isKeyDown("e") && character == 0 && isNearCar()){
character = 1;
getWorld().removeObject(this);
}
Then have all the controls for the car in thief and when you want to get out:
if(Greenfoot.isKeyDown("up") && character == 1){
move(4);
blah blah blah...
}

if(Greenfoot.isKeyDown("e") && character == 1){
character = 0;
getWorld().addObject(new Thiefs(), getX(), getY());
getWorld().removeObject(this);
}
Aermus Aermus

2013/10/27

#
Does this mean i need to make a new method called isNearCar?.. because greenfoot gives me the message.. cannot find symbol..
Sniper Sniper

2013/10/27

#
Not necessarily, that was just an example boolean. I'm guessing you only want the thief thief be able to get in a car when near it yes? I'd use getOneIntersectingObject(Actor) in a boolean.
public boolean isNearCar(){
    if(getOneIntersectingObject(Car.class) != null){
        return true;
    }
    else{
        return false;
    }
Aermus Aermus

2013/10/28

#
public int character = 0; Do i only put the public int character = 0; in de control class? or also in the thief class and the car class?
danpost danpost

2013/10/28

#
I would put the movement code for each in its own class (the car and the thief) because they do not move the same way. Also, I would add a reference to a 'private Thief driver = null;' in the Car class. Have the thief look for a car and, when found and key 'E' is released, get a reference to the intersecting car, set its driver to 'this' thief and remove the thief from the world. When the car is stopped and 'E' is released again, add the driver back into the world and set the driver of the car back to null. The movement code for the thief will not execute while it is not in the world; however, the car is always in the world and its movement code should be restricted by the condition that the driver is not null.
Aermus Aermus

2013/10/29

#
Is this correct? I put a private in character in the class speler == control..
public class Speler extends Actor{
    private int speed = 5;
    public int character = 0;
    public int maxspeed;
the i put the controls in the thief, like so
public class Autodief extends Speler{
    private GreenfootImage dief;
    private GreenfootImage dief1;
    private GreenfootImage dief2;
    private int count = 0;
    private boolean isDown;
    private boolean canSee;
    private boolean foundcollison;
    private int score;
   
    
    public void act(){        
        driver();
        
        
        if (foundcollison()){
          
        }
          if (Greenfoot.isKeyDown ("up")&& character == 0)
        {
            move(1);
            
            if (getImage() == dief1){
                setImage(dief2);
            }else{
                setImage(dief1);
                            }
                    
                   
        }    
          
        
        
        if (Greenfoot.isKeyDown("down")&& character == 0)
        {
            move (-1);
            if (getImage() == dief){
                setImage(dief1);
            }else{
                setImage(dief);
                            }
            
            
             
        }
        
        if (Greenfoot.isKeyDown("left")&& character == 0) 
        {
            turn (-5);
            
        }
        
        if (Greenfoot.isKeyDown("right")&& character == 0) 
        {
            turn (5);
          
        }
       
        if (Greenfoot.isKeyDown("e") && character == 0 && driver())
        {
            character = 1;
            getWorld().removeObject(this);
        }
       
         
              
        }
      
    public boolean driver()
     {
        Actor StolenCar = getOneIntersectingObject(StolenCar.class);
        
        if(StolenCar != null) {
            return true;
        }        else {
            return false;
        }
    } 
And for the car.. is used the following control
public class StolenCar extends Speler

What is does now is remove the thief from the world but not switch to control car..
{
   public boolean driver;
   private int character = 1;
   private int score = 0;
   private int speed = 5; 
   private int isingarage = 2;

public void PressKey(){
         if (Greenfoot.isKeyDown ("y")&& character == 1)
        {
            move(1);
            
               
        
        if (Greenfoot.isKeyDown("h")&& character == 1)
        {
            move (-2);
           
            
            
             
        }
        
        if (Greenfoot.isKeyDown("g")&& character == 1) 
        {
            turn (-5);
            
        }
        
        if (Greenfoot.isKeyDown("j")&& character == 1) 
        {
            turn (5);
          
        }
       
        if (Greenfoot.isKeyDown("e") && character == 1 )
        {
         character = 0;  
          getWorld().addObject(new Autodief(), getX(), getY());  
           
             }
         
          }    
        
        }
You need to login to post a reply.