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

2020/5/22

parking simulator problem with parking lot

1
2
minetruck minetruck

2020/5/22

#
hi i am doing a parking game, where you have to park a car on a parking lot. the parking lot is a red area in which the car has to park on. when the car is parked sucessfully, the new level starts. my question is: which method do i need for the car/parking lot? isTouching does not work because the car has to be on the parking lot, not only touching it on the side. thanks for help
danpost danpost

2020/5/22

#
Might be easiest to put up invisible guard rails (or fencing) around the lot. Then isTouching would not be a problem.
minetruck minetruck

2020/5/23

#
ok just one thing (for an other game): when a car crashes into another one, i want that the car doesnt go through the car, i just want it to stop. Thats the code for the car how it moves
final int btnNONE = 0, btnLEFT = 1, btnRIGHT= 3;
    public void act() {
        MouseInfo mi = Greenfoot.getMouseInfo();  
        if (Greenfoot.mousePressed(this)){
            int button = mi.getButton();
            if(button == btnLEFT){
                setLocation(getX(), getY()-85);
            }
            if(button == btnRIGHT){
                setLocation(getX(), getY()+85);
            }
        }
}
The problem when i am using isTouching the car does not know from which side it rammed the car, so i need a method for ramming cars at the front AND from behind.
if(isTouching(car2.class)){
            setLocation(getX(), getY()+85);
        }
This wont work because its only working when i ram someone at the front of my car. i hope you understand what i mean xD thanks for your help
danpost danpost

2020/5/23

#
Show the "isTouching" part within your code. Need to see where and how you try to use it. (showing entire class if you can)
minetruck minetruck

2020/5/23

#
public class _car3 extends Actor
{
    final int btnNONE = 0, btnLEFT = 1, btnRIGHT= 3;
    public void act() {
        MouseInfo mi = Greenfoot.getMouseInfo();  
        if (Greenfoot.mousePressed(this)){
            int button = mi.getButton();
            if(button == btnLEFT){
                setLocation(getX(), getY()-85);
            }
            if(button == btnRIGHT){
                setLocation(getX(), getY()+85);
            }
        }

        if(isTouching(car2.class)){
            setLocation(getX(), getY()+85);
        }
        if(isTouching(wall2.class)){
            setLocation(getX(), getY()-85);
        }    
        if(isTouching(wall.class)){
            setLocation(getX(), getY()+85);   
        }
        if(isTouching(_car.class)){
            setLocation(getX(), getY()+85);   
        }
        if(isTouching(car.class)){
            setLocation(getX(), getY()+85);   
        }
    }
    

}
danpost danpost

2020/5/23

#
You can simply reverse any movement that causes a collision if you check for them after EACH move. The following will demonstrate, but my following post will do one better, so here we go:
public void act() {
    MouseInfo mi = Greenfoot.getMouseInfo();
    if (Greenfoot.mousePressed(this))
    {
        int button = mi.getButton();
        if (button == btnLEFT) {
            setLocation(getX(), getY()-85);
            if (isTouching(car.class) || isTouching(wall.class) || isTouching(wall2.class) || isTouching(_car.class) || isTouching(car2.class)) {
                setLocation(getX(), getY()+85);
            }
        }
        if (button == btnRIGHT) {
            setLocation(getX(), getY()+85);
            if (isTouching(car.class) || isTouching(wall.class) || isTouching(wall2.class) || isTouching(_car.class) || isTouching(car2.class)) {
                setLocation(getX(), getY()-85);
            }
        }
    }
}
If you had all you car classes extending a Cars class and all your wall classes extending a Walls class, then that would allow for the simpler:
if (isTouching(Cars.class) || isTouching(Walls.class)) {
danpost danpost

2020/5/23

#
For one better, determine the direction of movement before any moves:
public void act() {
    if (Greenfoot.mousePressed(this)) {
        int button = Greenfoot.getMouseInfo().getButton();
        int dy = 0;
        if (button == btnLEFT) dy--;
        if (button == btnRIGHT) dy++;
        if (dy != 0) {
            setLocation(getX(), getY()+85*dy);
            if (isTouching(Cars.class) || isTouching(Walls.class)) {
                setLocation(getX(), getY()-85*dy);
            }
        }
    }
}
minetruck minetruck

2020/5/23

#
Thank you very much! It worked!
minetruck minetruck

2020/5/28

#
Hi got another problem:
java.lang.ClassCastException: class lvl1 cannot be cast to class winscreen (lvl1 and winscreen are in unnamed module of loader java.net.URLClassLoader @a6a02cf)
	at car3.act(car3.java:33)
here is the code:
public class car3 extends Actor
{
    final int btnNONE = 0, btnLEFT = 1, btnRIGHT= 3;
    public void act() {
        MouseInfo mi = Greenfoot.getMouseInfo();  
        if (Greenfoot.mousePressed(this)){
            int button = mi.getButton();
            if(button == btnLEFT){
                setLocation(getX()+85, getY());
                if(isTouching(_car.class) || isTouching(_car3.class) || isTouching(car.class) || isTouching(car2.class) || isTouching(wall3.class) || isTouching(wall4.class) || isTouching(car3.class)) {
                    setLocation(getX()-85, getY());   
                }
                else{
                    ((level)(getWorld())).moves();   
                    ((winscreen)(getWorld())).moves2(); //here is the problem
                }
            }
            if(button == btnRIGHT){
                setLocation(getX()-85, getY());
                if(isTouching(_car.class) || isTouching(_car3.class) || isTouching(car.class) || isTouching(car2.class) || isTouching(wall3.class) || isTouching(wall4.class) || isTouching(car3.class)) {
                    setLocation(getX()+85, getY());   
                }
                else{
                    ((level)(getWorld())).moves();   
                    ((winscreen)(getWorld())).moves2(); //here is the problem
                }
            }
        }
    } 
}
method:
public void moves2(){
        counter2.add(1);   
    }
and the other method:
public void moves(){
        counter1.add(1);
    }
thanks
danpost danpost

2020/5/28

#
You will need to determine which type world the car is in before trying to cast it as a specific type:
if (getWorld() instanceof level) ((level)getWorld()).moves();
(with a similar line for winscreen world)
minetruck minetruck

2020/5/28

#
sry if i am annoying you its a school project and i need a good grade. i have two counters. one is in the game and shows how many moves youve done, the other one appears on the winscreen and shows the total moves of the level. Now, i want to transfer the score from one counter to the other one but it doesnt work. ive done that what you said:
public void act() {
        MouseInfo mi = Greenfoot.getMouseInfo();  
        if (Greenfoot.mousePressed(this)){
            int button = mi.getButton();
            if(button == btnLEFT){
                setLocation(getX()+85, getY());
                if(isTouching(_car.class) || isTouching(_car3.class) || isTouching(car.class) || isTouching(car3.class) || isTouching(wall3.class) || isTouching(wall4.class)) {
                    setLocation(getX()-85, getY());   
                }
                else{
                    if (getWorld() instanceof winscreen)
                    ((winscreen)getWorld()).moves2();
                    if (getWorld() instanceof level)
                    ((level)getWorld()).moves();
                }     

            }
            if(button == btnRIGHT){
                setLocation(getX()-85, getY());
                if(isTouching(_car.class) || isTouching(_car3.class) || isTouching(car.class) || isTouching(car3.class) || isTouching(wall3.class) || isTouching(wall4.class)) {
                    setLocation(getX()+85, getY());   
                }
                else{
                    if (getWorld() instanceof winscreen)
                    ((winscreen)getWorld()).moves2();
                    if (getWorld() instanceof level)
                    ((level)getWorld()).moves();
                }
            }
        }

        
    }
AND i changed the moves2 method in winscreen:
public void moves2(){
        //counter2.add(1);
        counter2.setValue(level.counter1.getValue());
    }
Method of the exit where the car has to leave and the win screen appears:
if(isTouching(car2.class)){ 
            Greenfoot.setWorld(new win1());
        }
would be nice if you can help me with this, and i am sry if i am annoying you xD
minetruck minetruck

2020/5/28

#
win1 is a subworld of winscreen
danpost danpost

2020/5/28

#
I am finding it difficult trying to figure out exactly what you are trying to do. Are you putting cars in your win screen worlds?
minetruck minetruck

2020/5/29

#
no. You have to move the other cars, so you can move yours to the exit. The moves are counted by a counter. when your car isTouching the exit it should change the world to win1. In win1 is just a text like " well done! you solved the puzzle in x moves!" Now i want a secound counter in win1 world which shows the moves i ve done in the lvl. So its: "well done! you solved the puzzle in (secound counter who has the value of the counter in the game) moves!" hope u understand what i mean
danpost danpost

4 days ago

#
Okay. You have a misconception. You apparently believe that when a setWorld statement is executed, that the active world is instantly changed. This is not the case. The newly set world does not become active until after the current act step is completed for this actor. That being said, you should remove lines 11 thru 13 and lines 24 thru 26. Apparently, you can use just call moves2 from your win1 world.
There are more replies on the next page.
1
2