Im trying to mirror my character from right to left whenever they face left…
- User
Im trying to mirror my character from right to left whenever they face left (facingdirection ==2 ) but the flipping with a matrix is flipping them around the edge which means they dont stay in place when turning from right->left and vise versa. knowing that the following implementation works:
import processing.sound.*; import processing.data.JSONObject; import processing.data.JSONArray; import java.util.Map; import java.util.LinkedHashMap;
SoundFile stepSound;
PImage[] idleFrames, jumpFrames, deadFrames, attackFrames, runFrames; PImage[] idleLeftFrames, jumpLeftFrames, attackLeftFrames, runLeftFrames;
PImage[] bg;
float characterWorldX = 300; float characterY = 210; float characterX = 300;
float[] ldepth = {8,4,1}; float worldX = 0;
float gravity = 0.6; float jumpVelocity = -12; float yVelocity = 0;
float xVelocity = 0; float maxSpeed = 5; float acceleration = 0.5; float deceleration = 0.8;
enum AnimState { IDLE, JUMP, DEAD, ATTACK, RUN } enum Direction { RIGHT, LEFT } AnimState currentState = AnimState.IDLE; Direction currentDirection = Direction.RIGHT;
HashMap<String, PImage[]> animations = new HashMap<>(); int frameIndex = 0; float frameTimer = 0; float frameDuration = 1.0 / 13; // Approx. 13 FPS for animations
Background background; boolean isRunning = false; boolean isJumping = false; boolean isAttacking = false; boolean isFalling = false;
boolean movingLeft = false; boolean movingRight = false;
Camera camera;
void setup() { size(600, 360); frameRate(60); bg = new PImage[3];
//reverse loaded order for layering/parallax to not cover bg[2] = loadImage("background/bg_layer_1.png"); bg[1] = loadImage("background/bg_layer_2.png"); bg[0] = loadImage("background/bg_layer_3.png"); background = new Background(bg, ldepth); loadAnimation("character_anims_combined"); //in root folder camera = new Camera(width, height); stepSound = new SoundFile(this, "sound/step.wav");}
void draw() { background.draw(camera); handleMovement(); updateState();
String animKey = currentState.name() + "_RIGHT"; PImage[] frames = animations.get(animKey); if (frames != null && frames.length > 0) { if (currentDirection == Direction.LEFT) { // pushMatrix(); no need for push/pop translate(characterX+160, 0); scale(-1,1); translate(-characterX,0); image(frames[frameIndex], characterX, characterY); // popMatrix(); } else image(frames[frameIndex], characterX, characterY); frameTimer += 1.0 / frameRate; if (frameTimer >= frameDuration) { frameTimer = 0; frameIndex = (frameIndex + 1) % frames.length; if (frameIndex == 0 && currentState == AnimState.ATTACK) { isAttacking = false; } } } - Assistant