How to animate character in Game with controls?

Forum for J2ME mobile games related topics including programming doubts, books and other resources for J2ME game development

How to animate character in Game with controls?

Postby xc1 » Sat Oct 13, 2007 2:53 am

Hi,

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();
        }
    }
}
xc1
 
Posts: 1
Joined: Sat Oct 13, 2007 2:42 am

Postby DevelopmentTeam » Mon Oct 15, 2007 3:03 am

You can add the same method by adding another sequence to this sprite. Or if you are using an entirely different image file then you need to keep track of this when you are pressing the key you need to trigger a boolean with which in the draw function you have to paint the new image and not the old one. This way should be simple and you can still keep frame sequence for it as well.
User avatar
DevelopmentTeam
Site Admin
 
Posts: 661
Joined: Tue Aug 15, 2006 8:39 am
Location: India


Return to J2ME Games

Who is online

Users browsing this forum: No registered users and 1 guest