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

2019/6/17

Class.Car should bounce off the Class.Wall but doesnt

Wellvyre Wellvyre

2019/6/17

#
I need to program for the school a mudball car fighting game and im stuck at the point where the car needs to turn in the opposite direction where he hits the wall and move like 5 foward. I´ve tried once to code it but using: public void hitWall(){ if(isTouching(Wall.class)) turn(180); } didnt work as planned. And if needed here is my car code :
public class Car extends Actor
{
    private int ShotTimer = 0; 
    private int health = 100;
    public void act()
    {
        if (ShotTimer > 0) {
            ShotTimer = ShotTimer - 1;
        }
        else if (Greenfoot.isKeyDown("space")) {
            getWorld().addObject(new Shot(this), getX(), getY());
            ShotTimer = 200; // delay next shot
        }

        MouseInfo mi = Greenfoot.getMouseInfo(); 
        if(mi!=null)
            turnTowards(mi);
        if (Greenfoot.isKeyDown("w"))
            move(2);
        if (Greenfoot.isKeyDown("s"))
            move(-2);
            
        
    }

    public void turnTowards (int x, int y)
    {
        double dx = x-getX();
        double dy = y-getY();
        double angle = Math.atan2(dy,dx)*180.0/Math.PI;
        setRotation ( (int)angle );
    }
    public void turnTowards (MouseInfo mi)
    {
        turnTowards(mi.getX(), mi.getY());
    }
    
    public void loseHealth(int amount) 
    {
       health -= amount;
       if (health < 1)
       {
           getWorld().removeObject (this);
        }
    }
}
danpost danpost

2019/6/17

#
Wellvyre wrote...
I need to program for the school a mudball car fighting game and im stuck at the point where the car needs to turn in the opposite direction where he hits the wall and move like 5 foward. I´ve tried once to code it but using: << Code Omitted >> didnt work as planned. And if needed here is my car code : << Code Omitted >>
You need to undo any action that causes a collision with a wall. That is, check for a wall after each action (moving or turning) and undo that action if found touching. That way the actor is never touching at the end of an act step.
Wellvyre Wellvyre

2019/6/17

#
i think i understand what i need to remove and change And another thing: Do you know how to make the actor change the direction he is looking at by 180 while moving like 5foward when touching the color green ? Or even better before he touch it but i thing thats to much and wouldnt realy make sense in my game
danpost danpost

2019/6/17

#
Wellvyre wrote...
Do you know how to make the actor change the direction he is looking at by 180 while moving like 5foward when touching the color green ? Or even better before he touch it but i thing thats to much and wouldnt realy make sense in my game
Is the green part of the background image or the image of some other actor(s)? And, please elaborate -- your actor is to look away from the direction it is moving when touching green?
Wellvyre Wellvyre

2019/6/17

#
the green color is part of the worlds background, The world has a circle like field that is blue ( the blue field is part of the background ) and the green part is around the blue part and it is also a part of the My world - World background I dont know if i wrote it right im from germany and my english writing is pretty terrible...should i just like send my whole game or is it understandable
danpost danpost

2019/6/17

#
Wellvyre wrote...
I dont know if i wrote it right im from germany and my english writing is pretty terrible...should i just like send my whole game or is it understandable
I don't think sending the whole game is necessary.
the green color is part of the worlds background, The world has a circle like field that is blue ( the blue field is part of the background ) and the green part is around the blue part and it is also a part of the My world - World background
Okay -- so your actor is to be restricted to a certain range from the center of the screen and you want your actor to turn around when the perimeter is reached. Thinking of it this way should give you an idea of how to control the actor (without any color checking).
You need to login to post a reply.