Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Moving sprite leaves a trail

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Moving sprite leaves a trail

    Hi all, I am new to these forums and to Java programming so bare with me!

    I am trying to create a simple checkers game for a mobile phone using Java. I have currently displayed the checker board and checkers. I want to use an on screen cursor to select the checker piece to move. I have displayed a cursor on the screen and I can move it.

    However, it leaves a trail behind and I am struggling to work out what I need to do in order to fix this (even though I am pretty sure it is a matter of adding one or two lines!) My code is, as follows:

    package CheckersBoard;
     
    /**
     *
     * @author Chris
     */
     
    import javax.microedition.lcdui.*;
    import javax.microedition.lcdui.game.*;
    import java.io.*;
     
     
     
    public class CheckerBoard extends GameCanvas implements Runnable{
        private Display myDisplay;
        private long frameRate;
        private boolean sleeping;
        private Graphics g;
        private Sprite[] blackChecker;
        private Sprite[] whiteChecker;
        private Sprite cursor;
        private final int numberOfCheckers = 12;
     
        int row;                      // Row number, from 0 to 7
        int col;                      // Column number, from 0 to 7
        int x,y;                      // Top-left corner of square
        int z;                        //Used to store value of square dimensions
        int screenWidth = getWidth(); //Stores value of the screen width in px
        int screenHeight = getHeight();
        int curPositionX = getWidth()/2-20;
        int curPositionY = getHeight()/2-60;
     
        public CheckerBoard(Display d){
            super(true);
            myDisplay = d;
            frameRate = 40;
            g = this.getGraphics();
            whiteChecker = new Sprite[numberOfCheckers];
            blackChecker = new Sprite[numberOfCheckers];
        }
        public void start(){
            myDisplay.setCurrent(this);
            sleeping = false;
            if (screenWidth <= screenHeight) //Checks screen width is greater than screen height
                z = screenWidth/8;           //Divides screen width by 8
            else
                z = screenHeight/8;          //Divides screen height by 8
     
            for ( row = 0;  row < 8;  row++ ) {
                 for ( col = 0;  col < 8;  col++) {
                    x = col * z;                    //Set the square width to 1/8 of the screen width
                    y = row * z;                    //Set the square height to 1/8 of the screen height
                    if ( (row % 2) == (col % 2) ){   //Every other square
                       g.setColor(139,0,0);         //Colours the square red
     
                    try{
                        Image black = Image.createImage("/res/blackChecker.png");
                        Image white = Image.createImage("/res/whiteChecker.png");
                        Image hand = Image.createImage("/res/cursor.PNG");
                        cursor = new Sprite(hand);
                        cursor.setPosition(curPositionX, curPositionY);
     
                        for (int i=0; i<numberOfCheckers; i++){
                            blackChecker[0] = new Sprite(black);
                            blackChecker[0].setPosition(30,0);
                            blackChecker[1] = new Sprite(black);
                            blackChecker[1].setPosition(90,0);
                            blackChecker[2] = new Sprite(black);
                            blackChecker[2].setPosition(150,0);
                            blackChecker[3] = new Sprite(black);
                            blackChecker[3].setPosition(210,0);
                            blackChecker[4] = new Sprite(black);
                            blackChecker[4].setPosition(0,30);
                            blackChecker[5] = new Sprite(black);
                            blackChecker[5].setPosition(60,30);
                            blackChecker[6] = new Sprite(black);
                            blackChecker[6].setPosition(120,30);
                            blackChecker[7] = new Sprite(black);
                            blackChecker[7].setPosition(180,30);
                            blackChecker[8] = new Sprite(black);
                            blackChecker[8].setPosition(30,60);
                            blackChecker[9] = new Sprite(black);
                            blackChecker[9].setPosition(90,60);
                            blackChecker[10] = new Sprite(black);
                            blackChecker[10].setPosition(150,60);
                            blackChecker[11] = new Sprite(black);
                            blackChecker[11].setPosition(210,60);
                            whiteChecker[0] = new Sprite(white);
                            whiteChecker[0].setPosition(0,210);
                            whiteChecker[1] = new Sprite(white);
                            whiteChecker[1].setPosition(60,210);
                            whiteChecker[2] = new Sprite(white);
                            whiteChecker[2].setPosition(120,210);
                            whiteChecker[3] = new Sprite(white);
                            whiteChecker[3].setPosition(180,210);
                            whiteChecker[4] = new Sprite(white);
                            whiteChecker[4].setPosition(30,180);
                            whiteChecker[5] = new Sprite(white);
                            whiteChecker[5].setPosition(90,180);
                            whiteChecker[6] = new Sprite(white);
                            whiteChecker[6].setPosition(150,180);
                            whiteChecker[7] = new Sprite(white);
                            whiteChecker[7].setPosition(210,180);
                            whiteChecker[8] = new Sprite(white);
                            whiteChecker[8].setPosition(0,150);
                            whiteChecker[9] = new Sprite(white);
                            whiteChecker[9].setPosition(60,150);
                            whiteChecker[10] = new Sprite(white);
                            whiteChecker[10].setPosition(120,150);
                            whiteChecker[11] = new Sprite(white);
                            whiteChecker[11].setPosition(180,150);
                            }
                    }
                    catch(IOException e){
                        System.err.println("Failed loading sprites" + e.getMessage());
                    }
                    }
                    else                            //Every non red square
                       g.setColor(0,0,0);           //Colours the square black
                       g.fillRect(x, y, z, z);      //Draws the grid
                 }
            Thread t = new Thread(this);
            t.start();
            }
        }
        public void stop(){
            }
        public void run(){
            while(!sleeping){
                updateGame();
                draw(g);
                try{
                    Thread.sleep(frameRate);
                   }
                catch(InterruptedException ie) {
                    System.out.println(ie.getMessage());
                   }
            }
    }
        public void draw(Graphics g){
     
            for (int i=0; i<12; i++){
               blackChecker[i].paint(g);
               whiteChecker[i].paint(g);
            }
            flushGraphics();
            cursor.paint(g);
     
        }
        public void updateGame(){
            int keyState = this.getKeyStates();
            if ((keyState & LEFT_PRESSED) != 0)
                            {
                		curPositionX-=1;
                            cursor.setPosition(curPositionX,curPositionY);
     
                            }
            	if ((keyState & RIGHT_PRESSED) != 0)
                            {
                		curPositionX+=1;
                            cursor.setPosition(curPositionX,curPositionY);
                            }
            	if ((keyState & UP_PRESSED) != 0)
                            {
                		curPositionY-=1;
                            cursor.setPosition(curPositionX,curPositionY);
                            }
            	if ((keyState & DOWN_PRESSED) != 0)
                            {
                		curPositionY+=1;
                            cursor.setPosition(curPositionX,curPositionY);
                            }
           		checkBoundary(cursor);
     
        }
        public void checkBoundary(Sprite s) {
            	if (s.getX() < -s.getWidth())
                		s.setPosition(this.getWidth(),s.getY());
            	else if (s.getX() > this.getWidth())
                		s.setPosition(-s.getWidth(),s.getY());
                    if (s.getY() < - s.getHeight())
                		s.setPosition(s.getX(), this.getHeight());
            	else if (s.getY() > this.getHeight())
                		s.setPosition(s.getX(), - s.getHeight());
        		}
    }

    Thanks in advance

    Chris


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Moving sprite leaves a trail

    I'm not familiar with the library you're using, but I think the problem is because you're not removing the old sprite before changing the location of the piece.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving sprite leaves a trail

    I am sure you are right,

    however, I wouldn't know how to delete the old sprite. Wouldn't deleting the sprite delete the entire thing off the screen or am I thinking wrong here?

    Thanks for your help so far

  4. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Moving sprite leaves a trail

    Yes it would remove it from the screen, but you're re-drawing it back on in your update loop.

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving sprite leaves a trail

    How would I remove it from the screen? I'm really going round in circles with this now!

  6. #6
    Junior Member
    Join Date
    Feb 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Moving sprite leaves a trail

    I solved it by using a png image as the board - Probably not the ideal way to do it but needs must!

Similar Threads

  1. Need help... how to get the line moving?
    By thaiman in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 7th, 2010, 10:04 AM
  2. Alphabet from sprite sheet
    By Asido in forum Java Theory & Questions
    Replies: 2
    Last Post: September 18th, 2010, 11:49 AM
  3. Problem in navigation in a txt file with specific direction
    By wendybird79 in forum Collections and Generics
    Replies: 1
    Last Post: May 10th, 2009, 07:54 AM
  4. Problem with sprite rotation in graphics
    By Katotetu in forum Algorithms & Recursion
    Replies: 0
    Last Post: May 8th, 2009, 07:27 PM