使用我当前的代码,我有一个角色可以加载到动画的精灵表中。当我运行代码时出现了我的问题。游戏运行良好,只是玩家加载/循环精灵的速度太快了。我相信这源于整体游戏时间/更新。我应该声明游戏正在通过一个小程序运行。我收集了一些关于 SwingerWorkers 的信息,但我不知道如何根据需要对其进行操作。如果有人可以查看我的代码并让我知道我能做什么,我将不胜感激。
播放器构造函数:
public Player(Image pic, ImageObserver IO){
positionX=200;
positionY=200;
destinationX=(int)positionX;
destinationY=(int)positionY;
speed = 2.3;
girlImage=pic;
this.io=IO;
width=120;
height=100;
//this.playerImage=playerImage;
// Gets each sprite
BufferedImage img = createImage();
for(int j=0;j<4;j++)
{
for(int i = 0; i < frameCount; i++) {
girlAll[j][i] = girl[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
}//end of for
}//end of for
}
// Image loader
private BufferedImage createImage(){
//BufferedImage bufferedImage;
try {
girl[0] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northeast_walk.png"));
girl[1] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northwest.png"));
girl[2] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southeast.png"));
girl[3] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southwest.png"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
绘制方法:
public void draw(Graphics g){
//g.drawImage(girlImage, (int)positionX,(int) positionY,io);
picNum = (picNum + 1) % frameCount;
pics = girlAll[x];
if (x==0)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
if (x==1)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
if (x==2)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
if (x==3)
g.drawImage(pics[picNum], (int) (positionX), (int) (positionY), io);
}
SwingWorker 尝试:
SwingWorker worker = new SwingWorker<BufferedImage[], Void>() {
public BufferedImage[] doInBackground(){
//BufferedImage bufferedImage;
try {
girl[0] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northeast_walk.png"));
girl[1] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_northwest.png"));
girl[2] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southeast.png"));
girl[3] = ImageIO.read(new File("C:/Users/blutuu/Documents/Marlin/UD/Workspace S13/MoD/src/female_player_southwest.png"));
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public void loadImg() {
doInBackground();
for(int j=0;j<4;j++)
{
for(int i = 0; i < frameCount; i++) {
girlAll[j][i] = girl[j].getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
}//end of for
}//end of for
}
};
}
运行游戏(来自“游戏”类):
public void init(){
//Execute a job on the event-dispatching thread:
//creating this applet's GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createGUI();
}
});
} catch (Exception e) {
System.err.println("createGUI didn't successfully complete");
}
}
线程处理:
private void gameStuff(){
try {Thread.sleep(sleepTime);}
catch (InterruptedException e) {}
mouseClicked=false;
}
游戏渲染:
public void update (Graphics g)
{
// initialize buffer
if (dbImage == null)
{
dbImage = createImage (this.getSize().width, this.getSize().height);
dbg = dbImage.getGraphics ();
}
// clear screen in background
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize().width, this.getSize().height);
// draw elements in background
//dbg.setColor (getForeground());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
另一种渲染方法:
private void hud(Graphics g){
g.setColor(Color.orange);
g.fillRect(300, 0, 500, 80);
g.fillRect(200, 0, 100, 80);
//SHOVEL IMAGE AND BUTTON
g.drawImage(shovelImage, 520,0,this);
g.setColor(Color.black);
g.drawRect(520, 0, 60, 80);//show boundaries
if(mouseClicked && new Rectangle(520, 0, 60, 80).contains(mx,my))
toolSelected=1; //sets tool selected to shovel
//ROCKS IMAGE AND BUTTON
g.drawImage(rocksImage, 600,0, this);
g.drawRect(600, 0, 60, 80);//show boundaries
if(mouseClicked && new Rectangle(600, 0, 60, 80).contains(mx,my))
toolSelected=2; //sets tool selected to shovel
//JACKHAMMER IMAGE AND BUTTON
g.drawImage(jackHammerImage, 680,0, this);
g.drawRect(680, 0, 60, 80);//show boundaries
if(mouseClicked && new Rectangle(680, 0, 60, 80).contains(mx,my))
toolSelected=3; //sets tool selected to shovel
if(timer.getAngle()<360){
timer.draw(g);
}
if(timer.getAngle()==360){
raining=true;
timer.reset();
addDrops();
roundNum++;
puddles();
}
drawProgressBar(g);
if(toolSelected == 1)
{
Message = "Native Delaware plants";
Message2 = "are good";
}
else if(toolSelected == 2)
{
Message = "Rocks with Runoff";
Message2 = " ";
}
else if(toolSelected == 3)
{
Message = "Permeated Driveways";
Message2 = "help with excesse water";
}
else{
Message = "Play On";
Message2 = " ";
}
g.drawString("Round "+roundNum, 350, 30);
g.drawString(Message, 205, 40);
g.drawString(Message2, 205, 55);
drawTiles(g);
}