Rotation of sprite, libgdx android - java

I want my sprite which is a basketball to rotate. How can i make my sprite to rotate counter-clockwise based on delta time? Do you have a calculation for it?

call rotateSprite() method whenever you want to rotate your ball.
Take a look of my Test code.
public class TestGame extends Game {
SpriteBatch spriteBatch;
Sprite ball;
Texture ballTex;
private int spriteRotationSpeed=1;
#Override
public void create() {
spriteBatch=new SpriteBatch();
ballTex=new Texture("image/bone.png");
ball=new Sprite(ballTex);
ball.setSize(50,50);
ball.setOrigin(25,25);
ball.setPosition(0,0);
}
#Override
public void render() {
super.render();
Gdx.gl.glClearColor(1,1,1,1);
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
ball.draw(spriteBatch);
spriteBatch.end();
rotateSprite();
}
private void rotateSprite(){
float rotation=ball.getRotation();
rotation+=spriteRotationSpeed;
if(rotation>360)
rotation-=360;
ball.setRotation(rotation);
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void dispose() {
super.dispose();
ballTex.dispose();
spriteBatch.dispose();
}
}

Related

Create monster and set position of tiledMap #java #libGDX

I've a problem with setting my monster on the map. Firstly I create a Knight with camera coordinates. Now I want to set a monster on the map independent of the camera coordinates, so that when I am moving the player using keys the monster stays at the one position. I tried to implemets this and all I got was that the monster stayed at the bottom left corner of the screen all the time. Here is my Person class
public abstract class Person implements Stats {
public Person(String pathToFile,Vector2 position) {
...
}
public void update(float delta) {
spriteBatch.begin();
sprite.draw(spriteBatch);
spriteBatch.end();
}
And my bot class
public class Bot extends Person {
public Bot() {
super(toFilePath,new Vector2(500,550));
super.position.set(500,550);
}
#Override
public void update(float delta) {
super.update(delta);
}
#Override
public void dispose() {
super.dispose();
}
}
The Knight class
public class Knight extends Person {
public Knight(OrthographicCamera camera) {
super(toFilePath, new Vector2(MapScreen.startPositionX, MapScreen.startPositionY));
super.sprite.setCenter(camera.viewportWidth / 2, camera.viewportHeight / 2);
this.camera = camera;
// animation
...
}
public void update(float delta, MapScreen mapScreen) {
camera.update();
walkBatch.begin();
// input handling
walkBatch.end();
}
#Override
public void dispose() {
...
}
And the class where I set up all classes
public class MapScreen implements Screen {
...
#Override
public void show() {
init(startPositionX, startPositionY);
}
// initialize variable
private void init(float posX, float posY) {
camera = new OrthographicCamera();
camera.setToOrtho(false, width, height);
tiledMap = new TmxMapLoader(new ExternalFileHandleResolver()).load(mapName);
setTiledMapRenderer(new OrthogonalTiledMapRenderer(tiledMap));
knight = new Knight(camera);
camera.zoom = ZOOM;
camera.position.set(posX, posY, 0);
camera.update();
}
#Override
public void render(float delta) {
camera.update();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
getTiledMapRenderer().setView(camera);
getTiledMapRenderer().render(layerBottom);
knight.update(delta, this);
getTiledMapRenderer().render(layerTop);
}
When using a SpriteBatch (which you probably don't use at all, when looking at your code), then you can use your camera's matrix to calculate the offsets correctly.
use it like:
spriteBatch.setProjectionMatrix(camera.combined);
spriteBatch.begin();
// draw your sprites here
spriteBatch.end();
You should then also consider improve performances by not rendering sprites off the screen:
Consider implementing some of the tips
I find a solution. I created a object layer in Tiled and there I set my monsters. Then in MapScreen I render it. Thanks for your help.

Clickable textures

I'm kind of new to LibGdx and android studio.
I'm trying to create clickable textures, one for play and one for credits.
Both should be opening a new empty screen/event.
public void create() {
batch = new SpriteBatch();
img = new Texture("Main_Screen.png");
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
credits = new Texture("credits.png");
play = new Texture("play.png");
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(img, 0, 0);
batch.draw(play, 340, 1400);
batch.draw(credits, 340, 400);
batch.end();
}
However i'm unsure on how to do this since i'm also creating the background with a texture, so i'd be very happy if someone could assist me with helping me out.
Use Sprite instead of Texture, that holds the geometry, color, and texture information for drawing 2D sprites.
MyGdxGame
public class MyGdxGame extends Game {
public Screen menuScreen,creditsScreen,playScreen;
#Override
public void create () {
menuScreen=new MenuScreen(this);
creditsScreen=new CreditsScreen();
playScreen=new PlayScreen();
setScreen(menuScreen);
}
}
MenuScreen
public class MenuScreen extends InputAdapter implements Screen {
SpriteBatch batch;
Texture background,play,credits;
Sprite backgoundSprite,playSprite,creditsSprite;
private ExtendViewport extendViewport;
OrthographicCamera cam;
private float w=480;
private float h=800;
private Vector3 vector3;
MyGdxGame game;
Music music;
public MenuScreen(MyGdxGame game){
this.game=game;
}
#Override
public void show() {
batch = new SpriteBatch();
cam = new OrthographicCamera();
extendViewport=new ExtendViewport(w,h,cam);
vector3=new Vector3();
background = new Texture("Main_Screen.png");
play=new Texture("play.png");
credits=new Texture("credits.png");
backgoundSprite=new Sprite(background);
backgoundSprite.setSize(w,h); // If resources are not in context of your viewport
backgoundSprite.setPosition(0,0); //Default Position
playSprite=new Sprite(play);
playSprite.setSize(100,100);
playSprite.setPosition(w/2-playSprite.getWidth()/2,h/2+100);
creditsSprite=new Sprite(credits);
creditsSprite.setSize(100,100);
creditsSprite.setPosition(w/2-creditsSprite.getWidth()/2,h/2-100);
Gdx.input.setInputProcessor(this);
music = Gdx.audio.newMusic(Gdx.files.internal("bgmusic.wav"));
music.play();
music.setLooping(true);
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
backgoundSprite.draw(batch);
playSprite.draw(batch);
creditsSprite.draw(batch);
batch.end();
}
#Override
public void resize(int width, int height) {
extendViewport.update(width,height);
cam.position.x = w /2;
cam.position.y = h/2;
cam.update();
}
#Override
public boolean touchDown(int screenX, int screenY, int pointer, int button) {
vector3.set(screenX,screenY,0);
Vector3 position=cam.unproject(vector3);
if(playSprite.getBoundingRectangle().contains(position.x,position.y)) {
game.setScreen(game.playScreen);
}
if(creditsSprite.getBoundingRectangle().contains(position.x,position.y)){
game.setScreen(game.creditsScreen);
}
return super.touchDown(screenX, screenY, pointer, button);
}
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() {
batch.dispose();
background.dispose();
play.dispose();
credits.dispose();
}
}
CreditsScreen
public class CreditsScreen implements Screen {
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) { }
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() { }
}
PlayScreen
public class PlayScreen implements Screen {
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
#Override
public void resize(int width, int height) { }
#Override
public void pause() { }
#Override
public void resume() { }
#Override
public void hide() { }
#Override
public void dispose() { }
}
Welcome to libGDX!
If I understand your question correctly, you are trying to figure out how to change screens in your game. At a high level, this is what you'd need to do:
Draw your button texture on the screen.
Use Gdx.input to detect when the player lifts their mouse button (and if it's over one of your buttons when they do it)
Switch to the new screen (presumably with a new background) when this happens.
I would recommend that you first complete the "A Simple Game" and "Extending the Simple Game" tutorials in order to familiarize yourself with the basics of libGDX and the Game and Screen classes.
Next, try using Scene2D.UI (there are links at the bottom of the second tutorial) to add a Stage and TextButtons to your main menu.
Hopefully that helps you get started- there is a lot of helpful information on that libGDX wiki which I think you will find helpful.

Libgdx Switching Screens and Viewports

I am having an issue when switching screens in libgdx. I am building a asteroids game clone. So first my MainMenuScreen class (which uses a Fitviewport) is rendered and then I call setScreen() to GameScreen (GameScreen doesn't use a Fitviewport) and that works except that the second screen renders as if its using a Fitviewport. If I resize the second screen then the whole window is used for rendering. Why is this happening? Here are some pictures:
MainMenuScreen class:
GameScreen class after switching screens, the screen has black bars on the side (I colored the boundary in red for you) which I don't want:
I want the GameScreen to use all the window area for rendering, and not have black bars like the MainMenu. I am not using any Fitviewport in GameScreen also.
Here are relevant parts of my MainMenuScreen class:
public class MainMenuScreen implements Screen
{
private static final String TAG = "MainMenu";
private static final int VIRTUAL_WIDTH = 400;
private static final int VIRTUAL_HEIGHT = 400;
MyGdxGame game;
...
public MainMenuScreen(MyGdxGame game)
{
this.game = game;
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT);
stage = new Stage(viewport);
// Play button listener
btnPlay.addListener( new ClickListener() {
#Override
public void clicked(InputEvent event, float x, float y) {
Gdx.app.log(TAG, "PLAY");
MainMenuScreen.this.game.setScreen(MainMenuScreen.this.game.gameScreen);
};
});
....
}
Here is my GameScreen class:
MyGdxGame game;
OrthographicCamera guiCam;
World world;
WorldRenderer renderer;
public GameScreen(MyGdxGame game, SpriteBatch batch)
{
this.game = game;
guiCam = new OrthographicCamera(400, 400);
guiCam.position.set(400 / 2, 400 / 2, 0);
world = new World();
renderer = new WorldRenderer(game, batch, world);
}
public void draw()
{
GL20 gl = Gdx.gl;
gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
}
#Override
public void show()
{
world.createLevel();
}
#Override
public void render(float delta)
{
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()
{
}
The problem is this line of code:
public void resize(int width, int height)
{
viewport.update(width, height, true);
}
Whenever you update the viewport, you are also updating the underlying opengl viewport and this causes the black bars to persist to the second screen (https://github.com/libgdx/libgdx/wiki/Viewports). Thus if you want to reset it back to normal you must use the following line of code in your second screen show() method (https://github.com/libgdx/libgdx/wiki/Scene2d):
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

InputListener doesn't work with OrthographicCamera and Actors

I have a Stage with OrthographicCamera and also I have a Actor with InputListener setter by addListener (from actor class). The problem is that the actor doesn't proccess input, but if in my screen I delete OrthographicCamera the Actor proccess the input, so, with OrthographicCamera Actor doesn't proccess input but it works if I remove OrthographicCamera.
Any advice?
I have the following code
public class Test implements Screen {
private Game game;
private Stage stage;
private MemoryActor actor;
private AssetManager manager;
private boolean loaded = false;
float width, height;
private OrthographicCamera camera;
public Test(Game game){
this.game = game;
stage = new Stage();
manager = new AssetManager();
manager.load("img.png",Texture.class);
manager.load("img1.png",Texture.class);
InputMultiplexer im = new InputMultiplexer();
im.addProcessor(stage);
Gdx.input.setInputProcessor(im);
height = Gdx.graphics.getHeight();
width = Gdx.graphics.getWidth();
camera = new OrthographicCamera(width, height);
camera.position.set(((width / 2)), ((height / 2)), 0);
camera.update();
stage.setViewport(new ExtendViewport(300,300, camera));
}
public void createActor(){
Texture back = manager.get("img.png", Texture.class);
actor = new MemoryActor(manager.get("img1.png", Texture.class), back,0,0,50,50);
actor.setInputListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("down");
return true;
}
#Override
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
System.out.println("up");
}
});
stage.addActor(actor);
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
if (manager.update()){
if (!loaded){
createActor();
loaded = true;
}
}
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void show() {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
and MemoryActor:
public class MemoryActor extends Actor {
...
public MemoryActor(){}
public MemoryActor(Texture texture, Texture texBack, float x, float y, float width, float height){
...
}
public void setInputListener(InputListener il){
addListener(il);
}
#Override
public void draw(Batch batch, float alpha){
...
}
}
Just registering the stage as an InputProcessor isn't enough. You also need to trigger the event processing of Stage via stage.act() in every frame.
Furthermore you need to properly update the stage's Viewport when a resize event occurs. This can be done via stage.getViewport().update(width, height, true). Otherwise the stage will process the events based on incorrect assumptions about the screen size and might also render your stage not the way you want it. The true is important because it will also center the camera on the new screen size, which is necessary in case of UIs.

How we can remove the back texture in libGDX?

private Texture druidTexture;
private SpriteBatch batch;
Sprite sprite;
#Override
public void create() {
druidTexture = new Texture(Gdx.files.internal("star-large.gif"));
sprite = new Sprite(druidTexture);
batch = new SpriteBatch();
}
#Override
public void dispose() {
}
#Override
public void pause() {
}
#Override
public void render() {
batch.begin();
sprite.rotate(45);
float x=sprite.getX();
float y=sprite.getY();
float newx=x+1;
System.out.println(newx);
float newy=y+1;
sprite.setX(newx);
sprite.draw(batch);
batch.end();
}
#Override
public void resize(int arg0, int arg1) {
}
#Override
public void resume() {
}
}
i am getting output for the above code as this
but i need to remove the star at back,for each star,how can i do that??
You need to clear the screen before each frame as described here:
public void render () {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
batch.begin();
// Drawing goes here!
batch.end();
}
Clear the screen firstly in function render
#Override
public void render() {
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
.....
}

Categories

Resources