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

2019/6/22

Errors with playercollide-code after gameover-screen.

MisterUnknown MisterUnknown

2019/6/22

#
After one player in my two-player-game has been defeated, the gameover-screen appears, but also the console opens and tells me that there must be an issue with my collider-method. Here it is:
public void playerCollider()
    {
        if(isTouching(Player.class)) {
            playerContact = true;
            Actor oPlayer = (Actor) getOneIntersectingObject(Player.class);
            int oPlayerX = oPlayer.getX();
            int oPlayerY = oPlayer.getY();
            double k = 1.5 * currentForce;
            int angle = getRotation();
            if (oPlayerX > getX()) {
                 if(oPlayerY < getY()) {
                    
                    xVel = (-1) * (k) * Math.cos(Math.toRadians(360-angle));
                    yVel = (1) * (k) * Math.sin(Math.toRadians(360-angle));
                }
                else {
                    
                    xVel = (1) * (k) * Math.cos(Math.toRadians(angle-180));
                    yVel = (1) * (k) * Math.sin(Math.toRadians(angle-180));
                }
            }
            else {
                if(oPlayerY < getY()) {
                    
                    xVel = (1) * (k) * Math.cos(Math.toRadians(180-angle));
                    yVel = (-1) * (k) * Math.sin(Math.toRadians(180-angle));
                }
                else {
                    
                    xVel = (-1) * (k) * Math.cos(Math.toRadians(angle));
                    yVel = (-1) * (k) * Math.sin(Math.toRadians(angle));
                }
            }
            
        }
        else {
            playerContact = false;
        }
        
}
Thanks in advance
Super_Hippo Super_Hippo

2019/6/22

#
What exactly is it telling you? I guess (but I wouldn't have to guess if you copied the error message indicating in which line the error happened) that you remove the object from the world and then executing this method. Which means that the method itself is fine. Make sure that when you remove the object from the world (one player was defeated), this method is not executed afterwards. So you should either remove the object at the end of the act method or if this isn't possible (because there is more than one way that it is removed in the act method, make sure nothing is executed which needs the object in the world. A not so elegant fix on the other hand is to exit the method if it isn't in a world:
public void playerCollider()
{
    if (getWorld() == null) return;
    
    //rest of method
}
MisterUnknown MisterUnknown

2019/6/22

#
The consol gives me errors in the mutualmovement class, where Player intersects with the pipe-class.
You need to login to post a reply.