簡體   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