[LIBGDX学习经验]-Touchpad类,摇杆

这是旧版本了,更完整的传送门:http://www.cnblogs.com/cava/archive/2013/05/10/3072202.html

近日在网上搜索 关于LIBGDX摇杆的东西,发现国内没有现成的东西。

后来找 奋斗小土豆 哥,他分享了一下他自己实现的一个摇杆。

百度 LIBGDX摇杆 无果,GOOGLE 一样。

后来灵机一动,直接翻LIBGDX的官方文档。发现他官方有实现,喜不自胜,特拿来分享一下

com.badlogic.gdx.scenes.scene2d.ui.Touchpad;

Touchpad 类,就是一个摇杆类。总算找到你了。娃哈哈。代码实现是这样的。

Skin skin = new Skin(Gdx.files.internal("ui/uiskin.json"));
        
        Touchpad touchpad = new Touchpad(1f, skin);
        
        touchpad.setSize(300,300);
        
        touchpad.setPosition(20, 20);
        
        gameStage.addActor(touchpad);

这里我就卡在一个地方,skin皮肤。为了不让大伙走弯路,我把我遇到的问题分享一下

这是我的目录结构,把皮肤相关的信息放在UI文件夹里面。

default.fnt是个字体工具生成的。可以参考 奋斗小土豆 的教程,传送门http://blog.sina.com.cn/s/blog_940dd50a0101c43h.html

default.png 就是相对应的字体图片。

tou1.png 是我用图片pack工具压缩起来的。可以参考 夜明的孤行灯 的教程,传送门 http://www.cnblogs.com/htynkn/archive/2012/01/19/libgdx_16.html

uiskin.atlas是pack工具压缩起来的pack文件,改名成uiskin.atlas ,前缀与uiskin.json要一样的。注意,uiskin名字可以自己取。

uiskin.atlas文件是这样的

tou1.png
format: RGBA8888
filter: Nearest,Nearest
repeat: none
ipan
  rotate: false
  xy: 2, 2
  size: 251, 251
  orig: 251, 251
  offset: 0, 0
  index: -1
ibtn
  rotate: false
  xy: 2, 255
  size: 173, 173
  orig: 173, 173
  offset: 0, 0
  index: -1

最重要的是  uiskin.json文件了

我把我的代码贴出来,可以给你们参考一下

{
com.badlogic.gdx.graphics.g2d.BitmapFont: { default-font: { file: default.fnt }},
com.badlogic.gdx.graphics.Color: {
    green: { a: 1, b: 0, g: 1, r: 0 },
    white: { a: 1, b: 1, g: 1, r: 1 },
    red: { a: 1, b: 0, g: 0, r: 1 },
    black: { a: 1, b: 0, g: 0, r: 0 }
},
com.badlogic.gdx.scenes.scene2d.ui.Skin$TintedDrawable: {
    dialogDim: { name:ibtn ,color: { r: 0, g: 0, b: 0, a: 0.45 } }
},
com.badlogic.gdx.scenes.scene2d.ui.Button$ButtonStyle: {
    default: { },
    toggle: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.ImageButton$ImageButtonStyle: {
    default: { },
    toggle: { }
},
com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
    default: { },
    toggle: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.SelectBox$SelectBoxStyle: {
    default: { }
},
com.badlogic.gdx.scenes.scene2d.ui.SplitPane$SplitPaneStyle: {
    default-vertical: {  },
    default-horizontal: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle: {
    default: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle: {
    default: {  },
    dialog: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.Slider$SliderStyle: {
    default-horizontal: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle: {
    default: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.TextField$TextFieldStyle: {
    default: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.CheckBox$CheckBoxStyle: {
    default: { }
},
com.badlogic.gdx.scenes.scene2d.ui.List$ListStyle: {
    default: {  }
},
com.badlogic.gdx.scenes.scene2d.ui.Touchpad$TouchpadStyle: {
    default: { background: ipan, knob: ibtn }
},
com.badlogic.gdx.scenes.scene2d.ui.Tree$TreeStyle: {
    default: {  }
}
}

你们会发现,我只有

com.badlogic.gdx.scenes.scene2d.ui.Touchpad$TouchpadStyle: 

才有内容。因为我们现在测试的是 Touchpad

background 是他的背景。knob是摇杆

好啦,这是全部的东西了。如果还是有不明白的,可以加我QQ交流:862333853

很感谢  奋斗小土豆 对我的帮助。特此感谢

posted @ 2013-05-10 18:26  esdesign  阅读(1648)  评论(0编辑  收藏  举报