I'm having some trouble trying to create an animated character. This animated character, I'm trying to make it react to key pad punches where it would show different animated picture.
All that my code does is switch to one picture when a button is pressed. I have tried loops, but have had very little success. Can someone show me my mistake or show to me how its done?
Here's my code:
-the sprite
- Code: Select all
public class Characterzs extends Sprite
{
private final int[] LEFTFRAMESEQUENCE = {1,2,3,4,5,6,7,8,9,10};
static final int[] RIGHTFRAMESEQUENCE = {10,9,8,7,6,5,4,3,2,1};
private final int[] STANDDUPFRAMESEQUENCE = {0};
public Characterzs(int imageWidth, int imageHeight) throws IOException
{
super(Image.createImage("/animation!!!_.png"), imageWidth, imageHeight);
defineReferencePixel(imageWidth/2, imageHeight/2);
setRefPixelPosition(imageWidth, imageHeight);
setFrameSequence(RIGHTFRAMESEQUENCE);
}
public void movementLeft()
{
setFrameSequence(LEFTFRAMESEQUENCE);
nextFrame();
}
public void movementRight()
{
setFrameSequence(RIGHTFRAMESEQUENCE);
nextFrame();
}
public void standUp()
{
setFrameSequence(STANDDUPFRAMESEQUENCE);
}
}
-controls
- Code: Select all
public class BasicCanvas extends GameCanvas implements Runnable
{
private LayerManager manager;
private Characterzs character;
private int distance = 1;
private int xAxis_Image;
private int yAxis_Image;
private boolean running = false;
private final int SPEED = 170;
public BasicCanvas()
{
super(true);
try
{
character = createCharacter();
manager = new LayerManager();
manager.append(character);
}
catch(IOException io)
{
io.printStackTrace();
}
}
private Characterzs createCharacter() throws IOException
{
Image image = Image.createImage("/animation!!!_.png");
return new Characterzs(116, 60);
}
public void gatherImageProperties()
{
try
{
createCharacter();
xAxis_Image = getWidth()/2;
yAxis_Image = getHeight()/2;
}
catch(IOException io)
{
io.printStackTrace();
}
}
public void start()
{
running = true;
//gatherImageProperties();
Thread thread = new Thread(this);
thread.start();
}
public void run()
{
Graphics graphics = getGraphics();
while(true)
{
try{
gatherImageProperties();
userInput();
drawDisplay(graphics);
flushGraphics();
Thread.sleep(SPEED);
}catch(InterruptedException ie){
ie.printStackTrace();
}
}
}
public void drawDisplay(Graphics graphics)
{
manager.paint(graphics, 0, 0);
//graphics.drawImage(image, xAxis_Image, yAxis_Image, Graphics.HCENTER | Graphics.VCENTER );
flushGraphics();
}
public void stop()
{
running = false;
}
private void userInput()
{
int keyInput = getKeyStates();
calculatePosition(keyInput);
}
private void calculatePosition(int keyInput)
{
boolean movement = true;
character.standUp();
if((keyInput & LEFT_PRESSED) != 0)
{
xAxis_Image -= distance;
character.movementLeft();
}
if((keyInput & RIGHT_PRESSED) != 0)
{
xAxis_Image += distance;
character.movementRight();
}
}
}
