我目前正面临一个问题,即我试图通过touchdown
移动我的演员,我感到困惑并做了一些错误。我试图通过拖动演员来向左和向右移动我的桶。
这是我的代码
包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;
公共类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
}
}
和我的演员类
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;
类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);
}
}
我需要通过使用舞台,演员来拖动它来明确指示如何移动铲斗..谢谢
答案 0 :(得分:0)
@ Tenfour04是对的 代替
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
应该使用:
setBounds(getX(),getY(),sprite.getWidth(),sprite.getHeight());