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

2019/2/22

Help With - Left Wall / Actor Through Left Wall

CaptainGeorge CaptainGeorge

2019/2/22

#
I have right now a problem with left check... My right check is good.. And no problems But my left have a problem, my actor is going through walls from the left. Right wall check (Works perfectly fine) public boolean checkRightWalls() { 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 -1, getY()); } Here is the left check code.. (Doesn't work) public boolean checkLeftWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int)(spriteWidth/-2); Actor leftWall = getOneObjectAtOffset(-xDistance, -1, 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 +1, getY()); } Please tell me exactly what to change so I can understand because I am still a beginner.
danpost danpost

2019/2/22

#
CaptainGeorge wrote...
I have right now a problem with left check... My right check is good.. And no problems But my left have a problem, my actor is going through walls from the left. << Code Omitted >> Please tell me exactly what to change so I can understand because I am still a beginner.
For left check, you are negating xDistance multiple times. In stopByLeftWall, you still need to add the half-widths together to get the proper x-offset. And why you use different y-offsets in the two getOneObjectAtOffset statements is beyond me.
You need to login to post a reply.