在JavaFX中使用多个图像文件控制动画或仅控制角色
基本上,我想找到一种方法来使用多个文件来显示动画角色随keyevents移动。我已经寻找了很多解决方案,但似乎都很模糊。到目前为止,我已经在JavaFX中使用多个图像文件控制动画或仅控制角色,javafx,game-development,Javafx,Game Development,基本上,我想找到一种方法来使用多个文件来显示动画角色随keyevents移动。我已经寻找了很多解决方案,但似乎都很模糊。到目前为止,我已经 public class BerzerkWorld extends Application { Random rnd = new Random(); final private int CanvasH = 1008; final private int CanvasW = 690; private static final Image pWalkLeft
public class BerzerkWorld extends Application {
Random rnd = new Random();
final private int CanvasH = 1008;
final private int CanvasW = 690;
private static final Image pWalkLeft = new Image("PlayerWalkLeft_104x40_sprite26x40.png");
private static final int pwalkLCol = 4;
private static final int pwalkLCnt = 4;
private static final int pwalkLOfstX = 0;
private static final int pwalkLOfstY = 0;
private static final int pwalkW = 26;
private static final int pwalkH = 40;
Pane playFieldLayer;
Pane GUILayer;
List<PlayerCharacterSprite> Player = new ArrayList();
List<Robut> Robut = new ArrayList();
@Override
public void start(Stage primaryStage) throws Exception {
Pane root = new Pane();
// create layers
ImageView background = new ImageView("Berzerk_Level-1_Maze.png");
final ImageView pWalkL = new ImageView(pWalkLeft);
pWalkL.setViewport(new Rectangle2D(0, 0 , 26, 40));
final Animation pWalkLAni = new SpriteManager()
root.getChildren().add(background);
primaryStage.setScene(new Scene(root));
primaryStage.setWidth(CanvasH);
primaryStage.setHeight(CanvasW);
primaryStage.show();
}
public static void main(String[] args){
launch(args);
}
作为一个精灵经理,有人能给我指出正确的方向吗 看一看
public class SpriteManager extends Transition{
private final ImageView imageView;
private final int count;
private final int columns;
private final int offsetX;
private final int offsetY;
private final int width;
private final int height;
private int lastIndex;
public SpriteManager(Pane Layer,
ImageView imageView,
Duration duration,
int count, int columns,
int offsetX, int offsetY,
int width, int height){
this.imageView = imageView;
this.count = count;
this.columns = columns;
this.offsetX = offsetX;
this.offsetY = offsetY;
this.width = width;
this.height = height;
setCycleDuration(duration);
setInterpolator(Interpolator.LINEAR);
}
protected void interpolate(double arg0) {
final int index = Math.min((int) Math.floor(k * count), count - 1);
if (index != lastIndex){
final int x = (index % columns) * width + offsetX;
final int y = (index / columns) * height + offsetY;
imageView.setViewport(new Rectangle2D(x,y,width,height));
lastIndex = index;
}
}
public void addToLayer(){
this.layer.getChildren().add(this.imageView);
}
}