Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android LibGDX-自定义类中的精灵未显示_Android_Libgdx_Rendering_Sprite - Fatal编程技术网

Android LibGDX-自定义类中的精灵未显示

Android LibGDX-自定义类中的精灵未显示,android,libgdx,rendering,sprite,Android,Libgdx,Rendering,Sprite,我已经想了6个小时了。。。 我有两个类(Player和UiButton),它们都扩展了Sprite。 问题是播放器中的精灵正在渲染,而ui按钮中的精灵不在渲染中 在这里,看一下代码: Player.java(简化) UiButton.java import com.badlogic.gdx.Gdx; import com.badlogic.gdx.graphics.Texture; import com.badlogic.gdx.graphics.g2d.Sprite; public clas

我已经想了6个小时了。。。 我有两个类(
Player
UiButton
),它们都扩展了Sprite。 问题是播放器中的精灵正在渲染,而ui按钮中的精灵不在渲染中

在这里,看一下代码:

Player.java(简化)

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;
    }

}
导入com.badlogic.gdx.gdx;
导入com.badlogic.gdx.graphics.Texture;
导入com.badlogic.gdx.graphics.g2d.Sprite;
公共类UiButton扩展了Sprite
{
公共UiButton(纹理纹理、浮动posX、浮动posY、浮动旋转)
{
超级(新纹理(Gdx.files.internal(“ui/arrow.png”));
设置位置(posX、posY);
设置旋转(旋转);
}
公用UiButton(UiButton按钮)
{
超级();
}
公共空间设置大小(浮动到宽度,浮动到高度)
{
float-scaleX=toWidth/super.getWidth();
float scaleY=toHeight/super.getHeight();
设置刻度(scaleX、scaleY);
}
公共布尔值已打开(int-touchX,int-touch)
{

如果(touchX>=getX()&&touchX=getY()&&touchY您的问题在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允许您通过浮点零除法返回无穷值(),这就是为什么您在这里没有异常

解决方案是通过调用super.setSize(width,height)来修复此方法,或者干脆将其删除,然后依靠默认的Sprite类setSize()方法实现


还有一件事-在解决问题后,您将观察到旋转问题-它与原点有关。您只需将设置大小方法更改为

    @Override
    public void setSize(float w, float h)
    {
        super.setSize(w,h);

        setOrigin(w/2f, h/2f);
    }
它会成功的


你能发布你的
arrow.png
图像吗?首先,我会将你的按钮定位在你播放器的同一位置,即
arrow.png
。我已经尝试设置与播放器相同的位置…尝试取消旋转或设置为
setOriginCenter()
在旋转之前也旋转箭头-仍然不工作:/It正在使用与您添加的代码完全相同的代码工作(没有播放器-我已经对这些部分进行了注释)。位置有一些问题,但通常呈现箭头。请显示完整的uiButton类。谢谢,您节省了我的时间:D似乎我意外地覆盖了setSize()方法…再次感谢您的关注!哦,还有…90度和270度是libGDX中的倒位吗?因为90度应该旋转箭头,使其指向右侧:/
    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 );
    }
    @Override
    public void setSize(float w, float h)
    {
        super.setSize(w,h);

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