【发布时间】:2015-11-25 17:53:54
【问题描述】:
我正在尝试在 Android Studio 中使用 LibGDX 创建一个太空射击游戏。 在我尝试使用 javafx.animation.Animation 类创建动画之前,其他一切工作正常。在编译我的代码时,我收到以下 3 个错误和 1 个警告!
Warning:[options] bootstrap class path not set in conjunction with -source 1.6
Error:(11, 24) error: package javafx.animation does not exist
Error:(22, 13) error: cannot find symbol class Animation
Error:(44, 26) error: cannot find symbol class Animation
我检查了我的 android studio 确实有 javafx 插件!
我的代码中有两个类 Shootergame.java 和 AnimatedSprite.java
问题在于 AnimatedSprite.java 类,该类的代码如下!
package com.ahmed.nullapointershooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import javafx.animation.Animation;
/**
* Created by Hafeez ur Rehman on 8/30/2015.
**/
public class AnimatedSprite {
private static final int FRAMES_COL = 2;
private static final int FRAMES_ROW = 2;
private Sprite sprite;
private Animation animation;
private TextureRegion[] frames;
private TextureRegion currentFrame;
private float stateTime;
public AnimatedSprite(Sprite sprite) {
this.sprite = sprite;
Texture texture = sprite.getTexture();
TextureRegion[][] temp = TextureRegion.split(texture, texture.getWidth() / FRAMES_COL,
texture.getHeight() / FRAMES_ROW);
frames = new TextureRegion[FRAMES_COL * FRAMES_ROW];
int index = 0;
for (int i = 0; i < FRAMES_ROW; i++) {
for (int j = 0; j < FRAMES_COL; j++) {
frames[index++] = temp[i][j];
}
}
/**
* THE PROBLEM LIES HERE . I WANT TO DO THIS ----
*
*/
//animation = new Animation(0.1f, frames);
animation = new Animation() {
@Override
public void impl_playTo(long l, long l1) {
}
@Override
public void impl_jumpTo(long l, long l1) {
}
};
stateTime = 0f;
}
public void setPosition(float x, float y){
float widthOffset = sprite.getWidth() / FRAMES_COL;
sprite.setPosition(x - widthOffset / 2, y);
}
public void draw(SpriteBatch spriteBatch){
stateTime += Gdx.graphics.getDeltaTime();
//currentFrame = animation.getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, sprite.getX(), sprite.getY());
}
}
还有 Shootergame.java 的代码仅供参考!
package com.ahmed.nullapointershooter;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ShooterGame extends ApplicationAdapter {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture background;
private AnimatedSprite spaceshipAnimated;
@Override
public void create() {
//Texture.setEnforcePotImages(false);
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
background = new Texture(Gdx.files.internal("NullapointerBackground.png"));
Texture spaceshipTexture = new Texture(Gdx.files.internal("Spaceshipcanvas.png"));
Sprite spaceshipSprite = new Sprite(spaceshipTexture);
spaceshipAnimated = new AnimatedSprite(spaceshipSprite);
spaceshipAnimated.setPosition(800 / 2, 0);
}
@Override
public void dispose() {
batch.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(background, 0, 0);
spaceshipAnimated.draw(batch);
batch.end();
}
@Override
public void resize(int width, int height) {
}
}
【问题讨论】:
-
android上没有javafx包
-
我正在使用 Libgdx 为多个平台创建游戏,即 android 、 Java (desktop) 。我应该用什么代替 javafx 来执行动画?
-
我只使用了一点点javafx,但没有使用动画,不确定您需要什么,我认为您应该开始一个新问题,具体询问您需要什么动画以及使用什么android类。
-
你是对的。我的问题解决了。我使用 Javafx 导入动画类,这首先是错误的。相反,我不得不导入 import com.badlogic.gdx.graphics.g2d.Animation;现在它的工作!非常感谢
标签: java android animation javafx libgdx