i am currently facing a problem where i try to move my actor by touchdown
and i am confuse and did some error. I am trying to move my bucket to the left and right by dragging the actor. This is my code package 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 {
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")));
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
instead
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
should use:
setBounds(getX(),getY(),sprite.getWidth(),sprite.getHeight());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.