Hi,
I have some problems with block intersection, I'm trying to make a game where the player is moving between blocks who are all at the same distance. When the player moves down or up between the blocks it works fine but as soon as the player moves right or left it stucks in the blocks and can't move anymore.
I think my problem comes from the height of the image which is bigger than a block. the fact is I want that the foot of the image can't touch any block bottom but the top should be something like the middle of the image so that the player goes right even when part of the top of the image hits the block.
I uploaded the scenario, maybe it will be easier to understand my problem.
My code looks like this for now. Any help on what to do?
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends SuperActor { /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ GreenfootImage[] moveDown = new GreenfootImage[5]; GreenfootImage[] moveRight = new GreenfootImage[5]; GreenfootImage[] moveUp = new GreenfootImage[5]; GreenfootImage[] moveLeft = new GreenfootImage[5]; final int CONSTANT = 6; int imgFrame = 0; public boolean movingRight = false; public boolean movingDown = false; public boolean movingUp = false; public boolean movingLeft = false; public boolean objectIntersection = false; public double speed = 2.9; public Player() { for (int j=0; j< moveDown.length; j++) moveDown[j]=new GreenfootImage("playerMoveDown0"+(j+1)+".png"); for (int j=0; j< moveRight.length; j++) moveRight[j]=new GreenfootImage("playerMoveRight0"+(j+1)+".png"); for (int j=0; j< moveUp.length; j++) moveUp[j]=new GreenfootImage("playerMoveUp0"+(j+1)+".png"); for (int j=0; j< moveLeft.length; j++) moveLeft[j]=new GreenfootImage("playerMoveLeft0"+(j+1)+".png"); } public void act() { checkKeyPress(); checkBlock(); } public void checkKeyPress() { if (Greenfoot.isKeyDown("down") && !movingRight && !movingUp && !movingLeft && !objectIntersection) { moveDownAnimation(); setExactLocation(getExactX(),getExactY()+speed); movingDown = true; } if (Greenfoot.isKeyDown("right")&& !movingDown && !movingUp && !movingLeft && !objectIntersection) { moveRightAnimation(); setExactLocation(getExactX()+speed,getExactY()); movingRight = true; } if (Greenfoot.isKeyDown("up")&& !movingDown && !movingRight && !movingLeft && !objectIntersection) { moveUpAnimation(); setExactLocation(getExactX(),getExactY()-speed); movingUp = true; } if (Greenfoot.isKeyDown("left")&& !movingDown && !movingRight && !movingUp && !objectIntersection) { moveLeftAnimation(); setExactLocation(getExactX()-speed,getExactY()); movingLeft = true; } if (Greenfoot.isKeyDown("space")) { getWorld().addObject(new Bomb(),getExactX(),getExactY()); getWorld().setPaintOrder(Player.class); } movingRight = false; movingDown = false; movingUp = false; movingLeft = false; } public void checkBlock() { Actor blockBottom =(Block) getOneObjectAtOffset(0,-1,Block.class); Actor blockRight =(Block) getOneObjectAtOffset(8,0,Block.class); Actor blockLeft =(Block) getOneObjectAtOffset(-8,0,Block.class); Actor blockUp =(Block) getOneObjectAtOffset(0,5,Block.class); if (blockBottom != null) { objectIntersection = true; setLocation(getX(),getY()-1); } else if (blockUp != null) { objectIntersection = true; setLocation(getX(),getY()+1); } else if (blockRight != null) { objectIntersection = true; setLocation(getX()-1,getY()); } else if (blockLeft != null) { objectIntersection = true; setLocation(getX()+1,getY()); } else { objectIntersection = false; } } public void moveDownAnimation() { imgFrame++; if (imgFrame == CONSTANT*moveDown.length) imgFrame = 0; if (imgFrame%CONSTANT == 0) setImage(moveDown[imgFrame/CONSTANT]); } public void moveLeftAnimation() { imgFrame++; if (imgFrame == CONSTANT*moveLeft.length) imgFrame = 0; if (imgFrame%CONSTANT == 0) setImage(moveLeft[imgFrame/CONSTANT]); } public void moveUpAnimation() { imgFrame++; if (imgFrame == CONSTANT*moveUp.length) imgFrame = 0; if (imgFrame%CONSTANT == 0) setImage(moveUp[imgFrame/CONSTANT]); } public void moveRightAnimation() { imgFrame++; if (imgFrame == CONSTANT*moveRight.length) imgFrame = 0; if (imgFrame%CONSTANT == 0) setImage(moveRight[imgFrame/CONSTANT]); } }