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

2020/9/11

Acceleration, Collision and and Walljumps?

Pumk!n Pumk!n

2020/9/11

#
I need my character to acceletare before he reaches maximum speed, also I need him to stop at a wall but I still need a variable that i can call which tells me if he is touching the wall
public class Player extends Actor
{
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if(Greenfoot.isKeyDown("a"))
        {
            
            setLocation(getX()-5, getY());
        }
        if(Greenfoot.isKeyDown("d"))
        {
            
            setLocation(getX()+5, getY());
        }
    }    
}
danpost danpost

2020/9/11

#
Pumk!n wrote...
I need my character to acceletare before he reaches maximum speed,
If maximum speed is 5, then it would probably be best to use a smooth moving system; otherwise, the change from one speed to the next would be an abruptly noticeable jump in speed (not smooth).
I need him to stop at a wall but I still need a variable that i can call which tells me if he is touching the wall
You do not "call" a variable. You need to call a method the returns the value of a variable which tells you if he is touching a wall. So, add the required variable:
private boolean touchingWall;
then, add the method:
public boolean isTouchingWall()
{
    return touchhingWall;
}
Now, add to act method code that checks for wall and set the variable as per results. When wall is touched, zero speed and move actor back off of wall.
RcCookie RcCookie

2020/9/14

#
To actually implement the moving system, you need to save your speed from frame to frame:
//globally
double MAX_SPEED = 5;
double ACCELERATION = 0.1;
double speedX = 0;


public void act(){
    //first, gather the input
    if(Greenfoot.isKeyDown(„a“)) speed -= ACCELERATION;
    if(Greenfoot.isKeyDown(„d“)) speed += ACCELERATION;

    //Check collisions, refers to Danpost‘s code

    //Updates the collision state
    touching = getOneIntersectingObject(Wall.class) != null;

    //Updates Speed
    if(touchingWall) speed = 0;


    // Moves the actor with the current speed
    move(speed);
}
This does use SmoothMover.
You need to login to post a reply.