libgdx学习笔记.doc_第1页
libgdx学习笔记.doc_第2页
libgdx学习笔记.doc_第3页
libgdx学习笔记.doc_第4页
libgdx学习笔记.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Texture(纹理)public class HelloWorld implements ApplicationListener public SpriteBatch batch; / 声明纹理 public Texture texture; public Sprite sprite; Override public void create() batch = new SpriteBatch(); / 实例化texture texture = new Texture(Gernal(data/Potato.jpg); public void render() Gdx.gl.glClearColor(1, 1, 1, 1);/ 设置背景为白色 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);/ 清屏 batch.begin(); batch.draw(texture, 0, 0, 480, 320); batch.end(); SpriteBatch类(SpriteBatch就是一个画笔)首先声明类名,实例化、在绘制的的时候分三步,必须先调用begin()方法,然后再调用draw()方法,最后画完了调用end()结束。public class HelloWorld implements ApplicationListener public SpriteBatch batch; / 声明纹理 public Texture texture; public Sprite sprite; public void create() batch = new SpriteBatch(); / 实例化texture texture = new Texture(Gernal(data/Potato.jpg); public void render() Gdx.gl.glClearColor(1, 1, 1, 1);/ 设置背景为白色 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);/ 清屏 batch.begin(); batch.draw(texture, 0, 0, 480, 320); batch.end(); TextureRegion 类(截图工具)public class HelloWorld implements ApplicationListener public SpriteBatch batch; / 声明纹理 public Texture texture; public TextureRegion textureRegion; public Sprite sprite; public void create() batch = new SpriteBatch(); / 实例化texture texture = new Texture(Gernal(data/Potato.jpg); textureRegion = new TextureRegion(texture, 0, 0, 512, 512); / textureRegion = new TextureRegion(texture, 512, 512, -512, / -512);-反方向截图 public void dispose() batch.dispose(); texture.dispose(); public void render() Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.begin(); / batch.draw(texture, 0, 0, 480, 320); batch.draw(textureRegion, 0, 0, 480, 320); batch.end(); Sprite类public class HelloWorld implements ApplicationListener public SpriteBatch batch; / 声明纹理 public Texture texture; public Sprite sprite; public void create() batch = new SpriteBatch(); / 实例化texture texture = new Texture(Gernal(data/Potato.jpg); TextureRegion region = new TextureRegion(texture, 512, 0, 512, 512); sprite = new Sprite(region); sprite.setSize(120, 120);/ 设置绘制的大小,为了方便我们设置的120,120,小一点方便查看 sprite.setOrigin(sprite.getWidth() / 2, sprite.getHeight() / 2);/ 设置旋转的中心点,咱们就设置为屏幕的中心点 sprite.setRotation(50);/ 这个是以上面设置的中心点为中心,旋转一定角度的设置 sprite.setPosition(150, 110);/ 起始位置设置为中心区域附近(150,110) sprite.setColor(1, 0, 1, 1);/ 颜色就设置为粉色吧 public void render() Gdx.gl.glClearColor(1, 1, 1, 1);/ 设置背景颜色为白色 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);/ 清屏 batch.begin(); sprite.draw(batch); batch.end(); BitmapFont类public class HelloWorld implements ApplicationListener public SpriteBatch batch; BitmapFont font; public void create() font = new BitmapFont(Gernal(data/Potato.fnt), Gernal(data/Potato.png), false); / font.setColor(0.5f,0.4f,0.6f,1);/设置颜色 / / font.setScale(1.0f);/字体比例大小 batch = new SpriteBatch(); Override public void render() Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); batch.begin(); font.draw(batch, -奋斗小土豆丶, 200, 160); font.drawMultiLine(batch, 爱情来得快,去的也快,n只有猪肉卷是永恒的, Gdx.graphics.getWidth() / 5, 0.8f * Gdx.graphics.getHeight(); batch.end(); Animation类Animation walkAnimation = new Animation(float fDuration, keyFrames)(1)TextureRegion数组代码:TextureRegion tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() /FRAME_ROWS);这个段代码是怎么回事呢?他是采用分离式的方法分分割传入的纹理,将获得的纹理分为一个二维数组。记住,前提是分割的矩形大小相等。然后使用临时变量,填充walkframes数组。这是样使用起来很方便。NORMAL:这个不用说了,就是正常的播放模式。REVERSED:反向播放,从后向前播放,这个就像人物倒退的跑。LOOP:持续播放,这个比较常用。LOOP_REVERSED:持续倒退播放。LOOP_PINGPONG: 向前播放几张图片,再向后播放几帧图片 代码stateTime += Gdx.graphics.getDeltaTime(),他是一个获取一个状态下所持续的一个时间。就像我们在现实世界使用的时间一样,一般配合系统时间使用Gdx.graphics.getDeltaTime():获取系统渲染时间,一般默认是0.173秒。 public class Map implements ApplicationListener private static final int FRAME_COLS = 6; private static final int FRAME_ROWS = 5; Animation walkAnimation; Texture walkSheet; TextureRegion walkFrames; SpriteBatch batch; TextureRegion currentFrame; float stateTime; Override public void create() walkSheet = new Texture(Gernal(animation_sheet.png); TextureRegion tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS); walkFrames = new TextureRegionFRAME_COLS * FRAME_ROWS; int index = 0; for (int i = 0; i FRAME_ROWS; i+) for (int j = 0; j FRAME_COLS; j+) walkFramesindex+ = tmpij; walkAnimation = new Animation(0.025f, walkFrames); walkAnimation.setPlayMode(walkAnimation.LOOP_PINGPONG); batch = new SpriteBatch(); stateTime = 0f; public void render() Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); stateTime += Gdx.graphics.getDeltaTime(); currentFrame = walkAnimation.getKeyFrame(stateTime, true); batch.begin(); batch.draw(currentFrame, Gdx.graphics.getWidth() / 2, Gdx.graphics.getHeight() / 2); batch.end(); Image控件(图片) Image(Texture texture) 和 Image(TextureRegion region)public class MyGdxGame implements ApplicationListener Stage stage; Image VictoriaImage; TextureRegion region; Texture tex; public void create() tex = new Texture(Gernal(data/1.png); region = new TextureRegion(tex, 0, 0, 512, 512); VictoriaImage = new Image(region); VictoriaImage.setColor(Color.PINK);/颜色 VictoriaImage.setScale(0.5F);/缩放比例 VictoriaImage.setPosition(230, 40);/绘画起点 VictoriaImage.setOrigin(0, 0);/ 设置旋转中心 VictoriaImage.setRotation(45);/旋转中心 VictoriaImage.setSize(390, 300);/绘画大小 stage = new Stage(480, 320, false); Gdx.input.setInputProcessor(stage); stage.addActor(VictoriaImage); public void render() Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); 按钮(Button)ImageButton(Drawable imageUp, Drawable imageDown, Drawable imageChecked) ImageButton(Skin skin) public class MyGdxGame implements ApplicationListener Stage stage; TextureRegionDrawable up; TextureRegionDrawable down; TextureRegion buttonUp; TextureRegion buttonDown; Texture tex; ImageButton button; public void create() tex = new Texture(Gernal(data/control.png); TextureRegion tmp = TextureRegion.split(tex, 120, 120); buttonUp = tmp00; buttonDown = tmp01; up = new TextureRegionDrawable(buttonUp); down = new TextureRegionDrawable(buttonDown); button = new ImageButton(up, down); button.setPosition(100, 100); stage = new Stage(480, 320, false); Gdx.input.setInputProcessor(stage); stage.addActor(button); public void render() Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw(); Actor类(演员)public class mario extends Actor public static float x;public static float y;public static float statetime;Texture texture;TextureRegion currentFrame;ImageButton buttonL;ImageButton buttonR;Animation aniRight;Animation aniLeft;Animation aniIdle;public static int LeftState = 1;public static int IdeltState = 2;public static int RightState = 3;public static int state = 2;public mario(float x, float y) this.x = x;this.y = y;this.show();public void show() texture = new Texture(Gernal(data/mario.png);TextureRegion spilt = TextureRegion.split(texture, 64, 64);TextureRegion miror = TextureRegion.split(texture, 64, 64);for (TextureRegion region1 : miror) for (TextureRegion region2 : region1) region2.flip(true, false);/ 右TextureRegion regionR = new TextureRegion3;regionR0 = spilt01;regionR1 = spilt02;regionR2 = spilt00;aniRight = new Animation(0.1f, regionR);/ 左TextureRegion regionL = new TextureRegion3;regionL0 = miror01;regionL1 = miror02;regionL2 = miror00;aniLeft = new Animation(0.1f, regionL);/ 空闲TextureRegion regionI = new TextureRegion1;regionI0 = spilt00;aniIdle = new Animation(0.1f, regionI);buttonL = new ImageButton(new TextureRegionDrawable(spilt10),new TextureRegionDrawable(spilt11);buttonR = new ImageButton(new TextureRegionDrawable(miror10),new TextureRegionDrawable(miror11);buttonL.setPosition(20, 20);buttonR.setPosition(200, 20);buttonL.addListener(new InputListener() public void touchUp(InputEvent event, float x, float y,int pointer, int button) state = IdeltState;super.touchUp(event, x, y, pointer, button);public boolean touchDown(InputEvent event, float x, float y,int pointer, int button) state = LeftState;return true;);buttonR.addListener(new InputListener() public void touchUp(InputEvent event, float x, float y,int pointer, int button) / TODO Auto-generated method stubstate = IdeltState;super.touchUp(event, x, y, pointer, button);public boolean touchDown(InputEvent event, float x, float y,int pointer, int button) / TODO Auto-generated method stubstate = RightState;return true;);public void update()if(state = LeftState)this.x -=1.5f;if(x400) this.x = 400;this.x = x;public void aniCheck()if(state = LeftState)currentFrame = aniLeft.getKeyFrame(statetime, true);else if(state = RightState)currentFrame = aniRight.getKeyFrame(statetime, true);else if (state = IdeltState) currentFrame = aniIdle.getKeyFrame(statetime,true);public void draw(SpriteBatch batch, float parentAlpha) statetime+=Gdx.graphics.getDeltaTime();this.update();this.aniCheck();batch.draw(currentFrame, x, y);public void act(float delta) / TODO Auto-generated method stubsuper.act(delta);public class MyGdxGame implements ApplicationListener Stage stage; Mario mario; Image image; Override public void create() image = new Image(new Texture(Gernal(data/13.jpg); image.setPosition(0, 170); stage = new Stage(480, 320, false); mario = new Mario(100, 190); Gdx.input.setInputProcessor(stage); stage.addActor(image); stage.addActor(mario); stage.addActor(mario.buttonL); stage.addActor(mario.buttonR); Override public void render() Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); stage.act(); stage.draw();Stage(舞台)一个Stage必须负责接收输入事件,同时将它分配给演员。这通常是通过Stage的gdx.input.setinputprocessor来实现。Stage(float width, float height, boolean keepAspectRatio, SpriteBatch batch)public class MyGame implements ApplicationListener GameStage gameStage; StartStage startStage; StoreStage storeStage; public void create() gameStage = new GameStage(); startStage = new StartStage(); storeStage = new StoreStage(); public void selectStageRender() if(Constants.Stageflag = Constants.StartStageOn) Gdx.input.setInputProcessor(startStage); startStage.act(); startStage.draw(); else if (Constants.Stageflag = Constants.GameStageOn) Gdx.input.setInputProcessor(gameStage); gameStage.act(); gameStage.draw(); else if (Constants.Stageflag = Constants.StoreStageOn) Gdx.input.setInputProcessor(storeStage); storeStage.act(); storeStage.draw(); public void render() Gdx.gl.glClearColor(1, 1, 1, 1); Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); selectStageRender(); public class StartStage extends Stage Texture texture; TextureRegion startRegion; Image startImage; Image newGameBtn; TextureRegion new

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论