我正在尝试让我的3个字符执行我制作的文本文件中的10个命令。这些命令包括3种动作:MOVETO,TURNTO和PLAY(声音文件)。当我运行程序时,字符出现,但它们根本不移动。我该如何解决这个问题?
我有3个不同的课程:
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;
public class Cmd {
ArrayList<String> action;//stores whether TURNTO, MOVETO
ArrayList<String> params;
int nCmds;
Cmd(){
nCmds=0;
action=new ArrayList<String>();
params=new ArrayList<String>();
}
boolean cmdRead(String fname) throws FileNotFoundException{
Scanner fs = new Scanner(new FileReader(fname));
while(fs.hasNext()){
String act=fs.next();
if(act.equals("MOVETO") || act.equals( "TURNTO") || act.equals("PLAY")){
nCmds+=1;
action.add(act);
params.add(fs.nextLine().trim());
}
}
fs.close();
if( nCmds > 0){
return true;
}else{
return false;
}
}
String getAction(){
if(nCmds>0){
return action.get(0);
}else{
return "";
}
}
String getParam(){
if(nCmds>0){
return params.get(0);
}else{
return "";
}
}
boolean advance(){
if(nCmds >0){
nCmds--;
action.remove(0);
params.remove(0);
}
if(nCmds>0){
return true;
}else{
return false;
}
}
}
import java.io.FileNotFoundException;
public class Actor {
private EZImage img; // Member variable to store bug picture
private int x, y, startx, starty; // Member variables to store x, y, startx, starty
private int destx, desty; // Member variables to store destination values
private long starttime; // Member variable to store the start time
private long duration; // Member variable to store the duration
private boolean interpolation; // Member variable that is set to true if it is in the interpolation state
// ********* ROTATE
private float currRot= 0;
private float destRot = 0;
private float startRot = 0;
private boolean interpolationRot;
private boolean hasCmd;
private Cmd cmds;
// Constructor for character takes 3 parameters
public Actor(String filename,int posx, int posy){
// Set the current position of the character
x=posx;y=posy;
// Create the image of the bug
img = EZ.addImage(filename, posx, posy);
img.scaleTo(0.5);
// Move it to the starting position. This is actually redundant.
img.translateTo(x,y);
// Set interpolation mode to false initially
interpolation = false;
interpolationRot = false;
//(1)initialize hasCmd, and cmds object
hasCmd=false;
cmds=new Cmd();
}
public void readCmd(String fname) throws FileNotFoundException{
//(2) Read a file using the cmds variable
hasCmd=cmds.cmdRead(fname);
}
public void setCmd(){
//(3) Go through cmds variable and set next command
String action=cmds.getAction();
String parameters=cmds.getParam();
String []params=parameters.split(" ");
int x,y,ang,dur;
switch(action) {
case "PLAY":
EZSound sound=EZ.addSound(params[0]);
sound.play();
break;
case"MOVETO":
x=Integer.parseInt(params[0]);
y=Integer.parseInt(params[1]);
dur=Integer.parseInt(params[2]);
setDestination(x,y,dur);
break;
case"TURNTO":
ang=Integer.parseInt(params[0]);
dur=Integer.parseInt(params[1]);
setRot(ang,dur);
break;
default:
}
hasCmd=cmds.advance();
}
// Set the destination by giving it 3 variables
// Dur means duration and is measured in seconds
public void setDestination(int posx, int posy, long dur){
// Set destination position and duration
// Convert seconds to miliseconds
destx = posx; desty = posy; duration = dur*1000;
// Get the starting time (i.e. time according to your computer)
starttime = System.currentTimeMillis();
// Set the starting position of your bug
startx=x; starty=y;
// Set interpolation mode to true
interpolation = true;
}
// ********* Set our Rotation
public void setRot(float s, float dur){
destRot = s;
starttime = System.currentTimeMillis();
duration = (long) (dur*1000);
startRot = currRot;
interpolationRot = true;
}
// If youʻre in an interpolation state or have commands to process then return true, else false.
public boolean moving() {
//(4)return whether object is in process of moving or there are commands in the queue
return (interpolation || interpolationRot || hasCmd);
}
public boolean hasCmd(){
return hasCmd;
}
// This moves the character based on the current time and elapsed time according to the interpolation equation
public void go(){
// If interpolation mode is true then do interpolation
if (interpolation == true) {
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime)/ (float) duration;
// Calculate the interpolated position of the bug
x = (int) (startx + ((float) (destx - startx) * normTime));
y = (int) (starty + ((float) (desty - starty) * normTime));
// If the difference between current time and start time has exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolation = false;
// Move the character all the way to the destination
x = destx; y = desty;
}
// Don't forget to move the image itself.
img.translateTo(x,y);
}else if (interpolationRot == true){
// Normalize the time (i.e. make it a number from 0 to 1)
float normTime = (float) (System.currentTimeMillis() - starttime)/ (float) duration;
// Calculate the interpolated position of the bug
currRot = (startRot + ((float) (destRot - startRot) * normTime));
// If the difference between current time and start time has exceeded the duration
// then the animation/interpolation is over.
if ((System.currentTimeMillis() - starttime) >= duration) {
// Set interpolation to false
interpolationRot = false;
// Scale the character all the way to the destination
currRot = destRot;
}
// Donʻt forget to move the image itself.
img.rotateTo(currRot);
}else if (hasCmd){
//(5) if there is a command left set it
setCmd();
}
}
}
import java.awt.Color;
import java.io.FileNotFoundException;
public class Animation {
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
// Setup EZ graphics system.
EZ.initialize(1024, 1024);
// Set background color to dark green
EZ.setBackgroundColor(Color.BLUE);
//(6) Make characters
Actor monkey=new Actor("monkey2.png",500,500);
Actor pikachu=new Actor("pikachu-0.png", 300,500);
Actor luigi=new Actor("Luigi.png", 700,500);
//(7) read commands from files
monkey.readCmd("Monkey.txt");
pikachu.readCmd("Pikachu.txt");
luigi.readCmd("Luigi.txt");
//(8) reanimate them!
while(monkey.moving()) {
monkey.go();
}
while (pikachu.moving()) {
pikachu.go();
}
while (luigi.moving()) {
luigi.go();
}
EZ.refreshScreen();
}
}