2016-08-21 4 views
0

私は現在、私が俳優をtouchdownで動かそうとしているときに問題に直面しています。私は混乱して、何らかのエラーを出しました。私は俳優をドラッグしてバケツを左右に動こうとしています。 これは私のコードです パッケージcom.tcyzzz.savethesemester;ステージで動く俳優に関するいくつかのガイドlibgdx

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は俳優{ スプライトスプライト=新しいスプライト(新しいテクスチャを拡張します(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 @おかげ

+0

スプライトの位置と俳優の位置を混在させています。実際には、Spriteの代わりにTextureRegionを使用して、複数の場所での位置や色などの追跡の冗長性(およびバグの可能性)を避ける必要があります。 – Tenfour04

答えて

0

...俳優、ステージを用い、それをドラッグしてバケットを移動する方法についての明確な指示を必要とする権利である
代わり

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

を使用する必要があります。

setBounds(getX(),getY(),sprite.getWidth(),sprite.getHeight()); 
関連する問題