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

2020/2/28

Making an actor face right or left

nextstone nextstone

2020/2/28

#
Hello, So I am trying to create the beginnings of a 2D platforming game. I have images for my Stickman character set and I have even gotten a working idle animation and movement acceleration. However, now I want to try and make it so that my character faces the direction of movement. I have tried looking this up online, but all of the solutions I have seen deal with games where the movement can happen in all four directions from a top-down view. However, in a platformer game that I'm trying to make, the movement is only left and right with the exception of jumping. This means I would have to use the mirrorHorizontally() function, but I am having trouble making it so that the character stays the direction they're facing when they are moving left and right. Here is my code for flipping the image. My variable isLeft is set to true because I created the default images with my character facing left. If I did not give enough information about my problem I would be glad to help further.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private boolean isLeft = true;
 
if(Greenfoot.isKeyDown("left") == true) /** logic to flip image going left */
        {
            if(isLeft == false)
            {
            this.getImage().mirrorHorizontally();
            }
            isLeft = true;
        }
        if(Greenfoot.isKeyDown("right") == true) /** logic to flip image going right*/
        {
            if(isLeft == true)
            {
            this.getImage().mirrorHorizontally();
            }
            isLeft = false;
        }
danpost danpost

2020/2/28

#
You can download and look at the code of my GQActor Superclass Demo scenario to see how I did something similar there. Nevermind. It used to work properly, but I am having trouble updating it (getting it to work -- not re-uploading on the site). Correction: I figured it out. However, the code you require may be too wrapped up in other stuff that it may not be easy to understand what is happening.
nextstone nextstone

2020/2/28

#
Well, I just figured out a different way of doing it, where instead of using this.getImage i just mirror each of the individual images horizontally. It works and the character animates, but there's probably a more elegant and sensible way to do it. Is this a good method or is there a way that is cleaner? I could maybe see this getting a little out of hand when I have more animation images.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
if(Greenfoot.isKeyDown("left") == true) /** logic to flip image going left */
        {
            if(isLeft == false)
            {
            idle1.mirrorHorizontally();
            idle2.mirrorHorizontally();
            idle3.mirrorHorizontally();
            idle4.mirrorHorizontally();
            }
            isLeft = true;
        }
        if(Greenfoot.isKeyDown("right") == true) /** logic to flip image going right */
        {
            if(isLeft == true)
            {
            idle1.mirrorHorizontally();
            idle2.mirrorHorizontally();
            idle3.mirrorHorizontally();
            idle4.mirrorHorizontally();
            }
            isLeft = false;
        }
danpost danpost

2020/2/28

#
A better way would be to keep all 8 images stored in fields -- preferably a 2D array (2 sets, for left and right, of 4 images: { { ltA, ltB, ltC, ltD }, { rtA, rtB rtC, rtD } } Use a constructor block to fill the array with the images. The is a method in the scenario I linked above that will create a new flipped image. Then use an int field of plus or minus 1 for the facing direction instead of the boolean isLeft. That way, you can use it to access the correct set of the array with (1+facingDir)/2 .Finally, use a timer with modulus the time it takes for one full animation set to complete. If tfo is the amount of Time For One image to show and you have 4 image in a set, then timer%tfo == 0 would give when to change the image and the image to change to would be {(1+facingDir)/2}{timer/tfo} (I used squiggly brackets for square ones). The timer would be incremented with (timer+1)%(4*tfo) .
nextstone nextstone

2020/3/1

#
So are you saying I should have 8 separate images, or is this a way to take my 4 images and store them as 8 and assign them accordingly? Sorry, but I am having a little trouble understanding how to create this. I am currently taking a programming class where we use Greenfoot and we are about to go to another unit, and arrays are not something that we focused on. Is it possible you have code for how to do this or a way that might be at a less advanced level? I also tried looking at the GQActor class as a reference but I think I need more guidance.
danpost danpost

2020/3/2

#
I am saying to store all 8 images. If not using an array, you may might want to use switch blocks to hash out which image to use (out of 4 for each block).
You need to login to post a reply.