I am working on libgdx.I am using this example https://github.com/oakes/libgdx-examples/tree/master/SuperKoalio/core/src/net/sekao/superkoalio to create a game.
I want to add controls on the screen, so that user can move left,right,up using the buttons.I used this "Controller" class https://github.com/BrentAureli/ControllerDemo/tree/master/core/src/com/brentaureli/overlaydemo
But I am not able to see any controls on the screen.Can any one please provide me solution to fix this.
public class MainScreen implements Screen {
Stage stage;
TiledMap map;
OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
Koala koala;
Controller controller;
public void show() {
map = new TmxMapLoader().load("level1.tmx");
final float pixelsPerTile = 16;
renderer = new OrthogonalTiledMapRenderer(map, 1 / pixelsPerTile);
camera = new OrthographicCamera();
controller = new Controller();
stage = new Stage();
stage.getViewport().setCamera(camera);
koala = new Koala();
koala.layer = (TiledMapTileLayer) map.getLayers().get("walls");
koala.setPosition(20, 10);
stage.addActor(koala);
}
public void render(float delta) {
Gdx.gl.glClearColor(0.5f, 0.5f, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.position.x = koala.getX();
camera.update();
if(Gdx.app.getType() == Application.ApplicationType.Android)
controller.draw();
renderer.setView(camera);
renderer.render();
stage.act(delta);
stage.draw();
}
public void dispose() {
}
public void hide() {
}
public void pause() {
}
public void resize(int width, int height) {
camera.setToOrtho(false, 20 * width / height, 20);
controller.resize(width, height);
}
public void resume() {
}
}
Controller Class:
public class Controller {
Viewport viewport;
Stage stage;
boolean upPressed, downPressed, leftPressed, rightPressed;
OrthographicCamera cam;
public Controller(){
cam = new OrthographicCamera();
viewport = new FitViewport(800, 480, cam);
stage = new Stage(viewport);
stage.addListener(new InputListener(){
#Override
public boolean keyDown(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = true;
break;
case Input.Keys.DOWN:
downPressed = true;
break;
case Input.Keys.LEFT:
leftPressed = true;
break;
case Input.Keys.RIGHT:
rightPressed = true;
break;
}
return true;
}
#Override
public boolean keyUp(InputEvent event, int keycode) {
switch(keycode){
case Input.Keys.UP:
upPressed = false;
break;
case Input.Keys.DOWN:
downPressed = false;
break;
case Input.Keys.LEFT:
leftPressed = false;
break;
case Input.Keys.RIGHT:
rightPressed = false;
break;
}
return true;
}
});
Gdx.input.setInputProcessor(stage);
Table table = new Table();
table.left().bottom();
Image upImg = new Image(new Texture("flatDark25.png"));
upImg.setSize(50, 50);
upImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
upPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
upPressed = false;
}
});
Image downImg = new Image(new Texture("flatDark26.png"));
downImg.setSize(50, 50);
downImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
downPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
downPressed = false;
}
});
Image rightImg = new Image(new Texture("flatDark24.png"));
rightImg.setSize(50, 50);
rightImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
rightPressed = false;
}
});
Image leftImg = new Image(new Texture("flatDark23.png"));
leftImg.setSize(50, 50);
leftImg.addListener(new InputListener() {
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = true;
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
leftPressed = false;
}
});
table.add();
table.add(upImg).size(upImg.getWidth(), upImg.getHeight());
table.add();
table.row().pad(5, 5, 5, 5);
table.add(leftImg).size(leftImg.getWidth(), leftImg.getHeight());
table.add();
table.add(rightImg).size(rightImg.getWidth(), rightImg.getHeight());
table.row().padBottom(5);
table.add();
table.add(downImg).size(downImg.getWidth(), downImg.getHeight());
table.add();
stage.addActor(table);
}
public void draw(){
stage.draw();
}
public boolean isUpPressed() {
return upPressed;
}
public boolean isDownPressed() {
return downPressed;
}
public boolean isLeftPressed() {
return leftPressed;
}
public boolean isRightPressed() {
return rightPressed;
}
public void resize(int width, int height){
viewport.update(width, height);
}
}
Related
issue is next one: my game is built for 1080x1920 pixels screen but if I run it in other resolutions my actors' position is switching.
Some pictures if you haven't understood:
~> original resolution
~> other resolution
I looked for an answer for days in Google, Stackoverflow, forums, couldn't find the right answer for my problem, here's my code:
public class PlayScreen implements Screen {
private Main game;
private Stage stage;
Music music;
Sound sound;
private class MenuGame extends Actor {
Texture menu = new Texture(Gdx.files.internal("sprite/menu.png"));
#Override
public void draw(Batch batch, float alpha) {
batch.draw(menu, 0, 0,Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
}
private class NewGame extends Actor {
Texture newgame = new Texture(Gdx.files.internal("sprite/new.png"));
#Override
public void draw(Batch batch, float alpha) {
batch.draw(newgame, 350, 1000);
}
}
private class LoadGame extends Actor {
Texture loadgame = new Texture(Gdx.files.internal("sprite/load.png"));
#Override
public void draw(Batch batch, float alpha) {
batch.draw(loadgame, 350, 830);
}
}
private class CreditsGame extends Actor {
Texture creditsgame = new Texture(Gdx.files.internal("sprite/credits.png"));
#Override
public void draw(Batch batch, float alpha) {
batch.draw(creditsgame, 350, 660);
}
}
private class DonsGame extends Actor {
Texture donsgame = new Texture(Gdx.files.internal("sprite/dons.png"));
#Override
public void draw(Batch batch, float alpha) {
batch.draw(donsgame, 350, 490);
}
}
// our create()
public PlayScreen(final Main game) {
this.game = game;
Gdx.input.setInputProcessor(stage);
final Sound sound = Gdx.audio.newSound(Gdx.files.internal("sound/buttonsound.mp3"));
music = Gdx.audio.newMusic(Gdx.files.internal("sound/brun.mp3"));
music.setLooping(true);
music.setVolume(0.2f);
music.play();
// actors
MenuGame menu = new MenuGame(); // BACKGROUND
stage.addActor(menu);
final NewGame newGame = new NewGame(); // BOUTON NEW
stage.addActor(newGame);
newGame.setX(350);
newGame.setY(1000);
newGame.setWidth(410);
newGame.setHeight(100);
LoadGame loadGame = new LoadGame(); // BOUTON LOAD
stage.addActor(loadGame);
loadGame.setX(350);
loadGame.setY(830);
loadGame.setWidth(410);
loadGame.setHeight(100);
CreditsGame creditsGame = new CreditsGame(); // BOUTON CREDITS
stage.addActor(creditsGame);
creditsGame.setX(350);
creditsGame.setY(660);
creditsGame.setWidth(410);
creditsGame.setHeight(100);
DonsGame donsGame = new DonsGame(); // BOUTON DONS
stage.addActor(donsGame);
donsGame.setX(350);
donsGame.setY(490);
donsGame.setWidth(410);
donsGame.setHeight(100);
// actions
newGame.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) // NEW BOUTON ACTION
{
sound.play(0.2f);
music.stop();
game.setScreen(new Story(game));
return true;
}
});
loadGame.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) // LOAD BOUTON ACTION
{
sound.play(0.2f); //faire un truc ici
return true;
}
});
creditsGame.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) // CREDITS BOUTON ACTION
{
sound.play(0.2f);//faire un truc ici
return true;
}
});
donsGame.addListener(new ClickListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) // DONS BOUTON ACTION
{
sound.play(0.2f);//faire un truc ici
return true;
}
});
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
stage.dispose();
music.dispose();
sound.dispose();
}
}
You are positioning everything after hardcoded values. So right now your game is very resolution depended.
What you need to use is Viewports.
Here are some links to get you started:
Brent Aureli - Aspect Ratios & Viewports
libgdx github - viewports
libgdx github - scene2d
this is happening because different devices has different screen size. so it will appear differently . to avoid this you must use the viewPort .for more clarification you must read the libgdx api about the multi resolution and the viewPort . here i will suggest you some sample code to avoid this multi screen issue. please follow this pattern which acan solve your problem.
// in your main class
public class myGame extends ApplicationAdapter {
public OrthographicCamera camera
public Viewport viewPort
private SpriteBatch batch;
private BitMapFont myScoreFont
public myGame() {
}
#Override
public void create() {
myScoreFont = new BitmapFont(Gdx.files.internal(font.txt), true);
batch = new SpriteBatch();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.position.set(0, 0, 0);
camera.update();
camera.setToOrtho(false, Constants.APP_WIDTH, Constants.APP_HEIGHT);
viewPort = new FillViewport(1280, 800, camera);
}
#Override
public void dispose() {
batch.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
float deltaTime = Gdx.graphics.getDeltaTime();
batch.setProjectionMatrix(camera.combined);
batch.begin();
myScoreFont.draw(batch,"myText",0,0)
batch.end();
}
#Override
public void resize(int width, int height) {
viewPort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
for more details you must read the below link about the viewport
https://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/utils/viewport/FillViewport.html
Whenever I try to destroy a body in JBox2D it always give me a NullPointerException, and I have checked online and they show various ways of doing it 'safely' which I don't understand. I was wondering if anyone could type up some simple pseudo code to show how you would destroy a body in Box2D. I am also using the LibGDX API as well.
Here is my code that I use:
public void render(float delta) {
if(bodiesContainer.size() > 0)
{
for(Body body:bodiesContainer)
{
world.destroyBody(body);
}
}
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(delta);
stage.draw();
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
camera.update();
debugRenderer.render(world, camera.combined);
batch.begin();
batch.end();
}
public void show() {
// TODO Auto-generated method stub
batch = new SpriteBatch();
camera = new OrthographicCamera(Gdx.graphics.getWidth()/5, Gdx.graphics.getHeight()/5);
world = new World(new Vector2(0,0f), true);
debugRenderer = new Box2DDebugRenderer();
stage = new Stage();
particleCreator = new ParticleCreator(0, 0, 1);
particleCreator.destroyParticle(world, particleBody);
if (particleCreator == null)
{
System.out.println("true");
}
if (particleBody == null)
{
System.out.println("true particleBody");
}
if (particleBodyDef == null)
{
System.out.println("true particleBodyDef");
}
Gdx.input.setInputProcessor(new InputProcessor() {
#Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int arg0, int arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyUp(int arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyTyped(char arg0) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean keyDown(int keycode) {
switch(keycode) {
case Keys.SPACE:
break;
}
return false;
}
});
}
public class ParticleCreator
{
//Default constructor
public ParticleCreator()
{
}
public ParticleCreator(World world, BodyDef bodyDef, Body body, float position_x, float position_y, float particle_radius)
{
bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(position_x, position_y);
CircleShape circleShape = new CircleShape();
circleShape.setRadius(particle_radius);
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = circleShape;
fixtureDef.restitution = 0;
body = world.createBody(bodyDef);
body.createFixture(fixtureDef);
}
public void destroyParticle(World world, Body body)
{
world.destroyBody(body);
System.out.println("This Particle has been destroyed !!!");
}
}
The bodiesContainer is an ArrayList, and the particleBody is a JBox2D body i wish to destroy, but every time I press the space button it crashes and gives me a NullPointerException.
I've looked at other Stackoverflow questions and done what the answers have told me, but I can't seem to get my class to display any text.
Here's my code where all the text is being displayed:
package com.frinkly.jumpcat;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class WorldRenderer {
private static final float CAMERA_WIDTH = 12f;
private static final float CAMERA_HEIGHT = 20f;
private OrthographicCamera cam;
private SpriteBatch spriteBatch;
private int width;
private int height;
private float ppuX;
private float ppuY;
private BitmapFont font;
private World world;
private Cat cat;
ShapeRenderer debugRenderer = new ShapeRenderer();
public WorldRenderer(World newWorld) {
this.world = newWorld;
this.cat = world.getCat();
this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
this.cam.position.set(CAMERA_WIDTH / 2f, CAMERA_HEIGHT / 2f, 0);
spriteBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("font/font.fnt"), false);
font.setColor(Color.BLACK);
this.cam.update();
spriteBatch.setProjectionMatrix(new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()));
}
public void setSize(int newWidth, int newHeight) {
width = newWidth;
height = newHeight;
ppuX = (float) width / CAMERA_WIDTH;
ppuY = (float) height / CAMERA_HEIGHT;
}
public void render() {
cam.update();
spriteBatch.begin();
debugDraw();
drawPoints();
spriteBatch.end();
}
private void drawPoints() {
font.setColor(1.0f, 1.0f, 1.0f, 1.0f);
font.setScale(1, height/width);
font.draw(spriteBatch, Integer.toString(cat.getPoints()), 0.5f, height/width*0.5f);
}
private void debugDraw() {
debugRenderer.setProjectionMatrix(cam.combined);
debugRenderer.begin(ShapeType.Filled);
for(Block block : world.getBlocks()) {
Rectangle rect1 = block.getBounds1();
Rectangle rect2 = block.getBounds2();
float x1 = block.getPosition().x;//+block.getPosition().x
float y1 = block.getPosition().y;//+block.getPosition().y
debugRenderer.setColor(new Color(1, 0, 0, 1));
debugRenderer.rect(x1, y1, rect1.width, rect1.height);
debugRenderer.rect(x1, y1 + rect1.height + block.getSPACE(), rect2.width, rect2.height);
}
Rectangle rect = cat.getBounds();
float x1 = cat.getPosition().x + rect.x;
float y1 = cat.getPosition().y + rect.y;
debugRenderer.setColor(new Color(0, 1, 0, 1));
debugRenderer.rect(x1, y1, rect.width, rect.height);
debugRenderer.end();
}
private void drawBlocks() {
}
private void drawCat() {
}
}
Here's the GameScreen that calls the renderer to display the text:
package com.frinkly.jumpcat;
import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.Screen;
public class GameScreen implements Screen, InputProcessor{
private World world;
private WorldRenderer renderer;
private CatController catController;
private BlockController blockController;
private int screenWidth;
private int screenHeight;
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1f, 1f, 1f, 1f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
blockController.update(delta);
catController.update(delta);
renderer.render();
}
#Override
public void resize(int width, int height) {
screenWidth = width;
screenHeight = height;
renderer.setSize(width, height);
}
#Override
public void show() {
loadAssets();
world = new World();
renderer = new WorldRenderer(world);
catController = new CatController(world);
blockController = new BlockController(world);
blockController.start();
Gdx.input.setInputProcessor(this);
}
private void loadAssets() {
}
#Override
public void hide() {
Gdx.input.setInputProcessor(null);
}
#Override
public void pause() {
Gdx.input.setInputProcessor(null);
}
#Override
public void resume() {
Gdx.input.setInputProcessor(this);
}
#Override
public void dispose() {
}
#Override
public boolean keyDown(int keycode) {
if(keycode == Keys.SPACE) {
catController.jumpPressed();
}
return true;
}
#Override
public boolean keyUp(int keycode) {
return false;
}
#Override
public boolean keyTyped(char character) {
return false;
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
if(!Gdx.app.getType().equals(ApplicationType.Android)) {
return false;
}
catController.jumpPressed();
return true;
}
#Override
public boolean touchUp(int screenX, int screenY, int pointer, int button) {
if(!Gdx.app.getType().equals(ApplicationType.Android)) {
return false;
}
return true;
}
#Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean mouseMoved(int screenX, int screenY) {
// TODO Auto-generated method stub
return false;
}
#Override
public boolean scrolled(int amount) {
// TODO Auto-generated method stub
return false;
}
}
My fonts are in the right folder, and I used Hiero to generate the files. I used 150 size and it generated font1png-font5.png, which I've also put in my font folder.
The error should be in your render() method:
cam.update();
spriteBatch.begin();
debugDraw();
drawPoints();
spriteBatch.end();
Inside debugDraw() you begin() a ShapeRenderer. So 2 objects try to draw at the same time. Do the debugDraw() first and begin() your SpriteBatch after that:
cam.update();
debugDraw();
spriteBatch.begin();
drawPoints();
spriteBatch.end();
Hope it helps
I'm struggling with a at the first sight simple problem (and probably it is so...) - I have to disable certain button in the JTable upon value in one of the cells within the same row.
Below is my inner class for button rendering and editing:
private class CellButton {
class ButtonsPanel extends JPanel {
public JButton jbutton = new JButton("Accept!!");
public ButtonsPanel() {
super();
setOpaque(true);
jbutton.setFocusable(false);
jbutton.setRolloverEnabled(false);
jbutton.setSize(122, 30);
jbutton.setFont(new java.awt.Font("Tahoma", 3, 12));
jbutton.setForeground(new java.awt.Color(0, 102, 102));
add(jbutton);
}
private class ButtonsRenderer extends ButtonsPanel implements TableCellRenderer {
ButtonsPanel bp;
public ButtonsRenderer() {
super();
}
#Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
this.setBackground(isSelected ? table.getSelectionBackground() : table.getBackground());
Object o = table.getModel().getValueAt(row, 8);
bp = (ButtonsPanel) o;
if (jTable5.getValueAt(row, 3).equals("NORMALNA")) {
bp.jbutton.setEnabled(false);
}
return this;
}
}
private class ButtonsEditor extends ButtonsPanel implements TableCellEditor {
ButtonsPanel bp;
public ButtonsEditor(final JTable table, final String tableName) {
super();
//DEBUG: view button click -> control key down + edit button(same cell) press -> remain selection color
MouseListener ml = new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
ButtonModel m = ((JButton) e.getSource()).getModel();
if (m.isPressed() && table.isRowSelected(table.getEditingRow()) && e.isControlDown()) {
setBackground(table.getBackground());
}
}
};
jbutton.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
int row = table.convertRowIndexToModel(table.getEditingRow());
Object o = table.getModel().getValueAt(row, 0);
fireEditingStopped();
//update info status
setDmlUpdateStatusPrejeteInfo();
jDialog2.requestFocus();
}
});
addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
fireEditingStopped();
}
});
}
#Override
public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
this.setBackground(table.getSelectionBackground());
Object o = table.getModel().getValueAt(row, 8);
bp = (ButtonsPanel) o;
if (jTable5.getValueAt(row, 3).equals("NORMALNA")) {
bp.jbutton.setEnabled(false);
}
return this;
}
#Override
public Object getCellEditorValue() {
return "";
}
transient protected ChangeEvent changeEvent = null;
#Override
public boolean isCellEditable(EventObject e) {
return true;
}
#Override
public boolean shouldSelectCell(EventObject anEvent) {
return true;
}
#Override
public boolean stopCellEditing() {
fireEditingStopped();
return true;
}
#Override
public void cancelCellEditing() {
fireEditingCanceled();
}
#Override
public void addCellEditorListener(CellEditorListener l) {
listenerList.add(CellEditorListener.class, l);
}
#Override
public void removeCellEditorListener(CellEditorListener l) {
listenerList.remove(CellEditorListener.class, l);
}
public CellEditorListener[] getCellEditorListeners() {
return listenerList.getListeners(CellEditorListener.class);
}
protected void fireEditingStopped() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingStopped(changeEvent);
}
}
}
protected void fireEditingCanceled() {
// Guaranteed to return a non-null array
Object[] listeners = listenerList.getListenerList();
// Process the listeners last to first, notifying
// those that are interested in this event
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == CellEditorListener.class) {
// Lazily create the event:
if (changeEvent == null) {
changeEvent = new ChangeEvent(this);
}
((CellEditorListener) listeners[i + 1]).editingCanceled(changeEvent);
}
}
}
}
public ButtonsRenderer br() {
return new ButtonsRenderer();
}
public ButtonsEditor be(JTable jTable, String tableName) {
return new ButtonsEditor(jTable, tableName);
}
}
a) it's better not to call a Jbutton in the name Jbutton, it's just confusing
b) there are many way to disable a jbutton (make it disappear, remove it, resize it...) but the best one is probably this:
Jbutton.setEnabled(false);
c) I think you can use this very useful guide: http://www.macs.hw.ac.uk/guidebook/?name=JButton&page=1
I have created 10 object layers using Tile software,and rendering tiled map using OrthogonalTiledMapRenderer object.
renderer.setView(camera);
renderer.render();
I have created backgroundTiledMap class extending Actor class to it.
adding object of the backgroundTiledMap class in stage of mainGame class.
Its rendering perfectly.
but when I am trying to add next actor to the same stage. its not getting rendered.
is there any different way of using tiled map and actors to detect collision in between.
Below the actual code
backgroundTiledMap.java class which is extended as actor and used to draw background from tiled map (created in tile software)
public class BackgroundTiledMap extends Actor {
public MapProperties properties;
public MapLayer layer;
public MapObject mapObject;
public TiledMapTileLayer tiledLayer;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
public Array<Rectangle> tiles = new Array<Rectangle>();
private Pool<Rectangle> rectPool = new Pool<Rectangle>() {
#Override
protected Rectangle newObject () {
return new Rectangle();
}
};
GameStartPoint game;
Stage stage;
Reptile reptile;
public BackgroundTiledMap(GameStartPoint game,Stage stage)
{
this.game = game;
this.stage = stage;
init();
}
public void init()
{
map = new TmxMapLoader().load("Images/GuideCrocodile.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / 16f);
camera = new OrthographicCamera();
camera.setToOrtho(false, 70, 50);
camera.update();
stage.setCamera(camera);
}
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
render();
}
public void render()
{
camera.update();
renderer.setView(camera);
renderer.render();
}
}
this is mainGame screen used to create stage and all other actors and objects are added here
public class GuideCrocodileScreen implements Screen {
public GameStartPoint game;
private Stage stage;
public BackgroundTiledMap backgroundTiledMap;
public Reptile reptile;
MoveToAction moveAction;
public GuideCrocodileScreen(GameStartPoint game)
{
this.game = game;
stage = new Stage();
}
#Override
public void render(float delta) {
// clear the screen
Gdx.gl.glClearColor(0.7f, 0.7f, 1.0f, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
Gdx.input.setInputProcessor(stage);
stage.draw();
}
private void addReptile(){
reptile = new Reptile(stage,100,100,50,50);
// backgroundTiledMap.getTiles(100,100,50,50);
reptile.addListener(new InputListener() {
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true;
}
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
}
public void touchDragged (float x, float y, int pointer) {
reptile.updateReptile(x,y);
}
public boolean touchMoved (float x, float y) {
//reptile.updateReptile(x,y);
return false;
}
});
stage.addActor(reptile);
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
addBackgroundArea();
addReptile();
}
private void addBackgroundArea(){
backgroundTiledMap = new BackgroundTiledMap(game,stage);
stage.addActor(backgroundTiledMap);
}
}
new actor class , this is actor to move around the screen detecting collision with the objects drawn on backgroundTiledMap reptile.java (this is not getting rendered)
public class Reptile extends Actor {
public GameStartPoint game;
private OrthogonalTiledMapRenderer renderer;
public SpriteBatch batch;
public int screenWidth;
public int screenHeight;
private Texture reptiletexture;
private TextureRegion reptileRegion;
private Stage stage;
public Reptile( Stage stage,int x, int y,int width, int height){
setX((float)x);
setY((float)y);
setWidth((float)width);
setHeight((float)height);
this.setTouchable(Touchable.enabled);
this.stage = stage;
initialize();
}
private void initialize(){
this.screenWidth = GameStartPoint.WIDTH;
this.screenHeight = GameStartPoint.HEIGHT;
reptiletexture = new Texture(Gdx.files.internal("Images/basketball.png"));
reptileRegion = new TextureRegion(reptiletexture);
this.setVisible(true);
}
#Override
public void draw(SpriteBatch batch,float parentAlpha)
{
super.draw(batch,parentAlpha);
batch.draw(reptileRegion, getX(), getY(), getOriginX(),
getOriginY(), getWidth(), getHeight(), getScaleX(),
getScaleY(), getRotation());
}
public void updateReptile(float x,float y)
{
int duration = 5;
if(Gdx.input.isTouched())
{
MoveToAction action = Actions.action(MoveToAction.class);
action.setPosition(x, y);
action.setDuration(duration);
this.addAction(action);
}
}
#Override
public Actor hit(float x, float y, boolean touchable) {
//Gdx.app.log("","reptile is touched.. ") ;
updateReptile(x,y);
return super.hit(x, y, touchable);
}
}
An old question, but this was the first hit when I was googling for the same problem...
If the actor uses some other methods for drawing instead of the batch, the batch should be ended and then restarted again at the end of the method. So in BackgroundTiledMap:
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
batch.end();
render(); // Renders the TiledMap using OrthogonalTiledMapRenderer
batch.begin();
}
See https://github.com/libgdx/libgdx/wiki/Scene2d#drawing for more info.
To draw an actor above the tilemap you must call stage.draw after rendering tilemap like this:
#Override
public void draw(SpriteBatch batch, float parentAlpha) {
render();
super.draw(batch, parentAlpha);
}
For the collision part you can get the objects that you have created in the TiledMap in different classes such as
EllipseMapObject, PolygonMapObject, RectangleMapObject etc. from where you can extract polylines, polygons etc. and use Intersector class to check their bounds.