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

2013/2/11

Actor is going through wall on the left side.

Qslick Qslick

2013/2/11

#
Hello, My actor is going through walls when approaching it from the left side but from the right side it works fine. When he comes to the right side he stops, but when he gets to the left side he jumps to the other side of the wall. I think the problem is in "StopByLeftWall" public boolean checkLeftWall() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/2); Actor leftWall = getOneObjectAtOffset(xDistance, 0, Platform.class); if(leftWall == null) { return false; } else { stopByLeftWall(leftWall); return true; } } public void stopByLeftWall(Actor leftWall) { int wallWidth = leftWall.getImage().getWidth(); int newX = leftWall.getX() - (wallWidth - getImage().getWidth())/2; setLocation(newX + 5, getY()); } public boolean checkRightWall() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/2); Actor rightWall = getOneObjectAtOffset(xDistance, 0, Platform.class); if(rightWall == null) { return false; } else { stopByRightWall(rightWall); return true; } } public void stopByRightWall(Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2; setLocation(newX - 5, getY()); }
Qslick Qslick

2013/2/11

#
Here is My Act Method public void act() { moveRight(); moveLeft(); checkFall(); checkKey(); platformAbove(); checkRightWall(); checkLeftWall(); }
danpost danpost

2013/2/11

#
The offset for checking to the left needs to be '-xDistance' (otherwise you are still checking to the right). You also need to add the three terms to get newX in the stopByLeftWall method.
Qslick Qslick

2013/2/12

#
Thanks It WORKS!!!!!
You need to login to post a reply.