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

2023/11/9

Help With A Doodle Game.

yesineedhelp yesineedhelp

2023/11/9

#
As a normal Doodle Jump Game, the doodle jumps, however, the game is sideways. As for a unique power up, when Doodle touchs a Car, it allows the player to "driver" (move) the car (image with doodle in a car). Since this is a power up, it could only be use for a certain time. Here is what I have so far, this is written in my Car Class:
private final int carDuration=10;
     private int carTime;
     
     public void act() 
     {
        move();
  
       checkCar();
       
     }
   
     public void checkCar()
     {
        
         if(isTouching(Doodle.class))
        {
           removeTouching(Doodle.class);
           checkKeys();
           carTime = carDuration;
        } else if(carTime > 0)
        {
            if(--carTime==0)
            {
                int x = getOneIntersectingObject(Car.class).getX();
                int y = getOneIntersectingObject(Car.class).getY();
                Doodle doodle = new Doodle();
                getWorld().addObject(doodle, x, y);
            }
        }
     }
     
     public void checkKeys()
     {
        if(Greenfoot.isKeyDown("right"))
        {
            moving(true);
        }else moving(false);
        if(Greenfoot.isKeyDown("up"))
        {
            setLocation(getX(), getY() - 5);
        }
         if(Greenfoot.isKeyDown("down"))
        {
            setLocation(getX(), getY() + 5);
        }
     }
   
     public void moving(boolean movingFoward)
     {
       if(movingFoward == true)
       {
           setImage("CarDoodle.png");
           move(5);
        }else
        {
            setImage("DoodleInCar.png");
        }
      }
    }    
I know I definitely mess something up, like the "isTouching" method, and something else, probably the order of my code; however, I have no idea how to fix it. If you can help with a example of the code, that will be helpful and I will really appreciate it!
danpost danpost

2023/11/10

#
yesineedhelp wrote...
Here is what I have so far, this is written in my Car Class:
 
public void act()
{
    move();
    
    checkCar();
}
Where is the move method code? ... and shouldn't the Car object be removed from the world when the carTime expires?
yesineedhelp yesineedhelp

2023/11/10

#
The move code comes from the Mover Class, which includes these information:
public void move() 
    {
        //get the current speed
        MyWorld world = (MyWorld) getWorld();
        int speed = world.speed;
         //move to the left by the distance given by the speed
         setLocation(getX()-speed,getY());
 
    }    
And yes, the car should be remove and normal Doodle is added back in when the time is up, I just didn't add that yet. Thanks for reminding me on that part. Any other help will be appreciate for how to move the Car when the Doodle touches it! Basically, when the Doodle touchs the Car, a image of Doodle in the Car will appear, and then I will like to move it, but that is the problem.
danpost danpost

2023/11/14

#
Here are a couple helpful thoughts: (1) In your world constructor ("public MyWorld()" block), add the following line so the Doodle object is drawn on top of all other actors:
setPaintOrder(Doodle.class);
(2) The Doodle object can hold a reference to the car and therefore control it, when needed. This will require the Doodle object act after the Car object; so, also in the world constructor, add the following line:
setActOrder(Car.class);
Since the Doodle is the user-controlled actor, the interaction with Car objects should be coded in the Doodle class. Hence, the Car class can be simplified to the following:
public Car extends Mover
{
    public void act() {
        move();
    }
}
With the following items in the Doodle class:
private static final int CAR_TIME = 300; // adjust as needed (currently set to approx. 6 secs.)

private Actor car = null; // reference field for the car doodle is in
private int carTime = 0; // act steps remaining to be with car
As one of the last steps in or through the act method, you could have the following:
if (carTime == 0) {
    car = getOneIntersectingObject(Car.class);
    if (car != null) carTime = CAR_TIME;
}
else {
    car.setLocation(getX(), getY());
    car.setRotation(getRotation());
}
You need to login to post a reply.