Question: I cant figure the logic to my code using java, everytime I use the arrow keys to change the positon of my player in the
I cant figure the logic to my code using java, everytime I use the arrow keys to change the positon of my player in the screen I want to be able to change the image of my player when import it If I press left of right then my player should switch to the running image,it should also use values from the constant class, that way my code can figure out how many frames are on each images. if dont move then my character should go back to the idle image. I have keyboard input class and gamepanel class. Please check my code
package main;
Step : lets import an image
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.ioIOException;
import java.ioInputStream;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import inputs.KeyboardInputs;
import static utilz.Constants.PlayerConstants.;
import static utilz.Constants.Directions.;
public class GamePanel extends JPanel
private float xDelta yDelta ;
private BufferedImage img;
private BufferedImage animations;
private int aniTick, aniIndex, aniSpeed ;
private int playerAction IDLE;
private int playerDir ;
private boolean moving false;
private String actions idle "run";
private int currentAction ;
public GamePanel
setPanelSize;
importImg;
loadAnimations;
public void setPanelSize
Dimension size new Dimension;
setPreferredSizesize;
private void loadAnimations
animations new BufferedImage;
int xpositions ;
forint i ; i animations.length; i
i times the width of the sprite
the stupid x in the sub image, is the start of the x not the width
animationsi img.getSubimagei ;
private void importImg
InputStream is getClassgetResourceAsStreamKnightplayerActionpng;
try
img ImageIO.readis;
catch IOException e
TODO Autogenerated catch block
eprintStackTrace;
public void setDirectionint direction
this.playerDir direction;
moving true;
public void setMovingboolean moving
this.moving moving;
private void setAnimation
if moving
playerAction RUNNING;
else
playerAction IDLE;
private void updatePos
if moving
switch playerDir
case LEFT:
xDelta ;
playerAction RUNNING;
break;
case UP:
yDelta ;
break;
case RIGHT:
xDelta ;
playerAction RUNNING;
break;
case DOWN:
yDelta ;
break;
private void updateAnimationTick
aniTick;
ifaniTick aniSpeed
aniTick ;
aniIndex ;
ifaniIndex animations.length
aniIndex ;
public void paintGraphics pen
super.paintComponentpen;
updateAnimationTick;
updatePos;
pen.drawImageanimationsaniIndex null;
package inputs;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import main.GamePanel;
import static utilz.Constants.Directions.;
public class KeyboardInputs implements KeyListener
private GamePanel gamePanel;
public KeyboardInputsGamePanel gamePanel
this.gamePanel gamePanel;
@Override
public void keyTypedKeyEvent e
TODO Autogenerated method stub
@Override
public void keyReleasedKeyEvent e
switch egetKeyCode
case KeyEvent.VKW:
case KeyEvent.VKA:
case KeyEvent.VKS:
case KeyEvent.VKD:
gamePanel.setMovingfalse;
break;
@Override
public void keyPressedKeyEvent e
switch egetKeyCode
case KeyEvent.VKW:
gamePanel.setDirectionUP;
break;
case KeyEvent.VKA:
gamePanel.setDirectionLEFT;
break;
case KeyEvent.VKS:
gamePanel.setDirectionDOWN;
break;
case KeyEvent.VKD:
gamePanel.setDirectionRIGHT;
break;
package utilz;
public class Constants
public static class Directions
public static final int LEFT ;
public static final int UP ;
public static final int RIGHT ;
public static final int DOWN ;
public static class PlayerConstants
public static final int IDLE ;
public static final int RUNNING ;
public static final int JUMP ;
public static final int HIT ;
public static int GetSpriteAmountint playeraction
switch playeraction
case RUNNING:
return ;
case IDLE:
return ;
case HIT:
r
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
