我尝试在我的游戏中实现爆炸,但动画在第一帧后停止。如果运动员失去健康,则应触发爆炸。我做了3个课程。动画类,爆炸类和GameActivity类。我试图只发布相关的部分。 我搜索了互联网,但我无法找到解决问题的方法。所以我问你是否可能看到我的错误并且可以帮助我。如果需要更多代码,我当然会编辑它。也许我只是在忽视某些事情。
Animation Class{
public void setFrames(Bitmap[] frames)
{
this.frames = frames;
currentFrame = 0;
startTime = System.nanoTime();
}
public void setDelay(long d){delay = d;}
public void setFrame(int i){currentFrame= i;}
public void work()
{
long elapsed = (System.nanoTime()-startTime)/1000000;
if(elapsed>delay)
{
currentFrame++;
startTime = System.nanoTime();
}
if(currentFrame == frames.length){
currentFrame = 0;
playedOnce = true; }}
public Bitmap getImage(){
return frames[currentFrame];
}
public int getFrame(){return currentFrame;}
public boolean playedOnce(){return playedOnce; }}
这是爆炸类的相关部分:
Bitmap[] image = new Bitmap[numFrames];
//Bitmap has 4 * 4 frames
for (int i = 0; i < image.length; i++) {
if (i % 4 == 0 && i > 0) row++;
image[i] = Bitmap.createBitmap(spritesheet, (i - (4 * row)) * width,
row * height, width*4, height*4);
}
animation.setFrames(image);
animation.setDelay(10);
}
public void animating() {
if (!animation.playedOnce()) {
animation.work(); }}
如果玩家失去了所有的健康,我会在GameActivity中调用爆炸
if (player.getHealth() < 0) {
// player bitmap disappears
dissapear = true;
//game ends
gameEnded=true;
explosion = new Explosion(BitmapFactory.decodeResource(getResources(),
R.drawable.images),player.getX(),player.getY()-10,9,9,25);
explosion.animating();
答案 0 :(得分:0)
这是我用来加载精灵表并抓住精灵的主要代码:
package com.animation.main;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
public class Main extends JFrame{
BufferedImage sprite;
Animator player;
public Main(){
setSize(800, 600);
setVisible(true);
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
init();
}
private void init(){
BufferedImageLoader loader = new BufferedImageLoader();
BufferedImage spriteSheet = null;
try {
spriteSheet = loader.loadImage("insert your sprite sheet path here");
} catch (IOException ex) {
// TODO Auto-generated catch block
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
SpriteSheet ss = new SpriteSheet(spriteSheet);
ArrayList<BufferedImage> sprites = new ArrayList<BufferedImage>();
sprites.add(ss.grabSprite(0, 16, 92, 109));
sprites.add(ss.grabSprite(94, 16, 92, 109));
sprites.add(ss.grabSprite(196, 12, 92, 115));
sprites.add(ss.grabSprite(387, 5, 92, 115));
sprites.add(ss.grabSprite(387, 5, 92, 115));
sprites.add(ss.grabSprite(487, 5, 92, 115));
sprites.add(ss.grabSprite(587, 5, 77, 115));
player = new Animator(sprites);
player.setSpeed(100);
player.start();
}
Image dbImage;
Graphics dbg;
public void paint(Graphics g){
dbImage = createImage(getWidth(), getHeight());
dbg = dbImage.getGraphics();
paintComponent(dbg);
g.drawImage(dbImage, 0, 0, null);
}
private void paintComponent(Graphics g) {
if(player != null) {
g.drawImage(player.sprite, 100, 100, null);
player.update(System.currentTimeMillis());
}
repaint();
}
public static void main(String[] args){
Main main = new Main();
}
}
'sprites.add(ss.grabSprite(0,16,92,109))的参数;'代码是精灵左上角spriteSheet及其宽度和高度的x和y值。
当玩家健康状况小于或等于0时,在paintComponent类中运行代码。它看起来像这样:
private void paintComponent(Graphics g) {
if(player != null && playerHealth <= 0) {
g.drawImage(player.sprite, 100, 100, null);
player.update(System.currentTimeMillis());
}
repaint();
}
这是我用于spritesheet的代码:
package com.animation.main;
import java.awt.image.BufferedImage;
public class SpriteSheet {
public BufferedImage spriteSheet;
public SpriteSheet(BufferedImage ss){
this.spriteSheet = ss;
}
public BufferedImage grabSprite(int x, int y, int width, int height){
BufferedImage sprite = spriteSheet.getSubimage(x, y, width, height);
return sprite;
}
抓取精灵函数和BufferedImage精灵从spriteSheet中获取精灵。
这是BufferedImageLoader:
package com.animation.main;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
public class BufferedImageLoader {
public BufferedImage loadImage(String pathRelativeToThis) throws IOException{
URL url = this.getClass().getResource(pathRelativeToThis);
BufferedImage img = ImageIO.read(url);
return img;
}
}
这是动画师类:
package com.animation.main;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
public class Animator {
ArrayList<BufferedImage> frames;
BufferedImage sprite;
private volatile boolean running = false;
private long previousTime, speed;
private int frameAtPause, currentFrame;
public Animator(ArrayList<BufferedImage> frames){
this.frames = frames;
}
public void setSpeed(long speed){
this.speed = speed;
}
public void update(long time){
if(running){
if(time - previousTime >= speed){
// Update animation
currentFrame++;
try{
sprite = frames.get(currentFrame);
} catch(IndexOutOfBoundsException e){
currentFrame = 0;
sprite = frames.get(currentFrame);
}
previousTime = time;
}
}
}
public void start(){
running = true;
previousTime = 0;
currentFrame = 0;
frameAtPause = 0;
}
public void stop(){
running = false;
previousTime = 0;
currentFrame = 0;
frameAtPause = 0;
}
public void pause(){
frameAtPause = 0;
}
public void resume(){
}
}
答案 1 :(得分:0)
我解决了。问题是我的游戏活动。我不得不改变
if (player.getHealth() < 0) {
// player bitmap disappears
dissapear = true;
//game ends
gameEnded=true;
explosion = new Explosion(BitmapFactory.decodeResource(getResources(),
R.drawable.images),player.getX(),player.getY()-10,9,9,25);
explosion.animating();}
到
if (player.getHealth() < 0) {
// player bitmap disappears
dissapear = true;
//game ends
gameEnded=true;
explosion = new Explosion(BitmapFactory.decodeResource(getResources(),
R.drawable.images),player.getX(),player.getY()-10,9,9,25);
}
if (dissapear){explosion.animating();}