android+libgdx+教程,android-LibGDX-自定义类的精灵未显示

我已经尝试了6个小时,弄清楚了我们的这个问题…

我有2个类(Player和UiButton),它们都扩展了Sprite.

问题在于播放器中的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 );

}

}

没有显示错误或警告.仅不显示UiButton.我尚未设置OrthographicCamera.所以也不应该这样…

解决方法:

您的问题出在UiButton类的setSize()方法中:

public void setSize( float toWidth, float toHeight )

{

//System.out.println("super.width = " + super.getWidth() + ", super.height = " + super.getHeight() ); - you can uncomment it to see the super width/height value in the console

float scaleX = toWidth / super.getWidth( );

float scaleY = toHeight / super.getHeight( );

setScale( scaleX, scaleY );

}

super.getWidth()和super.getHeight()都返回零(因为您没有为超类对象设置任何宽度/高度).然后在scaleX / scaleY变量中您具有无穷大值-您可以通过在我的代码中取消注释标记行来查看它.

有趣的是Java允许您除以浮点数零-返回提到的无穷大值(read more here),这就是为什么那里没有Exception的原因.

解决方法是通过调用super.setSize(width,height)修复此方法,或者仅删除它并依靠默认的Sprite类setSize()方法实现.

还有一件事-解决问题后,您将观察到旋转问题-这与原点有关.您可以将setSize方法更改为

@Override

public void setSize(float w, float h)

{

super.setSize(w,h);

setOrigin(w/2f, h/2f);

}

它会做的事情

84dd7caaff3a5b5a5c3066860c2e5e3d.png

标签:libgdx,sprite,rendering,android

来源: https://codeday.me/bug/20191119/2038245.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值