简体   繁体   中英

Java Game - Making the Enemy move on X,Y Point using Threads

Good Day all.

I have the following Declarations and assignments and would like to know how i make my Image (A JLabel which has an Icon - The Enemy Unit) Move. The Label is called Enemy. I have implemented The Runnable and my code below - the switch Cases are situated in the overriden Run() method. Please also indicate PRECISELY where you place/run each method/instance/Thread. I will later implement multiple separate threads to do simultaneous actions, but 1st I need this cleared out. This is developed on Windows - Net beans for a windows project - Not for a handheld device

static Point EnemyLocation;

// This throws an error - Might it be due to putting an implementable
// overridden method in a thread? 

Thread moveTheEnemy = new Thread(run());

public Window() 
{
    initComponents();
    EnemyLocation = new Point(
        (int)(Enemy.getLocation().getX() + Enemy.getWidth()/2 - 7),
        (int)(Enemy.getLocation().getY() - 3 + Enemy.getHeight()/2)
    );
}

// How do i go about running this run method in that thread?
// And Can i Run it on a button click? Simply indicating
// ThreadName.start in the Button wont start the actions.

@Override
public void run(){

    boolean stop = false;

    // A Method is to be added here to flag for when the stop
    // will change... Dont worry  about this part... I need
    // the Cases below to work.

    while (stop == false) 
    {            

        for (int i = 0; i < 1; i++) 
        {
            Random rand = new Random();
            int a = rand.nextInt(4);

            switch (a) {
                case 0:
                    EnemyLocation.setLocation(
                        EnemyLocation.x + 4, EnemyLocation.y);
                    break;
                case 1:
                    EnemyLocation.setLocation(
                        EnemyLocation.x - 4, EnemyLocation.y);
                    break;
                case 2:
                    EnemyLocation.setLocation(
                        EnemyLocation.x, EnemyLocation.y + 4);
                    break;
                case 3:
                    EnemyLocation.setLocation(
                        EnemyLocation.x, EnemyLocation.y - 4);
                    break;
                default:
                    EnemyLocation.setLocation(
                        EnemyLocation.y + 4, EnemyLocation.y);

                    //It comes down to this Exception Each time...
                    throw new AssertionError("Failure to attempt Move");
            }
        }
    }  
}

eg javadoc tells you how to run a runnable in a thread (third example in doc). You just give the Runnable to the Thread constructor and start the Thread. However, i would suggest to work on your java rudiments and read some literatur on game programming before spending to much time on a non-sense project (excuse me). edit: "non-sense project" seems to be, in fact, to be too offensive. What i meant, was, that you wont finish successfully if you dont understand the basics and your code/question tells me, you dont. I may be wrong however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM