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;
Related
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;
I am following a video tutorial on Youtube called "Making Simple Sidescroller with Overlap2D and libGDX - Tutorial - 003".
I see on video he use raycast to detect collision. But when i try to follow, the player fall through the ground while in the video the player can stop on the ground.
Here is the link to download my libgdx project, and my overlap2d project for you to check.
Here is the code of Player.java
package com.test.superninja;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.uwsoft.editor.renderer.scripts.IScript;
import com.uwsoft.editor.renderer.components.TransformComponent;
import com.uwsoft.editor.renderer.components.DimensionsComponent;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
import com.uwsoft.editor.renderer.physics.PhysicsBodyLoader;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.RayCastCallback;
import com.badlogic.gdx.physics.box2d.World;
public class Player implements IScript {
private Entity player;
private TransformComponent transformComponent;
private DimensionsComponent dimensionsComponent;
private Vector2 speed;
private float gravity = -500f;
private float jumpSpeed = 170f;
private World world;
public Player(World world) {
this.world = world;
}
#Override
public void init(Entity entity) {
player = entity;
transformComponent = ComponentRetriever.get(entity, TransformComponent.class);
dimensionsComponent = ComponentRetriever.get(entity, DimensionsComponent.class);
speed = new Vector2(50, 0);
}
#Override
public void act(float delta) {
//transformComponent.scaleY = 0.5;
if (Gdx.input.isKeyPressed(Input.Keys.LEFT)) {
transformComponent.x -= speed.x * delta;
transformComponent.scaleX = -1f;
}
if (Gdx.input.isKeyPressed(Input.Keys.RIGHT)) {
transformComponent.x += speed.x * delta;
transformComponent.scaleX = 1f;
}
if (Gdx.input.isKeyJustPressed(Input.Keys.SPACE)) {
speed.y = jumpSpeed;
}
speed.y += gravity * delta;
transformComponent.y += speed.y * delta;
/*
if (transformComponent.y < 16f){
speed.y = 0;
transformComponent.y = 16f;
}
*/
}
private void rayCast() {
float rayGap = dimensionsComponent.height / 2;
// Ray size is the exact size of the deltaY change we plan for this frame
float raySize = -(speed.y) * Gdx.graphics.getDeltaTime();
//if(raySize < 5f) raySize = 5f;
// only check for collisions when moving down
if (speed.y > 0) return;
// Vectors of ray from middle middle
Vector2 rayFrom = new Vector2((transformComponent.x + dimensionsComponent.width / 2) * PhysicsBodyLoader.getScale(), (transformComponent.y + rayGap) * PhysicsBodyLoader.getScale());
Vector2 rayTo = new Vector2((transformComponent.x + dimensionsComponent.width / 2) * PhysicsBodyLoader.getScale(), (transformComponent.y - raySize) * PhysicsBodyLoader.getScale());
// Cast the ray
world.rayCast(new RayCastCallback() {
#Override
public float reportRayFixture(Fixture fixture, Vector2 point, Vector2 normal, float fraction) {
// Stop the player
speed.y = 0;
// reposition player slightly upper the collision point
transformComponent.y = point.y / PhysicsBodyLoader.getScale() + 0.1f;
return 0;
}
}, rayFrom, rayTo);
}
public float getX() {
return transformComponent.x;
}
public float getY() {
return transformComponent.y;
}
public float getWidth() {
return dimensionsComponent.width;
}
#Override
public void dispose() {
}
}
Here is SuperNinja.java
package com.test.superninja;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.uwsoft.editor.renderer.SceneLoader;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.uwsoft.editor.renderer.utils.ItemWrapper;
import com.uwsoft.editor.renderer.components.additional.ButtonComponent;
import com.badlogic.gdx.graphics.OrthographicCamera;
public class SuperNinja extends ApplicationAdapter {
private SceneLoader sceneLoader;
private Viewport viewport;
private ItemWrapper root;
private Player player;
private UIStage uiStage;
#Override
public void create () {
viewport = new FitViewport(266, 160);
sceneLoader = new SceneLoader();
sceneLoader.loadScene("MainScene", viewport);
root = new ItemWrapper(sceneLoader.getRoot());
player = new Player(sceneLoader.world);
root.getChild("player").addScript(player);
uiStage = new UIStage(sceneLoader.getRm());
}
#Override
public void render () {
Gdx.gl.glClearColor(0.5f, 0.5f, 0.5f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
sceneLoader.getEngine().update(Gdx.graphics.getDeltaTime());
uiStage.act();
uiStage.draw();
((OrthographicCamera)viewport.getCamera()).position.x=player.getX()+player.getWidth()/2f;
}
}
Here is UIStage.java
package com.uwsoft.platformer;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.uwsoft.editor.renderer.data.CompositeItemVO;
import com.uwsoft.editor.renderer.data.ProjectInfoVO;
import com.uwsoft.editor.renderer.resources.IResourceRetriever;
import com.uwsoft.editor.renderer.scene2d.CompositeActor;
/**
* Created by azakhary on 8/5/2015.
*/
public class UIStage extends Stage {
public UIStage(IResourceRetriever ir) {
Gdx.input.setInputProcessor(this);
ProjectInfoVO projectInfo = ir.getProjectVO();
CompositeItemVO menuButtonData = projectInfo.libraryItems.get("menuButton");
CompositeActor buttonActor = new CompositeActor(menuButtonData, ir);
addActor(buttonActor);
buttonActor.setX(getWidth() - buttonActor.getWidth());
buttonActor.setY(getHeight() - buttonActor.getHeight());
buttonActor.addListener(new ClickListener() {
#Override
public void clicked (InputEvent event, float x, float y) {
System.out.println("Hi");
}
});
}
}
With the help of Xeon, I solved my question. I just forgot to call rayCast() function.
I call raycast() function like this:
#Override
public void act(float delta) {
...
rayCast();
}
And it works. Thank you Xeon very much.
My game has a player who is constantly moving up. I want it so when he hits the bottom of a block, you lose, and if he hits a block on the side, he just bounces back. There is no physics involved. For whatever reason, the collision detection just isn't working as it as supposed to. For example, in the code below I am resetting the position every time the player hits the bottom. However, it always resets before he even hits the bottom of the tile. Why is this happening and how can I fix it?
Below is my code for the player (GameIcon),:
package com.xx4everPixelatedxx.gaterunner.sprites;
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.maps.MapObject;
import com.badlogic.gdx.maps.objects.RectangleMapObject;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.math.Intersector;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.xx4everPixelatedxx.gaterunner.GateRunner;
import com.xx4everPixelatedxx.gaterunner.gameObjects.Block;
import com.xx4everPixelatedxx.gaterunner.gameObjects.Spike;
import javax.xml.soap.Text;
/**
* Created by Michael Jan on 8/17/2015.
*/
public class GameIcon extends Sprite {
private float vX = 3;
private float vY = 3;
private float r = 9;
private Texture texture;
public GameIcon(int x, int y) {
super(new Texture(Gdx.files.internal("icon_players/icon1.png")));
setPosition(x, y);
// texture = new Texture(Gdx.files.internal("icon_players/icon1.png"));
// setTexture(texture);
}
public void update() {
addPosition(vX, vY);
setRotation( (getRotation() + r) % 360);
setOriginCenter();
}
public void update(TiledMap map) {
addPosition(vX, vY);
setRotation((getRotation() + r) % 360);
setOriginCenter();
//block
for(MapObject object : map.getLayers().get(2).getObjects().getByType(RectangleMapObject.class))
{
Rectangle rect = ((RectangleMapObject)object).getRectangle();
if(Intersector.overlaps(getBoundingRectangle(), rect))
{
setPosition(GateRunner.WIDTH/2 - GateRunner.WIDTH/20, GateRunner.HEIGHT/10);
if(getY() <= rect.getY() - getHeight() + vX)
{
System.out.println("bottom");
setPosition(GateRunner.WIDTH/2 - GateRunner.WIDTH/20, GateRunner.HEIGHT/10);
}
else
{
System.out.println("side");
negateVelocityX();
negateRotation();
}
}
}
//spike
for(MapObject object : map.getLayers().get(3).getObjects().getByType(RectangleMapObject.class))
{
Rectangle rect = ((RectangleMapObject)object).getRectangle();
if(rect.overlaps(getBoundingRectangle()))
{
}
}
}
public void addPosition(float x, float y) {
setPosition(getX() + x, getY() + y);
setOriginCenter();
}
public void negateVelocityX() {
if(vX < 0)
{
addPosition((int)(getWidth()*0.05), 0);
}
if(vX > 0)
{
addPosition(-(int)(getWidth()*0.05), 0);
}
vX = -vX;
}
public void negateRotation() {
r = -r;
}
public float getvX() {
return vX;
}
public void setvX(int vX) {
this.vX = vX;
}
public float getvY() {
return vY;
}
public void setvY(int vY) {
this.vY = vY;
}
public float getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
}
I'm using libGDX to create a game like curve fever but for Android.
Actually I'm working on the collision but everything I try won't work; I use the overlaps methods from the Rectangle class.
It always returns false.
Here's the code which checks if there's a collision:
public void collision() {
if(head.getAlive()) {
for (TailSnake tail : getTail()) {
if (tail.lifeTime > 2)
if (head.bounds.overlaps(tail.bounds))
head.dead();
}
for (Object wallObj : getWall()) {
Wall wall = (Wall) wallObj;
if (head.bounds.overlaps(wall.bounds))
head.dead();
}
}
}
The TailSnake's code:
package com.me.mygdxgame;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class TailSnake {
static final float SIZE = 0.1f;
Vector2 position = new Vector2();
Rectangle bounds = new Rectangle();
float lifeTime = 0;
public TailSnake(Vector2 position) {
this.position = position;
this.bounds.height = SIZE;
this.bounds.width = SIZE;
}
public Rectangle getBounds() {
return bounds;
}
public Vector2 getPosition() {
return position;
}
public void update(float delta) {
lifeTime += delta;
}
}
And finally the code of HeadSnake:
package com.me.mygdxgame;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
public class HeadSnake extends TailSnake{
static final float SPEED = 1f;
static final double ROTATE_SPEED = 200;
Rectangle bounds = new Rectangle();
Vector2 velocity = new Vector2();
boolean alive = true;
public HeadSnake(Vector2 position) {
super(position);
this.velocity.x = 0;
this.velocity.y = SPEED;
}
public Vector2 getVelocity() {
return velocity;
}
public boolean getAlive() {
return alive;
}
public void update(float delta) {
if (alive)
position.add(velocity.cpy().scl(delta));
}
public void dead() {
this.alive = false;
}
}
You are not updating the bounds. In your update method, after you set position, update the bounds as well. Something like-
bounds.x = position.x;
bounds.y = position.y;
bounds.x -= bounds.width / 2f;
bounds.y -= bounds.height / 2f;
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?