




已閱讀5頁,還剩9頁未讀, 繼續(xù)免費閱讀
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
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就是一個畫筆)首先聲明類名,實例化、在繪制的的時候分三步,必須先調(diào)用begin()方法,然后再調(diào)用draw()方法,最后畫完了調(diào)用end()結(jié)束。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);/ 設置旋轉(zhuǎn)的中心點,咱們就設置為屏幕的中心點 sprite.setRotation(50);/ 這個是以上面設置的中心點為中心,旋轉(zhuǎn)一定角度的設置 sprite.setPosition(150, 110);/ 起始位置設置為中心區(qū)域附近(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數(shù)組代碼:TextureRegion tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight() /FRAME_ROWS);這個段代碼是怎么回事呢?他是采用分離式的方法分分割傳入的紋理,將獲得的紋理分為一個二維數(shù)組。記住,前提是分割的矩形大小相等。然后使用臨時變量,填充walkframes數(shù)組。這是樣使用起來很方便。NORMAL:這個不用說了,就是正常的播放模式。REVERSED:反向播放,從后向前播放,這個就像人物倒退的跑。LOOP:持續(xù)播放,這個比較常用。LOOP_REVERSED:持續(xù)倒退播放。LOOP_PINGPONG: 向前播放幾張圖片,再向后播放幾幀圖片 代碼stateTime += Gdx.graphics.getDeltaTime(),他是一個獲取一個狀態(tài)下所持續(xù)的一個時間。就像我們在現(xiàn)實世界使用的時間一樣,一般配合系統(tǒng)時間使用Gdx.graphics.getDeltaTime():獲取系統(tǒng)渲染時間,一般默認是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);/ 設置旋轉(zhuǎn)中心 VictoriaImage.setRotation(45);/旋轉(zhuǎn)中心 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來實現(xiàn)。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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 咨詢類合同框架協(xié)議
- 售電公司轉(zhuǎn)讓合同協(xié)議
- 商場合同終止協(xié)議模板
- 推拿治療學腰痛
- 室性早搏普魯帕酮治療
- 民間房產(chǎn)抵押協(xié)議書
- 環(huán)評服務技術咨詢合同
- 二零二五版房屋買賣尾款補充協(xié)議書
- 薪制聘用合同正規(guī)范例
- 二零二五版施工現(xiàn)場安全協(xié)議書
- 2024年湖南長沙中考生物真題及答案
- 尾礦庫污染隱患排查治理制度
- 居家養(yǎng)老上門服務投標文件
- 砂石料居間合同范例
- 市場營銷培訓課件
- 省級啤酒代理權合同
- DB11T 1609-2018 預拌噴射混凝土應用技術規(guī)程
- 熒光-光譜完整版本
- 全過程工程咨詢服務投標方案(技術方案)
- 2024至2030年中國傳染病醫(yī)院產(chǎn)業(yè)發(fā)展動態(tài)及未來前景展望報告
- 2024年新人教版七年級上冊歷史教學課件 第10課 秦末農(nóng)民大起義
評論
0/150
提交評論