在libgdx游戏中
我想触摸然后然后拖动某个地方,然后在释放(touchUp)上根据目标身体的距离和方向施加方向力.触地时,目标体保持静止,然后在触摸时沿着所需的轨迹施加力.
(非常类似于愤怒的小鸟 - 当你拿着弹弓的时候,你可以看到目标身体的虚线轨迹 - 我想做同样的事情)
所以我想这可能不是最困难的事情但是给了一些选项我倾向于使用MouseJointDef但它立即施加力(即目标立即移动 - 我希望它保持静止然后一旦触摸事件发生然后施加力量)
什么是绘制轨迹的最简单方法?我也使用Box2D.
我是Libgdx的新手,我写了一个扩展Game类的类,事实是来自Game的setScreen()方法没有交换屏幕,因为在我设置屏幕之后我的游戏仍然只渲染渲染方法中的内容从游戏类而不是屏幕类的渲染方法中的内容.这是代码:
如果运行此代码,即使我在用户触摸(点击)屏幕时更改屏幕,我也只会看到红色屏幕
class myGame extends Game
{
GameScreen myOtherScreen;
public void create()
{
//create other screen
myMenuScreen = new GameScreen();
}
public void render(float delta)
{
// change screens if screen touched
if(Gdx.input.justTouched())
setScreen(myOtherScreen);
//render red screen
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
.
. //other methods
.
}
// ======= Screen Class ========
public class GameScreen implements Screen
{
@Override
public void render(float delta)
{
//render green screen
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
.
. //other methods
. …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用表来创建带有排列的按钮和标签的菜单.我正在使用最新的每日构建的libgdx,它改变了Table API(以及其他内容).
这是我的Screen构造函数中的代码:
this.mStage = new Stage();
this.mTable = new Table();
this.mTable.setFillParent(true);
this.mTable.debugTable();
this.mStage.addActor(this.mTable);
// now I add some stuff to the table
// ...
Run Code Online (Sandbox Code Playgroud)
我的resize功能看起来像这样:
this.mStage.setViewport(width, height, true);
this.mTable.setFillParent(true);
this.mTable.invalidate();
Run Code Online (Sandbox Code Playgroud)
我的render功能如下:
System.out.println("Table size is " +
this.mTable.getWidth() + " by " +
this.mTable.getHeight());
this.mStage.draw();
Table.drawDebug();
Run Code Online (Sandbox Code Playgroud)
虽然我的控制台显示正确的大小:
Table size is 480.0 by 640.0
Run Code Online (Sandbox Code Playgroud)
表大小围绕其子项收缩包装,并且不会扩展到480x640的正确大小.
有任何想法吗?
我正在尝试使用Nine Patch作为Libgdx Scene2d UI按钮的背景.这是装载,但它真的很难看.我可以看到"元数据"像素,它被拉伸就好像它只是一个普通的图像(按钮上的文字是"继续"):

我正在通过(libgdx)将.9.png文件直接加载到NinePatchDrawable(libgdx)中,NinePatch如下所示:
this.dialogButtonUp = new NinePatchDrawable(
new NinePatch(new Texture(Gdx.files.internal("data/button-round.9.png"))));
this.dialogButtonDown = new NinePatchDrawable(
new NinePatch(new Texture(Gdx.files.internal("data/button-round-down.9.png"))));
Run Code Online (Sandbox Code Playgroud)
然后我创建一个TextButtonStyle描述按钮,并引用两个NinePatchdrawable:
TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.font = aValidFontReally;
buttonStyle.fontColor = Color.BLACK;
buttonStyle.up = this.dialogButtonUp;
buttonStyle.down = this.dialogButtonDown;
buttonStyle.pressedOffsetX = -2;
Run Code Online (Sandbox Code Playgroud)
我通过一个Dialog盒子间接构建按钮:
new Dialog( ... ).button("Continue", null, buttonStyle);
Run Code Online (Sandbox Code Playgroud)
我检查了.9.png文件以确保:
draw9patch工具可以加载图像并验证它们关于检查或更改内容的任何其他建议?
为什么我的TextButtonlibgdx没有响应点击?
我有一个按钮,这个按钮有一个监听器,但它没有响应.该按钮正在显示,但它不会响应鼠标点击.
public MyStage extends Stage {
...
next.addListener(new InputListener() {
@Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
{
Gdx.app.log(ApothecaryGame.LOG, "Pressed: Next button.");
return true;
}
@Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
Gdx.app.log( ApothecaryGame.LOG, "Released: Next button.");
super.touchUp( event, x, y, pointer, button );
nextPage();
}
} );
this.addActor(next);
}
Run Code Online (Sandbox Code Playgroud) 所以我有一个像这样的sceneu2.ui表:
+--------------------------------------+
|+---------+ +------------+ +---------+|
|| Table 1 | | ScrollPane | | Table 2 ||
|+---------+ +------------+ +---------+|
+--------------------------------------+
| My Actor |
+--------------------------------------+
Run Code Online (Sandbox Code Playgroud)
里面scrollpane是另一个table.外壳也是一个table.Table 1,Table 2并且table里面scrollpane有相同数量的行.
My Actor 是简单的图像按钮actor,也支持鼠标悬停.
问题是,My Actor最初没有出现(但是它的边界存在 - 由表调试行检查)但是当我滚动某些东西scrollpane(用鼠标)时它会出现2秒钟,然后开始逐渐消失.(预期结果应始终显示)
我究竟做错了什么?
本My Actor类:
public class GameButton extends Actor {
public static abstract class Listener {
public abstract void onClick();
}
final Texture[] current;
final Listener …Run Code Online (Sandbox Code Playgroud) 我有这段代码:
Image myImage = new Image(new Texture("data/file.png"));
Run Code Online (Sandbox Code Playgroud)
如何myImage在屏幕上获得位置?我试过myImage.getX(),myImage.getImageX()两者总是返回0.0.怎么了?
我一直在谷歌搜索有关如何使用Scene2D使用LibGDX的多个屏幕的教程.到目前为止,这是我在场景处理课中的内容,但我不知道从哪里开始.我知道我必须对构造函数做一些事情,MainMenu.java但我不知道它是什么.
到目前为止我得到了什么:
public class ScreenHandler extends Game{
public MainMenu Main;
@Override
public void create() {
Main= new MainMenu();
setScreen(Main);
}
}
Run Code Online (Sandbox Code Playgroud) 我目前正在使用LibGDX和Java.我想知道,为什么LibGDX只为你提供游戏逻辑和渲染的渲染Mehtod.为什么LibGDX这样做,为什么LibGDX不继承像更新这样的另一个方法?我应该在渲染方法中处理游戏逻辑和渲染(当然我在渲染和更新方法中将其拆分)
我正在使用动画类来制作一个只有2帧的简单动画.通过动画,我可以获得当前时间的索引或纹理区域.
if (!animation.isAnimationFinished(time))
time += Gdx.graphics.getDeltaTime();
else
time = 0;
Run Code Online (Sandbox Code Playgroud)
但问题是,我无法加载任何纹理,它的宽度和高度必须是2的幂,所以我有一个大的1024x1024纹理,有2个图像.使用sprite.setTexture()我只能放置一个纹理,但animation.getframe会返回一个纹理区域.
有没有办法用动画改变精灵纹理?
也
sprite.setTexture(animation.getKeyFrame(time).getTexture());
Run Code Online (Sandbox Code Playgroud)
不行.
这有效
sprite = new Sprite(animation.getKeyFrame(time));
Run Code Online (Sandbox Code Playgroud)
但我不认为这是最好的主意......我认为很慢可以制造错误.