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

2014/10/20

How do i get an image to point the way it is moving?

JamesHughes JamesHughes

2014/10/20

#
Hi, i have an image that moves left and right when the a and d buttons are pressed. How do i get the picture to point the direction it is going? P.s im new at greenfoot so explain it simply! Thanks :)
Super_Hippo Super_Hippo

2014/10/20

#
What do you mean with 'point to the direction it is going'? And do you have an actor moving or just an image on the background?
danpost danpost

2014/10/20

#
Save the two images (for left and right facing) in fields in the class of the actor. Set the image of the actor to one of the images in the constructor of the class. When a button is found pressed and the image is wrong, fix it. Notice there are two checks that will be required for each direction (and image change).
Alwin_Gerrits Alwin_Gerrits

2014/10/21

#
You can just use a lastDir variable. Make 2 statements. One will be if(Greenfoot.isKeyDown("right");; and the other is the same but then for left. Use the lastDir as in last direction. So:
if (Greenfoot.isKeyDown("right"))
{
 if (lastDir=="left")
 {getImage().mirrorHorizontally();} //turns around the player if you're trying to go 
 //right after going left
            
 lastDir="right";
            
 //your moving code here
}
Obviously you can allso change the image to a mirrored one. You'll just have to put it instead of the getImage line. Anyway, this is what i came up with, but you have to make it twice, once for the right and once for the left. Allos don't put in 'this' anywhere. That will just result in errors. Also seen you mentioned you're new to Greenfoot (which might indicate you're new to programming to). Make sure you declare the lastDir variable as a global variable with a starting value of 'right'. In other words, put it right behind the first { in your code (so really allmost at the top of the entire code!). Declare it as: String lastdir = "right"; This is ofcourse assuming your image starts of facing the right. Anyway, declaring it as a global variable which is what you're doing by placing it behind the first bracket will make sure you don't get any problems while using it later.
Super_Hippo Super_Hippo

2014/10/21

#
I think this should work, too. (faceRight as a boolean which is true when the image is facing right)
if (Greenfoot.isKeyDown("right")) && !faceRight || Greenfoot.isKeyDown("left")) && faceRight)
{
    getImage().mirrorHorizontally();
    faceRight = !faceRight;
}
Alwin_Gerrits Alwin_Gerrits

2014/10/21

#
It will probably work indeed. Though I think to many arguments in one if-statement will make the code a lot harder to read. But indeed if you're looking for a short version that combines both going to the right and going to the left in one piece of code then there's no doubt Hippo's code is probably the shortest version that still works for what you want James.
Super_Hippo Super_Hippo

2014/10/21

#
You can start a new line after every '||' to make it easier to read, but that should still fit in one line. :)
Alwin_Gerrits Alwin_Gerrits

2014/10/21

#
Uuuh i just noticed this, but you can't place your moving code in this if-statement seen it only reacts when facing one side and going to the other. So using my code you don't need another if-statement to put your moving-code in. Actually you would need 2 more if-statements, one for moving right and one for moving left... guess you could argue my option is more complete then. :)
Super_Hippo Super_Hippo

2014/10/21

#
Well, the main problem about my code would be, that it would always switch if you press both keys. ;)
danpost danpost

2014/10/21

#
I would use a more straight-forward method by first saving the two images in fields:
private GreenfootImage imageR, imageL;
Save the images in the fields from in the constructor of the class:
imageR = getImage();
imageL = new GreenfootImage(imageR);
imageL.mirrorHorizontally();
Now, for moving and setting the appropriate image, I would use (either in the 'act' method or within a method the 'act' method calls):
// get key input for direction
int dir = 0;
if (Greenfoot.isKeyDown("left")) dir--;
if (Greenfoot.isKeyDown("right")) dir++;
// use direction for moving and facing if moving at all
if (dir != 0)
{
    // moving
    setLocation(getX()+dir*speed, getY());
    // facing
    if (dir < 0 && getImage() == imageR) setImage(imageL);
    if (dir > 0 && getImage() == imageL) setImage(imageR);
}
The 'speed' field can be hard-coded to a specific value or removed (if the speed is one); or, you can declare the field and give it a value at the top of your class.
Alwin_Gerrits Alwin_Gerrits

2014/10/21

#
Obviously you can allso change the image to a mirrored one. You'll just have to put it instead of the getImage line.
We thought the same dan :). I just didn't write it down. But that put aside, I see how your code is better :).
You need to login to post a reply.