Hello,

Thank you for your time!

I have an applet where I have two blocks moving:

When the program starts:
a)The first block is constantly moving with a time delay of 100 ms about the horizontal direction at the top end of the applet (x starts at 0 and is increased in steps of 10 until it hits the corner and then x is decreased and so on).

b) The second block is stationary at first and when the enter key is pressed, it starts moving every 10 ms in either of the four directions depending on the arrow key movements (using KeyListener).The boundaries are checked and the directions are changed when the block bumps against the four walls.The second block stops when a shift key is pressed.It resumes when an enter key is pressed again.

My issue:
When the program starts, the first block is moving every 100 ms and soon as I press the enter key, both the blocks start moving every 10 ms. As soon as I press the shift key, the second block stops moving as it should and first block goes back to moving at 100 ms. Obviously, this is not what I want.

Kindly let me know how can I fix this issue sothat when I press the enter key, the first block moves at 100ms and the second block moves at 10 ms. My program is below

Thank you!
Jet

import java.awt.*;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.awt.event.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SnakeBoards extends JApplet implements KeyListener
{
    private int x = 100, y = 100, x1 = 10, y1 = 10;
    private int flag = 0, flag1 = 0;
    private BufferedImage image;
    private Timer timer1, timer2;

    public void init()
    {
        addKeyListener (this);
        setFocusable(true); // setting the KeyListener Focus on the JPanel to enable Key Events
        timer1 = new Timer (10,

            new ActionListener()
            {

                public void actionPerformed(ActionEvent e)
                {

                    if (flag1 == 0)
                    {
                        x1+= 10;
                    }
                    else
                    {
                        x1 -= 10;
                    }
                    if (flag == 1)
                    {
                        x += 10;
                    }
                    else if (flag == 2)
                    {
                        x -= 10;
                    }
                    else if (flag == 3)
                    {
                        y -= 10;
                    }
                    else if (flag == 4)
                    {
                        y += 10;
                    }
                    if (x1 >= getWidth())
                    {
                        x1 = x1 - 10;
                        flag1 = 1;
                    }
                    if (x1 <= 0)
                    {
                        x1 = x1 + 10;
                        flag1 = 0;
                    }
                    if (x >= getWidth())
                    {
                        x = x - 10;
                        flag = 2;
                    }
                    if (x <= 0)
                    {
                        x = x + 10;
                        flag = 1;
                    }
                    if (y <= 0)
                    {
                        y = y + 10;
                        flag = 4;
                    }
                    if (y >= getWidth())
                    {
                        y = y - 10;
                        flag = 3;
                    }

                    repaint();
                }
            }
        );

        timer2 = new Timer (100,

            new ActionListener()
            {

                public void actionPerformed(ActionEvent e)
                {

                    if (flag1 == 0)
                    {
                        x1+= 10;
                    }
                    else
                    {
                        x1 -= 10;
                    }

                    if (x1 >= getWidth())
                    {
                        x1 = x1 - 10;
                        flag1 = 1;
                    }
                    if (x1 <= 0)
                    {
                        x1 = x1 + 10;
                        flag1 = 0;
                    }
                    repaint();
                }
            }
        );
        timer2.start();
    }

    public void keyPressed(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_RIGHT ) //right shift key
        {
            flag = 1;
        }

        if (e.getKeyCode() == KeyEvent.VK_LEFT ) //left shift key
        {
            flag = 2;
        }

        if (e.getKeyCode() == KeyEvent.VK_UP ) // up arrow key
        {
            flag = 3;
        }

        if (e.getKeyCode() == KeyEvent.VK_DOWN ) //down arrow key
        {
            flag = 4;
        }
        if (e.getKeyCode() == KeyEvent.VK_SHIFT ) //shift key
        {
            timer1.stop();
        }
        if (e.getKeyCode() == KeyEvent.VK_ENTER ) //enter key
        {
            flag = 1;
            timer1.start();
        }

    }

    public void keyReleased (KeyEvent e)
    {

    }

    public void keyTyped (KeyEvent e)
    {

    }

    public void paint(Graphics g)
    {
        g.setColor(Color.BLACK);
        g.fillRect (0, 0, getWidth(), getHeight());
        g.setColor (Color.WHITE);
        g.fillRect (x, y, 30, 30);
        g.setColor (Color.WHITE);
        g.fillRect (x1, y1, 30, 30);

    }
}"

You are stopping and starting to switch between two timers, so the timing keeps changing.
One solution:
Have 1 Timer that just moves block 1. Start this and leave it running
Have another Timer that just moves block 2. Stop and start it however you like because that won't affect block 1.

However, I would prefer to have all my blocks moving at the same time interval - fast enough for smooth movement (eg >=25/sec) but not so fast that it justs burns CPU pointelessly (eg <= 60/sec). If a block isn't moving at some given instant then simply return from its move method without having changed the coordinates.
I think this scales better, and is a lot easier of you ever need to think about collisions between blocks.

Hello,

Thank you! That solved my problem.

Regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.