- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个按钮可以在屏幕上随机生成 4 个 TreeMap 像(我称之为徽章)。现在我想让这些徽章变得可触摸。如果我触摸它,它就会打印出一条消息。目前代码如下所示。
public class PlayScreen implements Screen {
int scWidth, scHeight;
int playerWidth, playerHeight;
Stage stage;
Skin skin;
BitmapFont font;
private SpriteBatch batch; // This is in the render.
ArrayList<Badge> badges;
Iterator<Badge> badgeIterator;
int badgeWidth = 160;
int badgeHeight = 300;
Game game;
public PlayScreen(Game game){
this.game = game;
}
// Create a button to randomise tree positions
TextureAtlas buttonAtlas;
TextButton.TextButtonStyle buttonStyle;
TextButton playButton;
int randomBadgePosition(String choice)
{
int min, max, range;
if (choice == "x") {
min = scWidth / 2 - scHeight / 2;
max = scWidth / 2 + scHeight / 2 - badgeWidth;
range = (max - min) + 1;
return (int) (Math.random() * range) + min;
} else{
min = 0;
max = scHeight - badgeHeight;
range = (max - min) + 1;
return (int)(Math.random() * range) + min;
}
}
@Override
public void show() {
batch = new SpriteBatch();
stage = new Stage();
scWidth = Gdx.graphics.getWidth();
scHeight = Gdx.graphics.getHeight();
font = new BitmapFont();
font.setColor(Color.BLACK);
font.getData().setScale(5);
int numBadges = 4;
badges = new ArrayList<Badge>();
for (int i = 0; i < numBadges; i ++){
badges.add(new Badge(new Vector2(-400, -400),new Vector2(badgeWidth, badgeHeight) ));
}
skin = new Skin();
buttonAtlas = new TextureAtlas("buttons/button1.txt");
skin.addRegions(buttonAtlas); // You need to do that.
buttonStyle = new TextButton.TextButtonStyle();
buttonStyle.up = skin.getDrawable("button");
buttonStyle.over = skin.getDrawable("buttonpressed"); // over is not necessary for android.
buttonStyle.down = skin.getDrawable("buttonpressed");
buttonStyle.font = font;
playButton = new TextButton("play", buttonStyle);
playButton.setPosition(50, 20);
stage.addActor(playButton);
Gdx.input.setInputProcessor(stage);
// Input listener for buttons.
playButton.addListener(new InputListener(){
@Override
public boolean touchDown (InputEvent event, float x, float y,
int pointer, int button){
// Randomise the locations of the badges.
for (int i = 0; i < badges.size(); i++){
badges.get(i).setPosition(new Vector2(randomBadgePosition("x"), randomBadgePosition("y")));
}
return true;
}
});
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
batch.begin();
badgeIterator = badges.iterator();
// This is how to change the position of the badge.
badges.get(1).setPosition(new Vector2(scWidth/2, scHeight/2));
while(badgeIterator.hasNext()){
Badge cur = badgeIterator.next();
cur.draw(batch);
}
batch.end();
player.update(); // Update the bound
}
}
徽章类是:
public class Badge {
Vector2 position, size;
Texture badge;
Rectangle bounds;
public Badge(Vector2 position, Vector2 size){
this.position = position;
this.size = size;
bounds = new Rectangle(position.x, position.y, size.x, size.y);
badge = new Texture(Gdx.files.internal("tree.png"));
}
public void update(){
bounds.set(position.x, position.y, size.x, size.y);
}
public void draw(SpriteBatch batch){
batch.draw(badge, position.x, position.y, size.x, size.y);
}
public Vector2 getPosition() {
return position;
}
public void setPosition(Vector2 position) {
this.position = position;
}
public Vector2 getSize() {
return size;
}
public void setSize(Vector2 size) {
this.size = size;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
}
我想我需要将它们放在舞台上然后添加监听器。虽然我知道 addlistener 部分(就像按钮一样)。我不知道如何修改 Badge 类并以不同的方式构造它,以便将其添加到舞台中。请帮忙。
最佳答案
您正在使用Scene2D,因此您可以使用Image类,而不是在 Badge
类中创建您自己的实现。当然,如果您需要一些Image无法提供的功能,您可以扩展此功能。
Image
类本身继承自 Actor您可以将其添加到舞台,也可以添加监听器,这正是您想要做的。
如果您只想处理单击图像上的 ClickListener最好 - 如果您需要处理更多事件,您可以添加 EventListener .
示例代码可能如下所示
Stage stage;
//...creating stage etc...
Image badge = new Image(new Texture(Gdx.files.internal("tree.png"))); //Remember that you need to dispose Texture like this!
badge.addListener( new ClickListener() {
@Override
public void clicked(InputEvent event, float x, float y) {
System.out.println("Clicked!");
}
});
stage.addActor(badge);
另请注意,您需要使用以下命令将 stage
设置为输入处理器
Gdx.input.setInputProcessor(stage);
关于java - libgdx,如何使我的图像可触摸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37679492/
我该怎么做 touch R 中的文件(即更新其修改时间而不更改其内容)?我正在寻找一个跨平台的内置(或打包)等效于: system2("touch", file_name) 最佳答案 我找到了 an
我想在 android 中实现 body 部位选择。我的要求是,当我点击特定部分时,图像应用程序应该能够识别 body 部位并且所选部分的颜色应该改变。附上示例图像以供引用。 如有任何想法或建议,我们
我需要将3D模型加载到我的应用程序中(不是游戏,它没有任何区别),并检测用户何时触摸此模型的特定部分以采取不同的操作。 我怎样才能做到这一点?可能吗? 最佳答案 我不熟悉Rajawali,但GitHu
似乎连续触摸总是被它起源的控件捕获。是否有标准方法将触摸/手势传递给当前触摸结束的控件? 最佳答案 控件显示在 UIView 对象内部的屏幕上。 iOS 使用第一响应者的概念来处理 UIView 对象
我有一个 UIScrollView 实例,允许用户放大/缩小。我已经根据文档实现了一个委托(delegate)来处理这个问题。但是,我想知道用户触摸 ScrollView 的位置(相对于 Scroll
我正在创建一款射击游戏,您可以触摸屏幕,玩家可以射击。我遇到的问题是,当你触摸屏幕并按住它并拖动它时,它会快速射击。处理这个问题的最佳方法是什么? 我希望玩家能够按住手指并以稳定的速度射击,而手指向上
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typ
这是我从触摸事件中获得的返回数据。明显缺少任何有用的触摸 X/Y 坐标 ( https://developer.mozilla.org/en-US/docs/Web/API/TouchEvent/ch
因此,我有一个屏幕,其中包含 0 到 6 个通过 XML 包含的相同用户界面组件。类似这样的东西: ...等等 每个包含的 UI 都是我重复使用的几个小部件和自定义组件的集
我希望能够更改文件的修改日期以便在 Web 应用程序中使用。目前我直接在命令行上测试这个。在我的 Mac 上工作正常,但是当我在 Linux 服务器上执行此操作时出现错误。 命令:/bin/touch
概念问题: 我在使用 touches 时遇到了一个非常简单的问题属性,自动更新依赖模型的时间戳;它正确地这样做了,但也应用了全局范围。 有什么办法可以关闭这个功能吗?或专门询问自动 touches忽略
我很难在一个简单的等距 Sprite Kit 游戏中实现点击处理。 我有一个包含多个 Tile 对象(SKSpriteNode)的 map (SKNode)的游戏场景(SKScene)。 这是 map
有没有办法在触摸事件后恢复 Flexslider 幻灯片的自动播放?现在看来,一旦我滑动导航,幻灯片就会停止...... 最佳答案 FLEXISLIDER 2 更新 resume()事件不再存在,正确
有些游戏有一些小图片作为 Sprite ,可以通过触摸来移动。如果 Sprite 是较大的图片,触摸是很正常的。我们可以使用函数CGRectContainsPoint检查 Sprite 。但是当 Sp
我有一个 View 覆盖到 iPhone 的相机 View 上。在此 View 中,我有一些 uibuttons,它们显示的位置取决于加速度计/指南针的值。当用户在 View 内触摸但没有在 uibu
我有一个 UIView 和一个 UIWebView。 UIWebView 是 UIView 的 subview 。 UIWebView 包含 YouTube 视频,并设置为让视频适合 UIWebVie
我是通过 XCode 使用 SDK 版本 5.0 进行 iOS 开发的新手。在我的应用程序中,我需要在按下按钮时更改按钮的标题。 假设在正常状态下它是“未推送”的。我所需要的是,当我按下按钮时,按钮标
我需要防止我的网络应用程序中的水平滚动。我尝试在 touchmove 事件上使用 PreventDefault(),但它也阻止垂直滚动。 有什么想法吗?^^ 我的尝试: $(document)
对于完全适合动画组方法的动画效果,如Brad Larson's answer here所示,我需要动画根据输入进行。特别是触摸和检测到的触摸的位置。处理 TouchMoved: 并为每次触摸设置元素的
我在屏幕上散布了一系列硬币。我想在触摸硬币时添加声音。 private Music coinSound; 在 show() 中: coinSound=assetManager.get(Assets.c
我是一名优秀的程序员,十分优秀!