I've got a platformer game with 2 main characters. One has the usual parkour movement, while the other is a little tweaked to resemble the flappy bird one. It works perfectly in all cases but one: moving platforms in opposite direction. By that, I mean that, for the flappy bird movement, when flying in the side of one such platform, which is going the opposite way, the platform somehow gets inside of the character, resulting in a crash from the whole compiler. This would be a more graphic example:
flying towards the side of the platform moving the opposite way
character ----------------------------------------->crash<---------------------------------platform
When the platform is going the same way as the character, it behaves the right way. The same story could be said about ascending platforms as well. When the character is standing still on it, there's no problem at all, but when moving, he is instantly teleported to the side, resulting in his fall. How would you recommend to modify the surface checks in order to solve these issues? Here's the movement of the first character(the basic one):
And this is the movement of the second character:
I thought of adding an additional object checker in a certain radius, but it would be much better if I could somehow modify the first one.
public void act()
{
orizontala();
verticala();
}
public void orizontala()
{
int worldWidth = getWorld().getWidth();
int myWidth = getImage().getWidth();
int dx=0;
if (Greenfoot.isKeyDown("d"))
{
dx++;
animationCounter++;
mersAnm();
check = 1;
}
if (Greenfoot.isKeyDown("a"))
{
dx--;
animationCounter++;
mersInvAnm();
check = 2;
}
if(dx==0) {idleAnm();return;}
setLocation(getX()+dx*speedX, getY());
if(getX()<myWidth/2)setLocation(myWidth/2,getY());
if(getX()>worldWidth-myWidth/2)setLocation(worldWidth-myWidth/2, getY());
while(isTouching(Surface.class))setLocation(getX()-dx,getY());
}
public void verticala()
{
int worldHeight=getWorld().getHeight();
int myHeight=getImage().getHeight();
boolean onGround = false;
speedY+=gravitatie;
setLocation(getX(), getY()+speedY);
if(getY() > worldHeight-myHeight/2)
{
setLocation(getX(), worldHeight-myHeight/2);
speedY=0;
onGround = true;
}
int dy = (int) Math.signum(speedY);
while(isTouching(Surface.class))
{
setLocation(getX(), getY()-dy);
if(dy>0)onGround = true;
speedY=0;
}
if(onGround&&Greenfoot.isKeyDown("space"))speedY=-hop;
}public void act()
{
orizontala();
verticala();
}
public void orizontala()
{
int worldWidth = getWorld().getWidth();
int myWidth = getImage().getWidth();
int pltWidth = 60;
int dx=0;
String key = Greenfoot.getKey();
if(Greenfoot.isKeyDown("d") && ((level)getWorld()).c>0)
{
dx++;
fataAnm();
check=1;
if(!Greenfoot.isKeyDown("a"))
{
counter++;
temp();
}
}
if(Greenfoot.isKeyDown("a") && ((level)getWorld()).c>0)
{
dx--;
spateAnm();
check=2;
if(!Greenfoot.isKeyDown("d"))
{
counter++;
temp();
}
}
setLocation(getX()+dx*speedX, getY());
if(getX()<myWidth/2)setLocation(myWidth/2,getY());
if(getX()>worldWidth-myWidth/2)setLocation(worldWidth-myWidth/2, getY());
while(isTouching(Surface.class))setLocation(getX()-dx,getY());
}
public void temp()
{
if(counter%5==0)
((level)getWorld()).c--;
}
public void verticala()
{
int worldHeight=getWorld().getHeight();
int myHeight=getImage().getHeight();
boolean onGround = false;
speedY+=gravitatie;
setLocation(getX(), getY()+speedY);
if(getY() > worldHeight-myHeight/2)
{
setLocation(getX(), worldHeight-myHeight/2);
speedY=0;
onGround = true;
}
int dy = (int) Math.signum(speedY);
while(isTouching(Surface.class))
{
setLocation(getX(), getY()-dy);
if(dy>0)onGround = true;
speedY=0;
}
if(Greenfoot.isKeyDown("space") && ((level)getWorld()).c>0)
{
speedY=-hop;
if(check == 1)fataAnm();
else spateAnm();
counter++;
temp();
}
}

