繁体   English   中英

需要有关在libgdx舞台上移动演员的一些指南

[英]NEED some guides on moving actors on stage libgdx

我目前遇到一个问题,我试图通过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);
}

}

我需要有关如何使用舞台,演员拖动铲斗来移动铲斗的明确说明。谢谢

@ Tenfour04是正确的
代替

setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());

应该使用:

setBounds(getX(),getY(),sprite.getWidth(),sprite.getHeight());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM