I have a music playing in ApplicationAdapter. I want to pause and play the music form PlayState. How can i access instance of ApplicationAdapter from anywhere. I tried using GreenBot EventBus but it only works with android. Any help?
Thanks in advance.
Music is playing in below class.
package com.brentaureli.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.brentaureli.game.states.GameStateManager;
import com.brentaureli.game.states.MenuState;
public class FlappyDemo extends ApplicationAdapter {
public static final int WIDTH = 480;
public static final int HEIGHT = 800;
public static final String TITLE = "Flappy Bird";
private GameStateManager gsm;
private SpriteBatch batch;
private Music music;
#Override
public void create () {
batch = new SpriteBatch();
gsm = new GameStateManager();
music = Gdx.audio.newMusic(Gdx.files.internal("music.mp3"));
music.setLooping(true);
music.setVolume(0.1f);
music.play();
Gdx.gl.glClearColor(1, 0, 0, 1);
gsm.push(new MenuState(gsm));
}
#Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
gsm.update(Gdx.graphics.getDeltaTime());
gsm.render(batch);
}
#Override
public void dispose() {
super.dispose();
music.dispose();
}
}
I want to pause and play form below class:
package com.brentaureli.game.states;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.Array;
import com.brentaureli.game.FlappyDemo;
import com.brentaureli.game.sprites.Bird;
import com.brentaureli.game.sprites.Tube;
public class PlayState extends State {
private static final int TUBE_SPACING = 125;
private static final int TUBE_COUNT = 4;
private static final int GROUND_Y_OFFSET = -50;
private Bird bird;
private Texture bg;
private Texture ground;
private Vector2 groundPos1, groundPos2;
private Array<Tube> tubes;
public PlayState(GameStateManager gsm) {
super(gsm);
bird = new Bird(50, 300);
cam.setToOrtho(false, FlappyDemo.WIDTH / 2, FlappyDemo.HEIGHT / 2);
bg = new Texture("bg.png");
ground = new Texture("ground.png");
groundPos1 = new Vector2(cam.position.x - cam.viewportWidth / 2, GROUND_Y_OFFSET);
groundPos2 = new Vector2((cam.position.x - cam.viewportWidth / 2) + ground.getWidth(), GROUND_Y_OFFSET);
tubes = new Array<Tube>();
for(int i = 1; i <= TUBE_COUNT; i++){
tubes.add(new Tube(i * (TUBE_SPACING + Tube.TUBE_WIDTH)));
}
}
#Override
protected void handleInput() {
if(Gdx.input.justTouched())
bird.jump();
}
#Override
public void update(float dt) {
handleInput();
updateGround();
bird.update(dt);
cam.position.x = bird.getPosition().x + 80;
for(int i = 0; i < tubes.size; i++){
Tube tube = tubes.get(i);
if(cam.position.x - (cam.viewportWidth / 2) > tube.getPosTopTube().x + tube.getTopTube().getWidth()){
tube.reposition(tube.getPosTopTube().x + ((Tube.TUBE_WIDTH + TUBE_SPACING) * TUBE_COUNT));
}
if(tube.collides(bird.getBounds()))
gsm.set(new MenuState(gsm));
}
if(bird.getPosition().y <= ground.getHeight() + GROUND_Y_OFFSET)
gsm.set(new MenuState(gsm));
cam.update();
}
#Override
public void render(SpriteBatch sb) {
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(bg, cam.position.x - (cam.viewportWidth / 2), 0);
sb.draw(bird.getTexture(), bird.getPosition().x, bird.getPosition().y);
for(Tube tube : tubes) {
sb.draw(tube.getTopTube(), tube.getPosTopTube().x, tube.getPosTopTube().y);
sb.draw(tube.getBottomTube(), tube.getPosBotTube().x, tube.getPosBotTube().y);
}
sb.draw(ground, groundPos1.x, groundPos1.y);
sb.draw(ground, groundPos2.x, groundPos2.y);
sb.end();
}
#Override
public void dispose() {
bg.dispose();
bird.dispose();
ground.dispose();
for(Tube tube : tubes)
tube.dispose();
System.out.println("Play State Disposed");
}
private void updateGround(){
if(cam.position.x - (cam.viewportWidth / 2) > groundPos1.x + ground.getWidth())
groundPos1.add(ground.getWidth() * 2, 0);
if(cam.position.x - (cam.viewportWidth / 2) > groundPos2.x + ground.getWidth())
groundPos2.add(ground.getWidth() * 2, 0);
}
}
Full Code: https://github.com/BrentAureli/FlappyDemo
Get ApplicationListener instance anywhere in your game by this.
ApplicationListener applicationListener = Gdx.app.getApplicationListener();
Downcast it to FlappyDemo and then you can use data member of your FlappyDemo class.
FlappyDemo flappyDemo =(FlappyDemo) applicationListener;
flappyDemo.music.play();
Use in a single line
((FlappyDemo)Gdx.app.getApplicationListener()).music;
Related
Recently I started making my "first" real RPG (The other 'RPG's' were text based in batch or whatever). I am using Libgdx, since from what I read it's pretty simple to get the hang of.
But what I'm interested in is am I doing it right?
For example I make a TiledMap and draw the camera etc. everything 'semi works', but is it right? On one tutorial there was a person which created thread's and much more, so it's making me think I'm doing it wrong
For example this is my GameScreen
public class GameScreen implements Screen, Runnable{
private MenuScreen menuScreen;
SpriteBatch batch, infobatch;
BitmapFont font;
private int tileWidth, tileHeight,
mapWidthInTiles, mapHeightInTiles,
mapWidthInPixels, mapHeightInPixels;
private float delta;
private InputHandler inputHandler;
private TiledMap map;
private AssetManager manager;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
private FitViewport playerViewport;
private Player player;
public GameScreen(MenuScreen mc)
{
menuScreen=mc;
}
#Override
public void show() {
batch = new SpriteBatch();
infobatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.local("font/ornlan.fnt"));
font.setColor(Color.BLACK);
manager = new AssetManager();
manager.setLoader(TiledMap.class, new TmxMapLoader());
manager.load("world/test_1.tmx", TiledMap.class);
manager.finishLoading();
map = manager.get("world/test_1.tmx", TiledMap.class);
MapProperties properties = map.getProperties();
tileWidth = properties.get("tilewidth", Integer.class);
tileHeight = properties.get("tileheight", Integer.class);
mapWidthInTiles = properties.get("width", Integer.class);
mapHeightInTiles = properties.get("height", Integer.class);
mapWidthInPixels = mapWidthInTiles * tileWidth;
mapHeightInPixels = mapHeightInTiles * tileHeight;
camera = new OrthographicCamera(960.f, 720.f);
playerViewport = new FitViewport(mapWidthInTiles, mapHeightInTiles, camera);
camera.position.x = MathUtils.clamp(camera.position.x, camera.viewportWidth / 2, mapWidthInPixels - camera.viewportWidth /2);
camera.position.y = MathUtils.clamp(camera.position.y, camera.viewportHeight / 2, mapHeightInPixels - camera.viewportHeight / 2);
renderer = new OrthogonalTiledMapRenderer(map);
player = new Player();
inputHandler = new InputHandler();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0.75f, 0.75f, 0.85f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
camera.position.set(Player.x + Player.width / 2, Player.y + Player.height / 2, 0);
camera.update();
renderer.setView(camera);
renderer.render();
infobatch.begin();
font.draw(infobatch, "Test World, Dev. purposes", 10,25);
font.draw(infobatch, "FPS: " + Gdx.graphics.getFramesPerSecond(), 1180, 700);
infobatch.end();
batch.begin();
batch.setProjectionMatrix(camera.combined);
delta = Gdx.graphics.getDeltaTime();
inputHandler.update();
player.update(delta);
player.render(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
batch.dispose();
infobatch.dispose();
font.dispose();
renderer.dispose();
manager.dispose();
}
#Override
public void run() {
// TODO Auto-generated method stub
}
}
And this is my "MainMenu"
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL30;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.TextureRegionDrawable;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class MenuScreen extends Game {
SpriteBatch batch;
BitmapFont font;
private Stage stage;
private GameScreen gameScreen;
private Texture btn_start;
private TextureRegion btn_startRegion;
private TextureRegionDrawable btn_startDrawable;
private ImageButton btn_startIB;
#Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.local("font/ornlan.fnt"));
font.setColor(Color.BLACK);
btn_start = new Texture(Gdx.files.internal("ui/start_btn.png"));
btn_startRegion = new TextureRegion(btn_start);
btn_startDrawable = new TextureRegionDrawable(btn_startRegion);
btn_startIB = new ImageButton(btn_startDrawable);
btn_startIB.setPosition(10, 250);
stage = new Stage(new ScreenViewport());
stage.addActor(btn_startIB);
Gdx.input.setInputProcessor(stage);
btn_startIB.addCaptureListener(new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
try {
System.out.println("Starting GameScreen...");
setGameScreen();
} catch (Exception e) {
System.out.println("Error starting GameScreen E:" + e);
}
};
});
}
#Override
public void render() {
Gdx.gl.glClearColor(0.67f, 0.75f, 0.98f, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
font.getData().setScale(0.8f,0.8f);
font.draw(batch, "Ornlan DevBuild (pa) v0.0.0.1", 1000, 25);
batch.end();
stage.act(Gdx.graphics.getDeltaTime()); // Perform ui logic
stage.draw();
super.render();
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
super.dispose();
}
void setGameScreen() {
gameScreen = new GameScreen(this);
setScreen(gameScreen);
}
}
Contact Collision box2D wont run on collision i want the bullet to be able to run the WorldContactListener beginContact when it begins contact and when it ends contact runningendContact
iv'e looked through a lot of places and i cant get a system print
This is my contact listener class:
package com.mygdx.game;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Manifold;
public class WorldContactListener implements ContactListener {
#Override
public void beginContact(Contact contact) {
//called when 2 fixtures collide
System.out.println("Begin Contact");
}
#Override
public void endContact(Contact contact) {
//called when the 2 fixtures connected gets split apart
System.out.println("end Contact");
}
#Override
public void preSolve(Contact contact, Manifold oldManifold) {
//gives power to change the characteristics of fixture
collision
}
#Override
public void postSolve(Contact contact, ContactImpulse impulse) {
//gives results of what happened because of collision like
angles ext
}
}
play screen class:
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
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.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
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.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
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;
import com.mygdx.game.Sprites.Bullet;
import com.mygdx.game.Sprites.InversePlayer;
import com.mygdx.game.Sprites.Player;
#SuppressWarnings("unused")
public class PlayScreen implements Screen {
private main game;
private TextureAtlas atlas;
private OrthographicCamera gamecam;
private Viewport gamePort;
private Hud hud;
private TmxMapLoader maploader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private Player player;
private InversePlayer inversePlayer;
private Bullet bullet;
public PlayScreen(main game) {
atlas = new TextureAtlas("BurningShooterPlayer.pack");
this.game = game;
gamecam = new OrthographicCamera();
gamePort = new FitViewport(main.V_WIDTH / main.PPM, main.V_HEIGHT / main.PPM, gamecam);
hud = new Hud(game.batch);
maploader = new TmxMapLoader();
map = maploader.load("map1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / main.PPM);
gamecam.position.set(gamePort.getWorldWidth()/2, gamePort.getWorldHeight()/2, 0);
world = new World(new Vector2(0,-10), true);
b2dr = new Box2DDebugRenderer();
new B2WorldCreator(this);
player = new Player(this);
inversePlayer = new InversePlayer(this, .32f, .32f);
bullet = new Bullet(this, .64f, .64f);
}
public TextureAtlas getAtlas() {
return atlas;
}
#Override
public void show() {
//world.setContactListener(ContactListener listener) .
}
public void handleInput(float dt) {
if(Gdx.input.isKeyJustPressed(Input.Keys.W))
player.b2body.applyLinearImpulse(new Vector2(0, 4f), player.b2body.getWorldCenter(), true);
if(Gdx.input.isKeyPressed(Input.Keys.D) && player.b2body.getLinearVelocity().x <= 2)
player.b2body.applyLinearImpulse(new Vector2(0.1f , 0), player.b2body.getWorldCenter(), true);
if(Gdx.input.isKeyPressed(Input.Keys.A) && player.b2body.getLinearVelocity().x >= -2)
player.b2body.applyLinearImpulse(new Vector2(-0.1f , 0), player.b2body.getWorldCenter(), true);
if(Gdx.input.isKeyJustPressed(Input.Keys.RIGHT)) {
bullet.b2body.setLinearVelocity(0, 0);
bullet.b2body.setTransform(new Vector2((float)(player.b2body.getPosition().x+player.getWidth()-(3/main.PPM)), (float)(player.b2body.getPosition().y)), 0);
bullet.b2body.applyLinearImpulse(new Vector2(Bullet.BULLET_SPEED, 0), bullet.b2body.getWorldCenter(), true);
Bullet.Right = true;
}
if(Gdx.input.isKeyJustPressed(Input.Keys.LEFT)) {
bullet.b2body.setLinearVelocity(0, 0);
bullet.b2body.setTransform(new Vector2((float)(player.b2body.getPosition().x-player.getWidth()+(3/main.PPM)), (float)(player.b2body.getPosition().y)), 0);
bullet.b2body.applyLinearImpulse(new Vector2(-Bullet.BULLET_SPEED, 0), bullet.b2body.getWorldCenter(), true);
Bullet.Right = false;
}
//if(Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
//bullet.b2body.setBullet(true);
//world.destroyBody(bullet.b2body);
//}
}
public void update(float dt) {
handleInput(dt);
world.step(1/60f, 6, 2);
player.update(dt);
inversePlayer.update(dt);
bullet.update(dt);
gamecam.position.x = player.b2body.getPosition().x;
gamecam.update();
renderer.setView(gamecam);
}
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gamecam.combined);
game.batch.setProjectionMatrix(gamecam.combined);
game.batch.begin();
player.draw(game.batch);
inversePlayer.draw(game.batch);
bullet.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, width);
}
public TiledMap getMap() {
return map;
}
public World getWorld() {
return world;
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
map.dispose();
renderer.dispose();
world.dispose();
b2dr.dispose();
hud.dispose();
}
}
Bullet code (i want to detect if it collides with another body):
package com.mygdx.game.Sprites;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.utils.Array;
import com.mygdx.game.PlayScreen;
import com.mygdx.game.main;
public class Bullet extends projectile{
private float stateTime;
private Animation<TextureRegion> walkAnimation;
private Array<TextureRegion> frames;
public static float BULLET_SPEED = 1f;
public static boolean Right = true;
public Bullet(PlayScreen screen, float x, float y) {
super(screen, x, y);
frames = new Array<TextureRegion>();
frames.add(new TextureRegion(screen.getAtlas().findRegion("BurningShooterPlayer"),111, -1, 15, 8));
walkAnimation = new Animation<TextureRegion>(0.1f, frames);
stateTime = 0;
setBounds(getX(), getY(), (float) (7.5/ main.PPM), 4 / main.PPM);
}
public void update(float dt) {
stateTime += dt;
setPosition((b2body.getPosition().x - getWidth() / 2), b2body.getPosition().y - getHeight() / 2);
setRegion(walkAnimation.getKeyFrame(stateTime, true));
if((!Right) && !walkAnimation.getKeyFrame(dt).isFlipX()) {
walkAnimation.getKeyFrame(dt).flip(true, false);
}
else if((Right) && walkAnimation.getKeyFrame(dt).isFlipX()) {
walkAnimation.getKeyFrame(dt).flip(true, false);
}
}
#Override
protected void defineProjectile() {
BodyDef bdef = new BodyDef();
bdef.position.set(64 / main.PPM, 64 / main.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
shape.setAsBox((float) (7.5 / 2 / main.PPM), 4 / 2 / main.PPM);
fdef.filter.categoryBits = main.ENEMY_BIT;
fdef.filter.maskBits = main.GROUND_BIT |
main.ENEMY_BIT |
main.OBJECT_BIT;
fdef.shape = shape;
fdef.density = 100;
b2body.setBullet(true);
b2body.createFixture(fdef);
b2body.setUserData(this);
}
}
You create the World but you forget to set your ContactListener to your world:
private World world;
private WorldContactListener worldContactListener;
public PlayScreen(main game) {
...
world = new World(new Vector2(0,-10), true);
worldContactListener = new WorldContactListener();
world.setContactListener(worldContactListener);
...
}
I was not sure how to title this article. I am making a game that has sprite falling down. I already have triangles falling down in my game. I don't know how to change it where images that I import in the game that allows the image falling.
Icicle:
package com.udacity.gamedev.icicles;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
public class Icicle {
public static final String TAG = Icicle.class.getName();
Vector2 position;
Vector2 velocity;
public Icicle(Vector2 position) {
this.position = position;
this.velocity = new Vector2();
}
public void update(float delta) {
velocity.mulAdd(Constants.ICICLES_ACCELERATION, delta);
position.mulAdd(velocity, delta);
}
public void render(ShapeRenderer renderer) {
renderer.triangle(
position.x, position.y,
position.x - Constants.ICICLES_WIDTH / 2, position.y + Constants.ICICLES_HEIGHT,
position.x + Constants.ICICLES_WIDTH / 2, position.y + Constants.ICICLES_HEIGHT
);
}
}
Icicles:
package com.udacity.gamedev.icicles;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.utils.DelayedRemovalArray;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.udacity.gamedev.icicles.Constants.Difficulty;
public class Icicles {
public static final String TAG = Icicles.class.getName();
Difficulty difficulty;
int iciclesDodged;
DelayedRemovalArray<Icicle> icicleList;
Viewport viewport;
public Icicles(Viewport viewport, Difficulty difficulty) {
this.difficulty = difficulty;
this.viewport = viewport;
init();
}
public void init() {
icicleList = new DelayedRemovalArray<Icicle>(false, 100);
iciclesDodged = 0;
}
public void update(float delta) {
if (MathUtils.random() < delta * difficulty.spawnRate) {
Vector2 newIciclePosition = new Vector2(
MathUtils.random() * viewport.getWorldWidth(),
viewport.getWorldHeight()
);
Icicle newIcicle = new Icicle(newIciclePosition);
icicleList.add(newIcicle);
}
for (Icicle icicle : icicleList) {
icicle.update(delta);
}
icicleList.begin();
for (int i = 0; i < icicleList.size; i++) {
if (icicleList.get(i).position.y < -Constants.ICICLES_HEIGHT) {
iciclesDodged += 1;
icicleList.removeIndex(i);
}
}
icicleList.end();
}
public void render(ShapeRenderer renderer) {
renderer.setColor(Constants.ICICLE_COLOR);
for (Icicle icicle : icicleList) {
icicle.render(renderer);
}
}
}
Constant:
package com.udacity.gamedev.icicles;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;
public static final float ICICLES_HEIGHT = 1.0f;
public static final float ICICLES_WIDTH = 0.5f;
public static final Vector2 ICICLES_ACCELERATION = new Vector2(0,-5.0f);
public static final Color ICICLE_COLOR = Color.WHITE;
I am trying to create a Space Shooter game using LibGDX in Android Studio.
Everything else was working fine until I tried to create animation using javafx.animation.Animation class. While compiling my code, I get the following 3 errors and 1 warning !
Warning:[options] bootstrap class path not set in conjunction with -source 1.6
Error:(11, 24) error: package javafx.animation does not exist
Error:(22, 13) error: cannot find symbol class Animation
Error:(44, 26) error: cannot find symbol class Animation
I have checked that my android studio does have a plugin for javafx !
I have two classes in my code Shootergame.java and AnimatedSprite.java
The problem lies in AnimatedSprite.java class and the code for that class is below !
package com.ahmed.nullapointershooter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import javafx.animation.Animation;
/**
* Created by Hafeez ur Rehman on 8/30/2015.
**/
public class AnimatedSprite {
private static final int FRAMES_COL = 2;
private static final int FRAMES_ROW = 2;
private Sprite sprite;
private Animation animation;
private TextureRegion[] frames;
private TextureRegion currentFrame;
private float stateTime;
public AnimatedSprite(Sprite sprite) {
this.sprite = sprite;
Texture texture = sprite.getTexture();
TextureRegion[][] temp = TextureRegion.split(texture, texture.getWidth() / FRAMES_COL,
texture.getHeight() / FRAMES_ROW);
frames = new TextureRegion[FRAMES_COL * FRAMES_ROW];
int index = 0;
for (int i = 0; i < FRAMES_ROW; i++) {
for (int j = 0; j < FRAMES_COL; j++) {
frames[index++] = temp[i][j];
}
}
/**
* THE PROBLEM LIES HERE . I WANT TO DO THIS ----
*
*/
//animation = new Animation(0.1f, frames);
animation = new Animation() {
#Override
public void impl_playTo(long l, long l1) {
}
#Override
public void impl_jumpTo(long l, long l1) {
}
};
stateTime = 0f;
}
public void setPosition(float x, float y){
float widthOffset = sprite.getWidth() / FRAMES_COL;
sprite.setPosition(x - widthOffset / 2, y);
}
public void draw(SpriteBatch spriteBatch){
stateTime += Gdx.graphics.getDeltaTime();
//currentFrame = animation.getKeyFrame(stateTime, true);
spriteBatch.draw(currentFrame, sprite.getX(), sprite.getY());
}
}
and also code for Shootergame.java just for reference for what I am doing!
package com.ahmed.nullapointershooter;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class ShooterGame extends ApplicationAdapter {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture background;
private AnimatedSprite spaceshipAnimated;
#Override
public void create() {
//Texture.setEnforcePotImages(false);
camera = new OrthographicCamera();
camera.setToOrtho(false, 800, 480);
batch = new SpriteBatch();
background = new Texture(Gdx.files.internal("NullapointerBackground.png"));
Texture spaceshipTexture = new Texture(Gdx.files.internal("Spaceshipcanvas.png"));
Sprite spaceshipSprite = new Sprite(spaceshipTexture);
spaceshipAnimated = new AnimatedSprite(spaceshipSprite);
spaceshipAnimated.setPosition(800 / 2, 0);
}
#Override
public void dispose() {
batch.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(background, 0, 0);
spaceshipAnimated.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
}
}
My problem solved . I was using Javafx to import animation class which was wrong at the first place because there is no javafx package in android. Instead I had to import
import com.badlogic.gdx.graphics.g2d.Animation; Now its working !
I am trying to alter a pixmap and render it, but modified pixels are not shown on screen. I'm not sure if a Pixmap is the best way to do it. Can anyone explain to me where my errors are in the code below ? thanks
package com.me.mygdxgame;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Pixmap _pixmap;
private int _width;
private int _height;
private Texture _pixmapTexture;
private Sprite _pixmapSprite;
private int _x = 0;
private int _y = 0;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
_width = (int)Math.round(w);
_height = (int)Math.round(h);
_pixmap = new Pixmap( _width, _height, Format.RGBA8888 );
_pixmap.setColor(Color.RED);
_pixmap.fillRectangle(0, 0, _width, _height);
_pixmapTexture = new Texture(_pixmap, Format.RGB888, false);
}
#Override
public void dispose() {
batch.dispose();
_pixmap.dispose();
_pixmapTexture.dispose();
}
#Override
public void render() {
updatePixMap();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(_pixmapTexture, -_width/2, -_height/2);
batch.end();
}
private void updatePixMap() {
_x += 1;
if (_x >= _width) {
_x = 0;
}
_y += 1;
if (_y >= _height / 2) {
return;
}
_pixmap = new Pixmap( _width, _height, Format.RGBA8888 );
_pixmap.setColor(Color.CYAN);
_pixmap.drawPixel(_x, _y);
_pixmapTexture = new Texture(_pixmap, Format.RGB888, false);
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
You are creating a new pixmap every loop and you don't draw the complete texture in your view.
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.MathUtils;
public class MyGdxGame implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Pixmap _pixmap;
private Texture _pixmapTexture;
private int _x = 0;
private int _y = 0;
private float _w;
private float _h;
private int _width;
private int _height;
#Override
public void create() {
_w = Gdx.graphics.getWidth();
_h = Gdx.graphics.getHeight();
_width = MathUtils.round(_w);
_height = MathUtils.round(_h);
camera = new OrthographicCamera(1f, _h / _w);
camera.setToOrtho(false);
batch = new SpriteBatch();
_pixmap = new Pixmap(_width, _height, Format.RGBA8888);
_pixmap.setColor(Color.RED);
_pixmap.fillRectangle(0, 0, _width, _height);
_pixmapTexture = new Texture(_pixmap, Format.RGB888, false);
}
#Override
public void dispose() {
batch.dispose();
_pixmap.dispose();
_pixmapTexture.dispose();
}
#Override
public void pause() {
}
#Override
public void render() {
updatePixMap();
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(_pixmapTexture, 1f / 2f, _h / _w / 2f);
batch.end();
}
#Override
public void resize(final int width, final int height) {
}
#Override
public void resume() {
}
private void updatePixMap() {
_x += 1;
if (_x >= _width) _x = 0;
_y += 1;
if (_y >= _height / 2) return;
_pixmap.setColor(Color.CYAN);
_pixmap.drawPixel(_x, _y);
_pixmapTexture = new Texture(_pixmap, Format.RGB888, false);
}
}
But this is very slow, so why do you want to do it?