我已经花了大约 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。所以也不应该是这样......
最佳答案
您的问题出在 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() 都返回零(因为您还没有为父类(super class)对象设置任何宽度/高度)。然后在 scaleX/scaleY 变量中你有 infinity 值——你可以通过在我的代码中取消注释标记行来看到它。
有趣的是,Java 允许您除以浮点零 - 返回提到的 infinity 值 ( read more here ),这就是为什么您在那里没有异常。
解决方案 是通过调用 super.setSize(width,height) 来修复此方法,或者完全删除它并依赖默认的 Sprite 类 setSize() 方法实现。
还有一件事 - 解决问题后,您会发现旋转问题 - 它与原点有关。您可以将 setSize 方法更改为
@Override
public void setSize(float w, float h)
{
super.setSize(w,h);
setOrigin(w/2f, h/2f);
}
它会做的事
关于android - LibGDX - 来自自定义类的 Sprite 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32654526/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
所以我在关注Railscast,我注意到在html.erb文件中,ruby代码有一个微弱的背景高亮效果,以区别于其他代码HTML文档。我知道Ryan使用TextMate。我正在使用SublimeText3。我怎样才能达到同样的效果?谢谢! 最佳答案 为SublimeText安装ERB包。假设您安装了SublimeText包管理器*,只需点击cmd+shift+P即可获得命令菜单,然后键入installpackage并选择PackageControl:InstallPackage获取包管理器菜单。在该菜单中,键入ERB并在看到包时选择
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin