I'm getting this error while working on Android Studio with libGDX.
Here's my code:
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.Animation;
public class MyGdxGame extends ApplicationAdapter {
private SpriteBatch batch;
private TextureAtlas shooterAtlas;
private Animation animation;
private float timePassed = 0 ;
#Override
public void create () {
batch = new SpriteBatch();
shooterAtlas = new TextureAtlas(Gdx.files.internal("shooter.atlas"));
animation = new Animation(1/30f,shooterAtlas.getRegions());
}
#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.begin();
timePassed += Gdx.graphics.getDeltaTime();
batch.draw(animation.getKeyFrame(timePassed,true),300,500);
batch.end();
}
}
first i had this code:
import javafx.animation.animation
instead of:
import com.badlogic.gdx.graphics.g2d.Animation;
but it was giving me error on this:
animation = new Animation(1/30f,shooterAtlas.getRegions());
that:
animation is an abstract cannot be instantiated
But then I import this:
import com.badlogic.gdx.graphics.g2d.Animation;
and delete:
import javafx.animation.animation
because it was giving me error that you cant have both and resolving with alt+enter didn't worked also.
But on compilation it gives the error:
Warning:[options] bootstrap class path not set in conjunction with -source 1.6
C:\Users\COMPUTER\Documents\new libgdx\core\src\com\mygdx\game\MyGdxGame.java
1 warning
Error:Execution failed for task ':core:compileJava'.
Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 1.526 secs
Information:2 errors
Information:1 warning
Information:See complete output in console
The last error is because you are compiling your code with the previous jdk version and you have a newer one try to set source and target both to the jdk version you currently have.
Related
I have three objects using the same functions and variables: just different images. So I decided to move them into a parent class called Entity. They still can render the same images and stuff but suddenly, my Update() method which moves the objects using keypresses no longer works. I'm not very sure why that's the case because to me, the idea of parent class is to store things the child class would use.
my parent class
package com.mygdx.game;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Entity{
public Vector2 position;
public Sprite sprite;
public float speed;
public Entity(Texture img){
sprite = new Sprite(img);
position = new Vector2();
}
public void Update(float deltaTime){
if(Gdx.input.isKeyPressed(Input.Keys.A)) position.x-=deltaTime*speed;
if(Gdx.input.isKeyPressed(Input.Keys.D)) position.x+=deltaTime*speed;
if(Gdx.input.isKeyPressed(Input.Keys.S)) position.y-=deltaTime*speed;
if(Gdx.input.isKeyPressed(Input.Keys.W)) position.y+=deltaTime*speed;
}
public void Draw(SpriteBatch batch){
Update(Gdx.graphics.getDeltaTime());
sprite.setPosition(position.x, position.y);
sprite.draw(batch);
}
}
my child class
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
public class Object1 extends Entity{
public Object1(Texture img){
super(img);
super.position = new Vector2(Gdx.graphics.getWidth()/10,0);
}
}
I found out that the key presses work, but it can't seem to update the position of the image. I thought functions in parent classes are essentially functions in child classes so I don't know why it can't be updated.
I'm trying to code a plugin where the border follows a player and doubles everytime they get an advancement. The problem is I don't know how to detect when someone gets an advancement and keep it in the same class. Here is the current code.
survivalBorders.class
package sc458.survivalborders;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerAdvancementDoneEvent;
import org.bukkit.event.player.PlayerEvent;
import org.bukkit.plugin.java.JavaPlugin;
public class survivalBorders extends JavaPlugin {
public void onEnable() {
World world = Bukkit.getWorld("survival");
WorldBorder border = world.getWorldBorder();
double borderSize = border.getSize();
while(true) {
if(PlayerAdvancementDoneEvent) {
border.setSize(borderSize*2);
}
}
}
public void onDisable() {
}
}
MoveEvent.class
package sc458.survivalborders;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.WorldBorder;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerMoveEvent;
public class MoveEvent implements Listener {
public void onMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
World world = Bukkit.getWorld("survival");
WorldBorder border = world.getWorldBorder();
int locX = p.getLocation().getBlockX();
int locZ = p.getLocation().getBlockZ();
border.setCenter(locX, locZ);
}
}
Try to call the PlayerMoveEvent with a Listener and then you can just everytime he moves (event gets triggered) -> set the border again. So basically when his coordinates change, the border will immediantly follow you.
public class MoveEvent implements Listener {
#EventHandler
public void onMove(PlayerMoveEvent e) {
Player p = e.getPlayer();
WorldBorder border = world.getWorldBorder();
int locX = p.getLocation().getBlockX();
int locZ = p.getLocation().getBlockZ();
border.setCenter(locX, locZ);
}
}
I hope you can help me and I hope I did not overlook any relevant answers.
So I get this Exception when I try and run my DesktopLauncher.java class.
Executing: gradle run
Configuration on demand is an incubating feature.
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:core:compileJava
:core:processResources UP-TO-DATE
:core:classes
:core:jar
warning: [options] bootstrap class path not set in conjunction with -source 1.6
1 warning
:desktop:compileJava
:desktop:processResources UP-TO-DATE
:desktop:classes
Exception in thread "LWJGL Application" java.lang.ClassCastException: com.bluemoon.game.MainGame cannot be cast to com.badlogic.gdx.InputProcessor
at com.bluemoon.game.MainGame.create(MainGame.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
:desktop:run
BUILD SUCCESSFUL
Total time: 4.267 secs
I just copied the my code from this tutorial.
This is my MainGame.java from the core files
package com.bluemoon.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapRenderer;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class MainGame extends ApplicationAdapter {
Texture img;
TiledMap tiledMap;
OrthographicCamera camera;
TiledMapRenderer tiledMapRenderer;
#Override
public void create () {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera();
camera.setToOrtho(false,w,h);
camera.update();
tiledMap = new TmxMapLoader().load("basic_map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);
Gdx.input.setInputProcessor((InputProcessor) this);
}
#Override
public void render () {
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
tiledMapRenderer.setView(camera);
tiledMapRenderer.render();
}
}
And this is my DesktopLauncher.java
package com.bluemoon.game.desktop;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.bluemoon.game.MainGame;
public class DesktopLauncher {
public static void main (String[] arg) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.fullscreen = true;
cfg.resizable = false;
new LwjglApplication(new MainGame(), cfg);
}
}
When I run the DesktopLauncher.java, the program is built, then the screen turns black (probably because it's going into full screen mode). After about 1-2 seconds the program crashes.
I'm using gradle a packet management system, if that's important.
It is because MainGame class doesn't implement InputProcessor class. It should be:
public class MainGame extends ApplicationAdapter implements InputProcessor {
Of course, you will have to implement all methods from InputProcessor.
I'm new to Libgdx and am having some problems filling an image to the entire screen. If I import a 1900x1200 image, it fills the screen, but if instead the image is 1024x512, the image is not stretched to the screen. The following is the code I've used. I thought that ''background.setSize(stageWidth, stageHeight)'' would scale the image to the screen but this doesn't happen. Could you please inform me of what I'm doing wrong? I've tried toggling with the ''setFillParent'' (as mentioned in another post) but it still doesn't work. Here's also included a screenshot of what currently appears (both on the Desktop implementation and Android): http://tinypic.com/r/b7j20z/8
Thank You for your help!
package com.mygdx.game;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
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.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.Actions;
import com.badlogic.gdx.scenes.scene2d.actions.MoveToAction;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.StretchViewport;
import com.badlogic.gdx.utils.viewport.Viewport;
public class MyGdxGame implements ApplicationListener{
private Stage stage;
#Override
public void create() {
stage = new Stage();
float stageHeight = Gdx.graphics.getHeight();
float stageWidth = Gdx.graphics.getWidth();
// Background
Texture board = new Texture(Gdx.files.internal("data/frame.png"));
Image background = new Image(board);
background.setOrigin(0, 0);
background.setSize(stageWidth, stageHeight);
background.rotateBy(0);
background.setPosition(0, 0);
stage.addActor(background);
}
#Override
public void dispose() {
stage.dispose();
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
After thinking about it for a bit, the most likely cause of your problem is that you're adding your image directly to the root of the stage. The root of the stage is a Group that doesn't handle any kind of layout managing, which is probably why your image isn't sized correctly.
I'd recommend instead that you create a Table, and then use setFillParent() so it takes up the entire screen. You can then simply use the Table's built in function, table.background(drawable), to set a background. However, you have to keep in mind that a Texture doesn't count as drawable for the table, so you have to use the class TextureRegionDrawable to get a valid drawable for the background. Here's an example:
table.background(new TextureRegionDrawable(new TextureRegion(board)));
i am making an app to make the hands of a clock.That is, i draw a simple line on the screen and want it rotate like the hands of a clock.how do i do this using a timer?(so that the position of the line refreshes).Only one end point of the line should rotate,while the other remains staionary.In the folllowing code,i only draw a line on the screen:
DrawView.java:
package LineRefresh.xyz.com;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
#Override
public void onDraw(final Canvas canvas) {
paint.setColor(Color.BLACK);
canvas.drawLine(50, 200, 270, 200, paint);
}
}
LineRefresh.java:
package LineRefresh.xyz.com;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class LineRefresh extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
Try something like this:
private Handler mHandler = new Handler();
private Runnable MethodToDrawClockLine = new Runnable()
{
public void run()
{
//execute here your drawing routine.
// after drawing set the timer for another x period of time
mHandler.postDelayed(MethodToDrawClockLine, 1000); //it'll re-execute it after 1sec.
}
}
in you OnCreate or any method where you want to Start the Timer you can use:
mHandler.removeCallbacks(mUpdateTimeTask); //be sure to remove any handler before add a new one
mHandler.postDelayed(MethodToDrawClockLine, 1000); //it'll execute it after 1sec.
when you're done, remember to remove the handler.
mHandler.removeCallbacks(mUpdateTimeTask);
That shold be it.