เพิ่มเติมไฟล์ฉากและรูปประกอบควรทำใน folder assets ไม่เลยจะได้ไม่มีปัญหา link ไฟล์
แก้ไฟล์ PlayScreen.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.mygdx.game;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
/**
*
* @author kitty
*/
public class PlayScreen implements Screen {
c_Super_Mario game;
Texture texture;
OrthographicCamera gamecam;
Viewport gameport;
Hud hud;
TmxMapLoader maploader;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
public PlayScreen(c_Super_Mario game) {
this.game = game;
texture = new Texture("badlogic.jpg");
gamecam = new OrthographicCamera();
//gameport = new StretchViewport(800,480,gamecam);
//gameport = new ScreenViewport(gamecam);
gameport = new FitViewport(game.V_width, game.V_height, gamecam);
hud = new Hud(game.batch);
maploader = new TmxMapLoader();
map = maploader.load("test1.tmx");//โหลดไฟล์ฉาก
renderer = new OrthogonalTiledMapRenderer(map);
gamecam.position.set(gameport.getScreenWidth() / 2, gameport.getScreenHeight() / 2, 0);//จัดตำแหน่งกล้องให้อยู่ตรงกลาง
}
@Override
public void show() {
}
void handleInput(float dt) {
if(Gdx.input.isTouched()){
gamecam.position.x+=100*dt;//เลื่อนกล้องตามแกน x ที่ละ 100
}
}
void update(float dt) {
handleInput(dt);
gamecam.update();
renderer.setView(gamecam);
}
@Override
public void render(float f) {
update(f);
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
/* game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
game.batch.draw(texture, 0, 0);
game.batch.end();*/
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
@Override
public void resize(int w, int h) {
gameport.update(w, h);
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void hide() {
}
@Override
public void dispose() {
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น