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

2018/7/11

Scenario stops after pressing "A".... help

Denni Denni

2018/7/11

#
    public void Bewegung() {
        boolean move_right = true;
        boolean move_left = true;
        if (Greenfoot.isKeyDown("D")){
            if (velocity_x >= 0)
                velocity_x += 0.2f;
            else 
            if (velocity_x < 0)
                velocity_x += 0.5f;

            if (velocity_x+0.3f > velocity_move_MAX)
                velocity_x = velocity_move_MAX;
            else
            if (velocity_x > velocity_move_MAX)
                velocity_x -= 0.4f;
        }
        else 
            move_left = false;

        if (Greenfoot.isKeyDown("A"))
        {   
            if (velocity_x <= 0)
                velocity_x -= 0.2f;
            else
            if (velocity_x > 0)
                velocity_x -= 0.5f;

            if (velocity_x-0.3f < -velocity_move_MAX)
                velocity_x = -velocity_move_MAX;
            else

            if (velocity_x < -velocity_move_MAX)
                velocity_x += 0.4f;

        }       
        else
            move_right = false;


        if (Greenfoot.isKeyDown("space") && !Sprunglock)
        {
            if (Sprunghigh == 0)
                Sprunghigh = getY() - SprungMAX;

            if (getY() <= Sprunghigh)
            {
                Sprunghigh = 0; 
                Sprunglock = true;
            }

            velocity_y -= 0.4f;
            if (velocity_y < -velocity_jump_MAX)
                velocity_y = -velocity_jump_MAX;

        }
        else   
        if (!Greenfoot.isKeyDown("space") && velocity_y < 0 && !Sprunglock)
        {
            Sprunglock = true;
        }

        if (Sprunglock)
        {
            velocity_y += 0.3f;
            if (velocity_y > velocity_fall_MAX)
                velocity_y = velocity_fall_MAX;
        }

        if (!move_left && !move_right)
        {

            if (velocity_x < 0)
                velocity_x += 0.75f;
            else

            if (velocity_x > 0)
                velocity_x -= 0.75f;

            if (velocity_x <= 1.1f && velocity_x >= -1.1f)
                velocity_x = 0;
        }

        if (Greenfoot.isKeyDown("r"))
            Tod();

    }
does anybody know why the scenario stops, as soon as I press A? Cheers
danpost danpost

2018/7/11

#
Denni wrote...
<< Code Omitted >> does anybody know why the scenario stops, as soon as I press A?
There is nothing apparent that would cause the scenario to stop when pressing "A". If you are getting any terminal error output, however, you should copy/paste it here. Also, you have only given the code to the Bewegung method. Other places of interest might be the Tod method and the act method.
Denni Denni

2018/7/11

#
danpost wrote...
Denni wrote...
<< Code Omitted >> does anybody know why the scenario stops, as soon as I press A?
There is nothing apparent that would cause the scenario to stop when pressing "A". If you are getting any terminal error output, however, you should copy/paste it here. Also, you have only given the code to the Bewegung method. Other places of interest might be the Tod method and the act method.
This is what's shown in the terminal: java.lang.NullPointerException at Spieler.Collision(Spieler.java:157) at Spieler.act(Spieler.java:278) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
public void Collision(){
        if (velocity_x > 0){
            Actor Box = getOneObjectAtOffset(10, 0, Box.class);
            if (Box != null){

                Box box = (Box) Box;
                if (!box.SideIsSolid(10, 9, velocity_x) ) {
                    Box.setLocation(Box.getX()+(int) velocity_x, Box.getY());
                }
                else
                {
                    setLocation(Box.getX() - Feldgroesse, getY());
                    box.Setvelocity(velocity_x, 0);
                    velocity_x = 0;
                }

            }
            else
            {
                int X = getX() + 10 + (int)velocity_x;
                int Y = getY() -9;
                int Yende = getY() +9;
                boolean frei =true;
                while (Y <= Yende) {
                    if (m_World.isSolid(X, Y)) {
                        setLocation(X/Feldgroesse * Feldgroesse - 10, getY());
                        frei = false;
                    }

                    Y += 20;
                }

                if (frei)
                    movexy(velocity_x, 0);
                else
                    velocity_x = 0;
            }
        }
        else
        if (velocity_x < 0) {
            Actor Box = getOneObjectAtOffset(-10, 0, Box.class);
            if (Box != null){
                Box box = (Box)Box;
                if(!box.SideIsSolid(-10, 9, velocity_x) ) 
                    Box.setLocation(Box.getX()+(int)velocity_x, Box.getY());
            }
            else
            {
                setLocation(Box.getX() + Feldgroesse, getY());
                velocity_x = 0;
            }
        }
        else
        { 
            int X = getX() - 10 + (int)velocity_x;
            int Y = getY() - 9;
            int Yende = getY() + 9;
            boolean frei = true;
            while (Y >= Yende){
                if (m_World.isSolid(X, Y))
                    setLocation((X/Feldgroesse + 1) * Feldgroesse + 10, getY());
                frei = false;
            }
            Y += 20;

            if (frei) 
                movexy(velocity_x, 0);
            else
                velocity_x = 0;
        }

        //im Fallen 
        if (velocity_y > 0){
            Sprunghigh = 0;

            Actor Box         = getOneObjectAtOffset(9, 10+(int)velocity_y, Box.class);
            Actor Box2        = getOneObjectAtOffset(-9, 10+(int)velocity_y, Box.class);

            if (Box != null) {
                velocity_y = 0;
                Sprunglock = false;
                setLocation(getX(), Box.getY() - Feldgroesse);
            }
            else
            if (Box2 != null){

                velocity_y = 0;
                Sprunglock = false;
                setLocation(getX(), Box2.getY() - Feldgroesse);
            }
            else
            {
                int Y = getY() + 10 + (int) velocity_y;
                int X =  getX() -9;
                int Xende = getX() +9;
                boolean frei = true;
                if (m_World.onSpike(X, Y)){
                    Tod();
                    return;
                }
                while (X <= Xende) {
                    if (m_World.isSolid(X, Y)){
                        setLocation(getX(), (Y/Feldgroesse) * Feldgroesse - 10);
                        frei = false;
                        Sprunglock = false;
                    }
                    X += 18;
                }
                if (frei) 
                    movexy(0, velocity_y);
                else 
                    velocity_y = 0;
            }
        }
        else 
        if (velocity_y < 0) {
            int Y= getY() - 10 + (int) velocity_y;
            int X = getX() -9;
            int Xende = getX() + 9;
            boolean frei = true;
            while (X <= Xende) {
                if (m_World.isSolid(X, Y)){
                    setLocation(getX(),(Y/Feldgroesse + 1) * Feldgroesse + 10);
                    frei = false;
                    Sprunglock = true;
                }
                X+= 18;
            }
            if (frei)
                movexy(0, velocity_y);
            else 
                velocity_y = 0.1;
        }
        else
        if (velocity_y == 0){
            boolean links = m_World.isSolid(getX() -9, getY() +11);
            boolean rechts = m_World.isSolid(getX() +9, getY() +11);
            if(!links && !rechts){
                velocity_y = 0.3f;
                Sprunglock = true;
            }
        }
    }

    private void KolMitGegner() {
        Actor Gegner = getOneObjectAtOffset(10, 0, Enemy.class);
        Actor Gegner2 = getOneObjectAtOffset(-10,0 , Enemy.class);
        Actor Gegner3= getOneObjectAtOffset(10, -9, Enemy.class);
        Actor Gegner4= getOneObjectAtOffset(-10, -9, Enemy.class);

        if (Gegner != null || Gegner2 != null || Gegner3 != null || Gegner4 != null)
        {
            Freeze(true);
            Tod();
        }
    }

    public void Freeze(boolean option)
    {
        //freezed den Spieler wenn ein Level beendet wird
        freeze = option;
    }
public void act(){
        if (!freeze){
            if (canSee(Muenze.class)){
                eat(Muenze.class);
            }
            Bewegung();
            Collision();
            KolMitGegner();
        }
danpost danpost

2018/7/11

#
Denni wrote...
This is what's shown in the terminal: << Error Output Omitted >> << Codes Omitted >>
Which is line 157 in your Spieler class?
Denni Denni

2018/7/11

#
danpost wrote...
Denni wrote...
This is what's shown in the terminal: << Error Output Omitted >> << Codes Omitted >>
Which is line 157 in your Spieler class?
 setLocation(Box.getX() + Feldgroesse, getY());
danpost danpost

2018/7/11

#
Denni wrote...
danpost wrote...
Denni wrote...
This is what's shown in the terminal: << Error Output Omitted >> << Codes Omitted >>
Which is line 157 in your Spieler class?
 setLocation(Box.getX() + Feldgroesse, getY());
That line is in the else part of the if structure, where Box is null. Cannot getX on null.
Denni Denni

2018/7/11

#
danpost wrote...
Denni wrote...
danpost wrote...
Denni wrote...
This is what's shown in the terminal: << Error Output Omitted >> << Codes Omitted >>
Which is line 157 in your Spieler class?
 setLocation(Box.getX() + Feldgroesse, getY());
That line is in the else part of the if structure, where Box is null. Cannot getX on null.
so... what should I do, I'm pretty frustrated and don't want to do anything wrong
danpost danpost

2018/7/11

#
Denni wrote...
so... what should I do, I'm pretty frustrated and don't want to do anything wrong
For now, you can just remove lines 47 through 51 from the Collision method as shown above. You can then ask yourself what behavior you want the Spieler object to exhibit when velocity_x is negative and no Box object is encountered (maybe nothing).
You need to login to post a reply.