【发布时间】:2015-09-18 14:29:15
【问题描述】:
我已经尝试了大约 6 个小时了...
我有 2 个类(Player 和 UiButton),它们都是 extends Sprite。
问题是 Player 中的 Sprite 正在渲染,而 UiButton 中的 Sprite 没有强>。
这里,看一下代码:
Player.java(简化)
public class Player extends Sprite
{
public Player( float posX, float posY )
{
super( new Texture( Gdx.files.internal( "character/char_idle.png" ) ) );
super.setPosition( posX, posY );
}
}
UiButton.java
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class UiButton extends Sprite
{
public UiButton( Texture texture, float posX, float posY, float rotation )
{
super( new Texture( Gdx.files.internal( "ui/arrow.png" ) ) );
setPosition( posX, posY );
setRotation( rotation );
}
public UiButton( UiButton button )
{
super( );
}
public void setSize( float toWidth, float toHeight )
{
float scaleX = toWidth / super.getWidth( );
float scaleY = toHeight / super.getHeight( );
setScale( scaleX, scaleY );
}
public boolean isTouched( int touchX, int touchY )
{
if( touchX >= getX( ) && touchX <= getX( ) + getWidth( ) )
if( touchY >= getY( ) && touchY <= getY( ) + getHeight( ) )
return true;
return false;
}
}
GameEngine.java(不是整个文件,而是所需的函数和变量)
public class GameEngine extends ApplicationAdapter implements ApplicationListener
{
private SpriteBatch spriteBatch;
private Player player;
private Sprite grid;
private UiButton arrow_right;
private UiButton arrow_left;
private UiButton arrow_down;
private UiButton arrow_up;
@Override public void create( )
{
/** Setting InputProcessor **/
InitializeInputProcessor( );
/** Initializing SpriteBatch **/
spriteBatch = new SpriteBatch( );
spriteBatch.maxSpritesInBatch = 10;
/** Creating UI **/
GenerateUiArrows( 10, Gdx.graphics.getHeight( ) / 4 );
/** Creating Player **/
player = new Player( 100, 100 );
/** Creating background grid **/
grid = new Sprite( new Texture( generateGrid( 50 ) ) );
}
@Override public void dispose( )
{
spriteBatch.dispose( );
}
public void doUpdating( )
{
player.update( );
}
public void doRendering( )
{
/** Clearing the screen **/
Gdx.gl.glClearColor( 1, 1, 1, 1 );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
/** Beginning SpriteBatch rendering **/
spriteBatch.begin( );
/** Drawing background grid **/
grid.draw( spriteBatch );
/** Drawing Player **/
player.draw( spriteBatch );
/** Drawing UI **/
arrow_right.draw( spriteBatch );
arrow_left.draw( spriteBatch );
arrow_down.draw( spriteBatch );
arrow_up.draw( spriteBatch );
/** Ending SpriteBatch rendering **/
spriteBatch.end( );
}
@Override public void render( )
{
doUpdating( );
doRendering( );
/******************************************/
/** Here was a loop to control framerate **/
/******************************************/
}
private Pixmap generateGrid( int interval )
{
/** Creating temporary Pixmap **/
Pixmap tempMap = new Pixmap( Gdx.graphics.getWidth( ), Gdx.graphics.getHeight( ), Pixmap.Format.RGBA8888 );
/** Setting lines color to BLACK **/
tempMap.setColor( Color.BLACK );
/** Creating horizontal lines **/
for( int i = interval; i < Gdx.graphics.getWidth( ); i += interval )
tempMap.drawLine( i, 0, i, Gdx.graphics.getHeight( ) );
/** Creating vertical lines **/
for( int i = interval; i < Gdx.graphics.getHeight( ); i += interval )
tempMap.drawLine( 0, i, Gdx.graphics.getWidth( ), i );
/** Returning the result **/
return tempMap;
}
private void GenerateUiArrows( float padding, float button_size )
{
arrow_right = new UiButton( new Texture( Gdx.files.internal( "ui/arrow.png" ) ), button_size * 2 + padding * 3, padding, 90 );
arrow_right.setSize( button_size, button_size );
arrow_left = new UiButton( new Texture( Gdx.files.internal( "ui/arrow.png" ) ), padding, padding, 270 );
arrow_left.setSize( button_size, button_size );
arrow_down = new UiButton( new Texture( Gdx.files.internal( "ui/arrow.png" ) ), button_size + padding * 2, padding, 180 );
arrow_down.setSize( button_size, button_size );
arrow_up = new UiButton( new Texture( Gdx.files.internal( "ui/arrow.png" ) ), button_size + padding * 2, button_size + padding * 2, 0 );
arrow_up.setSize( button_size, button_size );
}
}
不显示错误或警告。只是 UiButtons 没有被渲染。我还没有设置 OrthographicCamera。所以也不应该是这样……
【问题讨论】:
-
你能发布你的
arrow.png图像吗?我先把你的按钮放在播放器的相同位置 -
i.imgur.com/VBW99je.png -
arrow.png。而且我已经尝试设置与玩家相同的位置... -
尝试删除旋转或旋转前
setOriginCenter() -
也试过了——还是不行:/
-
它使用与您添加的完全相同的代码工作(没有播放器 - 我已经评论了那部分)。位置存在一些问题,但通常会呈现箭头。请显示完整的 uiButton 类。
标签: android libgdx rendering sprite