渲染循环开始后 libgdx 中的一次线程
One time thread in libgdx after render loop starts
是否可以 运行 一个线程在渲染方法之后仅执行一次,因为渲染循环将在每次迭代中调用它。这是我的话题:
new Thread(new Runnable() {
float time=0;
@Override
public void run() {
while(redPos<10) {
time += Gdx.graphics.getDeltaTime();
redPos+=(int)time;
}
}
}).start();
我不太熟悉线程,所以不知道正确的方法。
如有不妥请指正
更新:我通过将 redPos 增量转移到像这样的渲染方法来让它工作-
float time = 0;
void render(){
if(redPos<10){
time += Gdx.graphics.getDeltaTime();
redPos+=(int)time;
}
batch.begin();
batch.draw(ball, redPos, 5);
batch.end();
}
使用 Thread 为 sprite 设置动画是个坏主意,因为它会与渲染线程不同步。
您可能不想为此任务避开线程。您可以创建一个扩展 Sprite
的 class,并在 draw
方法中编写一些代码来逐步更改 Sprite
的比例。
public class MySprite extends Sprite {
private boolean doScale;
private float scaleBy;
private float scaleTime;
private float timeElapsed;
private float progress;
private float lastProgress;
public MySprite(Texture texture) {
super(texture);
}
public void scaleTo(float scaleTo, float time){
scaleBy(scaleTo - getScaleX(),time);
}
public void scaleBy(float scaleBy, float time){
this.scaleBy = scaleBy;
doScale = true;
scaleTime = time;
timeElapsed = 0;
lastProgress = 0;
setOriginCenter();
}
@Override
public void draw(Batch batch) {
if(doScale){
timeElapsed += Gdx.graphics.getDeltaTime();
progress = timeElapsed/scaleTime;
progress = progress > 1?1: progress;
float progressDelta = progress - lastProgress;
float scaleBy = this.scaleBy *progressDelta;
setScale(getScaleX()+scaleBy,getScaleY()+scaleBy);
lastProgress = progress;
if(progress == 1)
doScale = false;
}
super.draw(batch);
}
}
然后在MySprite
...
中使用方法scaleTo()
或scaleBy()
@Override
public void create () {
...
...
...
mySprite = new MySprite(texture);
mySprite.scaleTo(2,1);//Scale to 200% in 1 seconds
}
尽管 libGdx 已经为此提供 Actions
,您可能需要考虑使用 Actor
@Override
public void create () {
...
...
...
imageActor = new Image(texture);
imageActor.addAction(Actions.scaleTo(2,2,1));//Scale to 200% in 1 seconds
}
是否可以 运行 一个线程在渲染方法之后仅执行一次,因为渲染循环将在每次迭代中调用它。这是我的话题:
new Thread(new Runnable() {
float time=0;
@Override
public void run() {
while(redPos<10) {
time += Gdx.graphics.getDeltaTime();
redPos+=(int)time;
}
}
}).start();
我不太熟悉线程,所以不知道正确的方法。 如有不妥请指正
更新:我通过将 redPos 增量转移到像这样的渲染方法来让它工作-
float time = 0;
void render(){
if(redPos<10){
time += Gdx.graphics.getDeltaTime();
redPos+=(int)time;
}
batch.begin();
batch.draw(ball, redPos, 5);
batch.end();
}
使用 Thread 为 sprite 设置动画是个坏主意,因为它会与渲染线程不同步。
您可能不想为此任务避开线程。您可以创建一个扩展 Sprite
的 class,并在 draw
方法中编写一些代码来逐步更改 Sprite
的比例。
public class MySprite extends Sprite {
private boolean doScale;
private float scaleBy;
private float scaleTime;
private float timeElapsed;
private float progress;
private float lastProgress;
public MySprite(Texture texture) {
super(texture);
}
public void scaleTo(float scaleTo, float time){
scaleBy(scaleTo - getScaleX(),time);
}
public void scaleBy(float scaleBy, float time){
this.scaleBy = scaleBy;
doScale = true;
scaleTime = time;
timeElapsed = 0;
lastProgress = 0;
setOriginCenter();
}
@Override
public void draw(Batch batch) {
if(doScale){
timeElapsed += Gdx.graphics.getDeltaTime();
progress = timeElapsed/scaleTime;
progress = progress > 1?1: progress;
float progressDelta = progress - lastProgress;
float scaleBy = this.scaleBy *progressDelta;
setScale(getScaleX()+scaleBy,getScaleY()+scaleBy);
lastProgress = progress;
if(progress == 1)
doScale = false;
}
super.draw(batch);
}
}
然后在MySprite
...
scaleTo()
或scaleBy()
@Override
public void create () {
...
...
...
mySprite = new MySprite(texture);
mySprite.scaleTo(2,1);//Scale to 200% in 1 seconds
}
尽管 libGdx 已经为此提供 Actions
,您可能需要考虑使用 Actor
@Override
public void create () {
...
...
...
imageActor = new Image(texture);
imageActor.addAction(Actions.scaleTo(2,2,1));//Scale to 200% in 1 seconds
}