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

2013/3/12

What coding is required to stop the player entering through the walls?

Blakehammond Blakehammond

2013/3/12

#
I have tried to do it before but the character still somehow went through.
GreenGoo GreenGoo

2013/3/12

#
Can you post the coding of your character and walls?
Blakehammond Blakehammond

2013/3/12

#
Character import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Dom here. * * @author (your name) * @version (a version number or a date) */ public class Dom extends Mover { private int speed=4; public Dom() { } public void act() { checkKeys(); Actor actor = getOneIntersectingObject(Dom.class); if(actor != null) { getWorld().removeObject(this); } } private void checkKeys(){ if(Greenfoot.isKeyDown("left")){ moveLeft(); } if(Greenfoot.isKeyDown("right")){ moveRight(); } if(Greenfoot.isKeyDown("up")){ moveUp(); } if(Greenfoot.isKeyDown("down")){ moveDown(); } } public void moveRight(){ setLocation(getX()+speed,getY()); } public void moveLeft(){ setLocation(getX()-speed,getY()); } public void moveUp(){ setLocation(getX(),getY() -speed); } public void moveDown(){ setLocation(getX(),getY() +speed); } }
Blakehammond Blakehammond

2013/3/12

#
Cave_rotated import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Cave_rotated here. * * @author (your name) * @version (a version number or a date) */ public class Cave_rotated extends Actor { /** * Act - do whatever the Cave_rotated wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
Blakehammond Blakehammond

2013/3/12

#
Cave import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Cave here. * * @author (your name) * @version (a version number or a date) */ public class Cave extends Actor { /** * Act - do whatever the Cave wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } }
Blakehammond Blakehammond

2013/3/12

#
If you are confused I have two types of wall. Which I have posted.
danpost danpost

2013/3/13

#
If your walls are at least 4 pixels wide then you should be able to detect when you came upon one. After the 'checkKeys' call in the act method, add a 'checkWalls' method. Create the new 'checkWalls' method checking for intersecting walls (one check for each type of wall). If either wall is present, move the character back to the location it started at. You will have to add fields to hold the initial location and set them at the beginning of the act method.
You need to login to post a reply.