[英]NEED some guides on moving actors on stage libgdx
i am currently facing a problem where i try to move my actor by touchdown
and i am confuse and did some error. 我目前遇到一个问题,我试图通过
touchdown
来移动演员,但我感到困惑,并犯了一些错误。 I am trying to move my bucket to the left and right by dragging the actor. 我正在尝试通过拖动演员将水桶左右移动。 This is my code package com.tcyzzz.savethesemester;
这是我的代码包com.tcyzzz.savethesemester;
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.Batch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class MyGdxGame extends ApplicationAdapter { 公共类MyGdxGame扩展了ApplicationAdapter {
Stage stage;
@Override
public void create () {
stage = new Stage(new ScreenViewport());//establish a stage for the game
MyActor actor = new MyActor();
stage.addActor(actor);
stage.setKeyboardFocus(actor);
actor.addListener(new InputListener(){
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
MoveByAction mba = new MoveByAction();
mba.setAmount(5,0);
stage.addAction(mba);
return true;
}
});
Gdx.input.setInputProcessor(stage);//handle user input
}
@Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();// call all sprites in actors class
}
@Override
public void dispose () {
stage.dispose();//stage dispose
}
} }
and my actors class 和我的演员班
package com.tcyzzz.savethesemester;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
class MyActor extends Actor { Sprite sprite = new Sprite(new Texture(Gdx.files.internal("bucket.png"))); 类MyActor扩展Actor {Sprite sprite = new Sprite(new Texture(Gdx.files.internal(“ bucket.png”)));
public MyActor(){
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
setTouchable(Touchable.enabled);
}
@Override
public void draw(Batch batch, float parentAlpha) {
sprite.draw(batch);
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
}
@Override
public void act(float delta) {
super.act(delta);
}
} }
i need a clear instruction on how to move the bucket by dragging it using stage , actors .. thanks 我需要有关如何使用舞台,演员拖动铲斗来移动铲斗的明确说明。谢谢
@Tenfour04 is right @ Tenfour04是正确的
instead 代替
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
should use: 应该使用:
setBounds(getX(),getY(),sprite.getWidth(),sprite.getHeight());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.