使用Graphics.DrawString
将一组(相当静态的)文本渲染到屏幕外位图,将其转换为Texture2D
一次,然后简单地调用SpriteBatch.Draw
,而不是使用内容管道并使用渲染文本,是否有任何缺点SpriteFont
?这基本上是一页文本,绘制成"纸张",但用户也可以选择调整字体大小,这意味着我必须包含不同大小的spritefonts.
由于这是一个仅限Windows的应用程序(不打算移植它),我可以访问所有字体,例如普通的WinForms应用程序,我相信使用Graphics.DrawString
(甚至TextRenderer
)使用精灵字体时渲染质量会好得多.
此外,似乎性能可能更好,因为SpriteBatch.DrawString
需要在每次迭代中"渲染"整个文本(即分别为每个字母发送顶点),而使用位图我只做一次,所以它应该稍微减少工作量CPU方面.
[更新]
从技术方面来看,它似乎比通过精灵字体更好地完成文本渲染.如果它们是水平渲染的,我甚至会得到ClearType.可能存在的一个问题是字体的spritesheets(可能是?)在纹理内存方面比为文本页面创建单独的纹理更有效.
它说
"方法必须具有返回类型"
每当我尝试调试它.
我不知道怎么修这门课
这是ac#编码的2d游戏的玩家类
public class player
{
public float moveSpeed;
public Vector2 position;
public Texture2D texture;
//default constructer
public Player(Texture2D tex, Vector2 startPos)
{
position = startPos;
texture = tex;
moveSpeed = 5.0f;
}
public void Update(GameTime gameTime)
{
//------------------------------------------
//check for keyboard input(keyboard IF statements)
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, position, Color.White);
}
}
Run Code Online (Sandbox Code Playgroud) 当我试图运行桌面独立版本时,我在LibGdx项目中得到UnsatisfiedLinkError.请注意,如果我只是直接运行桌面版本而不构建一个独立的jar,只有我先构建一个桌面jar,然后尝试运行它,才会出现此问题.有趣的是,直接运行桌面版本总体上要容易得多,而且我甚至没有接近运行独立的桌面jar,直到我搜索了一大堆jar文件,并手动添加它们到LibGdx项目.
我在Windows 10 x64上使用Android Studio 3.3,gradle 4.10和Java 1.8.0_201,并使用'gradle'命令将我的LibGdx应用程序版本部署到桌面:
gradle desktop:dist
来自ProjectRoot.
然后在运行由此创建的jar文件时,通过从ProjectRoot键入命令行:
cd desktop/build/libs
, 然后 java -jar desktop-1.0.jar
这样做我得到了 UnsatisfiedLinkError error
我尝试将jar文件复制到Linux并在那里进行测试,我也遇到了同样的错误.
我做了一些搜索,我发现一个建议可能是调用new SpriteBatch()
过早发生了:
就像在create()
核心模块中的函数之前一样,但这不是问题,因为new SpriteBatch()
调用从来没有比这更早.
此链接使用的建议LibGdx Setup Generator
也没有帮助我,因为我已经使用该LibGdx Setup Generator
GUI工具来创建同一个项目,我仍然遇到这个问题.
我注意到,虽然从程序的输出中,它在new SpriteBatch()
调用时抛出了UnsatifisedLinkError异常,因为该链接确实引用了,但它是从create()
函数调用的,而不是早于该函数.
我做了一个实验,发现如果我new SpriteBatch()
完全删除了调用以及依赖于该调用的所有其他代码,例如创建纹理,并使用精灵批处理来绘制该纹理.我发现通过这样做,程序运行没有得到错误,当然我也无法绘制任何东西.
所以,然后我做了另一个实验,并按照以下方式将new SpriteBatch()
调用从create函数移到render()
函数中,正如您将在代码中注意到的那样.我只是尝试在每次执行render()函数时创建SpriteBatch(),如果尚未创建它,并输出它抛出的次数UnsatisfiedLinkError exception
------------------------- MyGdxGame.java ---------------------- ----------------
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class …
Run Code Online (Sandbox Code Playgroud) desktop-application gradle libgdx unsatisfiedlinkerror spritebatch
我通常SpriteBatch
在XNA/Monogame中使用2D游戏,并且最近刚刚研究DrawUserIndexedPrimatives
过诸如此类的3D绘图方法.我正在开展一个项目,我们的动画师希望能够剪切精灵和纹理.
有了SpriteBatch
你可以在一个矩阵传递SpriteBatch
开始剪切的对象.就像是:
//translate object to origin
Matrix translate1 = Matrix.CreateTranslation(-rectangle.X, -rectangle.Y, 0);
//skew the sprite 33 degrees on the X and Y axis
Matrix skew = Matrix.Identity;
skew.M12 = (float)Math.Tan(33 * 0.0174532925f);
skew.M21 = (float)Math.Tan(33 * 0.0174532925f);
//translate object back
Matrix translate2 = Matrix.CreateTranslation(rectangle.X, rectangle.Y, 0);
Matrix transform = translate1 * skew * translate2;
_spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied,
SamplerState.PointWrap, DepthStencilState.Default,
RasterizerState.CullCounterClockwise, null, transform);
_spriteBatch.Draw(_texture, rectangle, Color.White);
_spriteBatch.End();
Run Code Online (Sandbox Code Playgroud)
明显的缺点是它需要你SpriteBatch
为每个剪切的精灵进行一个新的开始和结束调用.我们目前只需要2个电话即可SpriteBatch
开始游戏.一个用于UI,一个用于世界的东西.我们的艺术家想要使用剪切来做摇晃的树木或动物的腿和四肢的动物,所以如果我们给他们选择,我可以看到这个数字跳到10多个不同的批次.
平均水平有大约250个元素,每个元素包含10-20个精灵.
我已经为Android编写了一个测试,调用1000个精灵.没有任何偏斜,它可以在大约11秒或大约53fps中绘制所有1000,600次.但是,如果我倾斜每十个精灵(增加100个新的 …
虽然我确实掌握了OpenGL的基本知识,但我只是从libgdx开始.
我的问题是:为什么,拥有完全相同的代码,但只从OrthographicCamera切换到PerspectiveCamera有不再显示我的任何SpriteBatches的效果?
这是我使用的代码:
create()方法:
public void create() {
textureMesh = new Texture(Gdx.files.internal("data/texMeshTest.png"));
textureSpriteBatch = new Texture(Gdx.files.internal("data/texSpriteBatchTest.png"));
squareMesh = new Mesh(true, 4, 4,
new VertexAttribute(Usage.Position, 3, "a_position")
,new VertexAttribute(Usage.TextureCoordinates, 2, "a_texCoords")
);
squareMesh.setVertices(new float[] {
squareXInitial, squareYInitial, squareZInitial, 0,1, //lower left
squareXInitial+squareSize, squareYInitial, squareZInitial, 1,1, //lower right
squareXInitial, squareYInitial+squareSize, squareZInitial, 0,0, //upper left
squareXInitial+squareSize, squareYInitial+squareSize, squareZInitial,1,0}); //upper right
squareMesh.setIndices(new short[] { 0, 1, 2, 3});
spriteBatch = new SpriteBatch();
}
Run Code Online (Sandbox Code Playgroud)
和render()方法:
public void render() {
GLCommon gl = Gdx.gl;
camera.update();
camera.apply(Gdx.gl10); …
Run Code Online (Sandbox Code Playgroud) 所以我启动了我当前的项目,我做的第一件事是运行它,它给了我上面的例外.昨晚运行得很好.这是我的Draw事件中的所有代码.spriteBatch.Begin不会出现在项目的任何其他位置.在这里删除Begin会导致spriteBatch.Draw抛出异常,在begin之前放置spriteBatch.End也会引发异常.我不知道什么是错的,以及如何解决这个问题.
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(background, Vector2.Zero, Color.White);
player.Draw(spriteBatch);
level1.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
Run Code Online (Sandbox Code Playgroud) 我创建了一个AnimatedSprite
类,绘制一个特定的TextureRegion
.有时我需要一种色调效果,所以我设置(this.color是Color
我的一个字段AnimatedSprite
):
super.draw(batch, parentAlpha);
batch.setColor(this.color);
batch.draw(this.frames[this.currentFrame], x, y, originX, originY, width, height, scaleX, scaleY, rotation)
batch.setColor(Color.WHITE);
Run Code Online (Sandbox Code Playgroud)
但是,当我将AnimatedSprite
颜色设置为黑色或任何颜色时,其他所有颜色都会有颜色.我甚至尝试flush()
,结束批次并开始新的等等......但似乎没有任何工作.
请帮我正确应用色调效果.我会很感激任何想法.
在尝试将文本/对话框系统放入我的游戏项目时,我遇到了一个问题.当我创建一个字体并在其上调用draw方法传递相机更新的spriteBatch时,该字体的每个像素都具有一个sprite的相同维度.
我得到以下渲染:
您可以在图片上看到的是每个像素超大的"你好"的"h"的顶部.相同的相机用于渲染瓷砖/精灵.
我想要实现的效果类似于:
这是代码:
// 15 * 12 tile size
camera = new OrthographicCamera(Const.VIEWPORT_WIDTH, Const.VIEWPORT_HEIGHT);
BitmapFont font = new BitmapFont(Gdx.files.internal("data/fonts/myfont.fnt"));
// ....
// p => player position
camera.position.x = p.getX();
camera.position.y = p.getY();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
font.draw(batch, "hello", p.getX(), p.getY());
batch.end();
Run Code Online (Sandbox Code Playgroud)
我尝试过使用但font.setScale()
没有成功.
有人知道如何实现这一目标吗?
我使用的是libgdx 0.9.9版.我想在3D空间中使用ParticleEffect以及其他3D模型渲染火焰效果.
我的代码的逻辑流程:
问题:火焰效果在3D空间中的某个点上呈现正常.但是当我旋转相机以使3D模型位于相机和火焰效果之间时,火焰效果会渲染3D模型,而不是隐藏在3D模型后面.
我尝试过的事情:
有没有人遇到类似的问题?任何建议的解决方法都可以使ParticleEffect成为3D世界的一部分,以便在被其他3D模型阻挡时隐藏它?我看过Xoppa在youtube上发布了关于libgdx中3D粒子的视频,但没有提到步骤/解决方案.任何帮助将受到高度赞赏.
我一直在使用MonoGame将我在XNA中制作的滚动射击游戏移植到Linux上.几乎所有事情都进展顺利,但我在一个特定的地方遇到问题,调用SpriteBatch.Draw()会削弱帧速率.大多数游戏运行良好,没有任何打嗝.它同时吸引了大量的敌人和大量的子弹而没有任何减速.但是,导致丢帧的部分是分层滚动背景.代码的相关部分在这里.
Level.cs:
public void Draw(SpriteBatch spriteBatch)
{
foreach (ScrollingBackgroundLayer sbl in scrollingBackground)
{
sbl.Draw(spriteBatch);
}
}
Run Code Online (Sandbox Code Playgroud)
上面的"scrollingBackground"是ScrollingBackgroundLayers的列表,其相关部分是:
ScrollingBackgroundLayers.cs:
Vector2[] scrollingBackgroundImages;
public float DrawLayer;
public ScrollingBackgroundLayer(GameScene newScene, Texture2D texture, float scrollSpeed, Color color)
{
layerColor = color;
layerSpeed = scrollSpeed;
layerTexture = texture;
thisScene = newScene;
Initialize();
}
public void Initialize()
{
scrollingBackgroundImages = new Vector2[2];
for (int i = 0; i < 2; i++)
{
scrollingBackgroundImages[i] = new Vector2(0, thisScene.ScreenArea.Height - (layerTexture.Height * i));
}
}
public void Update(GameTime gameTime) …
Run Code Online (Sandbox Code Playgroud) spritebatch ×10
libgdx ×5
java ×3
monogame ×3
xna ×3
3d ×2
c# ×2
draw ×1
drawing ×1
drawstring ×1
exception ×1
fonts ×1
frame-rate ×1
gradle ×1
methods ×1
opengl ×1
scale ×1
sprite ×1
spritefont ×1
tint ×1