Hi i'm trying to create a project in which the main actor (fighter) moves around and needs to not be able to move through walls. I added code to see if it is facing a wall that i know works because i've used it in past projects. Here is teh code for my main actor.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Fighter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fighter extends Actor
{
public Fighter()
{
}
public void act()
{
move();
}
private static final int EAST =0;
private static final int SOUTH =90;
private static final int WEST =180;
private static final int NORTH =270;
int t = 0;
int c = 0;
int e= 0;
int a = 0;
int move = 2;
public void move()
{
if(facingWall()){
move=0;
}else if (!facingWall()){
move=2;
}
if(Greenfoot.isKeyDown("w"))
{
upSequence();
setLocation(getX(),getY()-move);
}else
if(Greenfoot.isKeyDown("a"))
{
leftSequence();
setLocation(getX()-move,getY());
}else
if(Greenfoot.isKeyDown("s"))
{
downSequence();
setLocation(getX(),getY()+move);
}else
if(Greenfoot.isKeyDown("d"))
{
rightSequence();
setLocation(getX()+move,getY());
}
}
public boolean facingWall(){
int dx = 0;
int dy = 0;
switch(getRotation())
{
case EAST:dx=1; break;
case WEST: dx= -1; break;
case SOUTH: dy =1; break;
case NORTH: dy = -1; break;
}
return getOneObjectAtOffset(dx,dy,Obstacle.class) !=null;
}
public void upSequence()
{
if(move>=2)
{
t++;
if(t==3){
setImage("NorthMan1.png");
}
if(t==6){
setImage("NorthMan2.png");
}
if(t==9){
setImage("NorthMan3.png");
t = 0;
}
}
}
public void downSequence()
{
if(move>=2)
{
a++;
if(a==3){
setImage("SouthMan1.png");
}
if(a==6){
setImage("SouthMan3.png");
}
if(a==9){
setImage("SouthMan1.png");
a = 0;
}
}
}
public void rightSequence()
{
if(move>=2)
{
c++;
if(c==3){
setImage("EastMan1.png");
}
if(c==6){
setImage("EastMan2.png");
}
if(c==9){
setImage("EastMan3.png");
c = 0;
}
}
}
public void leftSequence()
{
if(move>=2)
{
e++;
if(e==3){
setImage("WestMan1.png");
}
if(e==6){
setImage("WestMan2.png");
}
if(e==9){
setImage("WestMan3.png");
e = 0;
}
}
}
}
now when it moves in any direction it doesn't matter what direction it looks like it is moving it is always facing EAST so when i run into a wall I'm inside it and i can't get out. I tried simply setting its rotation to EAST,W, N, S accordingly but that rotates the image and it looks all weird. Now i tried to rotate the actual image in photoshop and everything wokrs but there is a brief moment when the image is still facing the wrong way when it changes directions. So can have the program reading that (ie the rotation is north when it's moving up) without actually rotating it so that it is facing north.

