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

2015/1/13

Setting Rotation without rotating the object

BrownBoii333 BrownBoii333

2015/1/13

#
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.
danpost danpost

2015/1/13

#
Add add a field to the class to hold to "rotation" or rather the current direction of movement. Then to move you could:
1
2
3
setRotation(/**  field value */);
move(/** speed */);
setRotation(0);
and the actor will always appear upright.
BrownBoii333 BrownBoii333

2015/1/13

#
Thank you very much. hey quick Question you're always helping around on these forums and me and a couple friends think you're the man who made Greenfoot. is this true?
BrownBoii333 BrownBoii333

2015/1/13

#
Also, on a more relevant note it looks great now, thanks but it still gets stuck if it is facing a wall. I think it is because since at teh end i say rotation is 0 i cant get out . maybe?
danpost danpost

2015/1/13

#
BrownBoii333 wrote...
Thank you very much. hey quick Question you're always helping around on these forums and me and a couple friends think you're the man who made Greenfoot. is this true?
Far from it. I was just learning the java programming language about the time I created my user name on the site (the site was up several years before I joined in).
BrownBoii333 wrote...
Also, on a more relevant note it looks great now, thanks but it still gets stuck if it is facing a wall. I think it is because since at teh end i say rotation is 0 i cant get out . maybe?
Set the rotation back to zero before doing the collision detection.
You need to login to post a reply.