演员在 screen2d 中不动
Actor not moving in screen2d
我想使用 moveToAction 移动演员。但如果我使用 moveToAction,它就不会移动。如果在 draw 方法中更改 x,y 它有效,但不适用于 movetoaction
public class Aks extends Actor {
private State state;
private MainGame game;
private TextureAtlas movingAtlas;
private Animation movingAnimation;
private float elapsedTime = 0f;
public Aks(MainGame game) {
this.game = game;
movingAtlas = new TextureAtlas(Gdx.files.internal("atlas/myaltas/atlas.atlas"));
movingAnimation = new Animation(1f/15f, movingAtlas.getRegions());
TextureRegion texture = (TextureRegion) movingAnimation.getKeyFrame(elapsedTime, true);
setBounds(getX(),getY(),texture.getRegionWidth(),texture.getRegionHeight());
MoveToAction moveAction = new MoveToAction();
moveAction.setPosition(300f, 300f);
moveAction.setDuration(10f);
this.addAction(moveAction);
addListener(new ActorGestureListener(){
@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
Gdx.app.log("Tag", "Actor touched x = " );
super.tap(event, x, y, count, button);
}
});
}
@Override
public void draw(Batch batch, float alpha){
drawFlying(batch);
}
void drawFlying(Batch batch){
TextureRegion texture = (TextureRegion) movingAnimation.getKeyFrame(elapsedTime, true);
setBounds(getX(),getY(),texture.getRegionWidth(),texture.getRegionHeight());
Gdx.app.log("Tag", "x =" + getX() + " y =" + getY() );
batch.draw(texture, getX(),getY(),50,50);
}
@Override
public void act(float delta){
elapsedTime = elapsedTime+delta;
}
}
您的绘图函数需要调用 super.draw(batch, alpha) 才能影响子操作。
你需要调用super.act(delta),在actorclass中,act方法控制所有的动作,所以如果你不调用它,动作就不会发生,在相反,你不需要调用 super.draw() 那是因为 actor class 只是为实体的逻辑部分创建的,你必须定义它在需要时将绘制的内容。
希望对您有所帮助!
我想使用 moveToAction 移动演员。但如果我使用 moveToAction,它就不会移动。如果在 draw 方法中更改 x,y 它有效,但不适用于 movetoaction
public class Aks extends Actor {
private State state;
private MainGame game;
private TextureAtlas movingAtlas;
private Animation movingAnimation;
private float elapsedTime = 0f;
public Aks(MainGame game) {
this.game = game;
movingAtlas = new TextureAtlas(Gdx.files.internal("atlas/myaltas/atlas.atlas"));
movingAnimation = new Animation(1f/15f, movingAtlas.getRegions());
TextureRegion texture = (TextureRegion) movingAnimation.getKeyFrame(elapsedTime, true);
setBounds(getX(),getY(),texture.getRegionWidth(),texture.getRegionHeight());
MoveToAction moveAction = new MoveToAction();
moveAction.setPosition(300f, 300f);
moveAction.setDuration(10f);
this.addAction(moveAction);
addListener(new ActorGestureListener(){
@Override
public void tap(InputEvent event, float x, float y, int count, int button) {
Gdx.app.log("Tag", "Actor touched x = " );
super.tap(event, x, y, count, button);
}
});
}
@Override
public void draw(Batch batch, float alpha){
drawFlying(batch);
}
void drawFlying(Batch batch){
TextureRegion texture = (TextureRegion) movingAnimation.getKeyFrame(elapsedTime, true);
setBounds(getX(),getY(),texture.getRegionWidth(),texture.getRegionHeight());
Gdx.app.log("Tag", "x =" + getX() + " y =" + getY() );
batch.draw(texture, getX(),getY(),50,50);
}
@Override
public void act(float delta){
elapsedTime = elapsedTime+delta;
}
}
您的绘图函数需要调用 super.draw(batch, alpha) 才能影响子操作。
你需要调用super.act(delta),在actorclass中,act方法控制所有的动作,所以如果你不调用它,动作就不会发生,在相反,你不需要调用 super.draw() 那是因为 actor class 只是为实体的逻辑部分创建的,你必须定义它在需要时将绘制的内容。
希望对您有所帮助!