how to open another class by clicking on a button on libgdx? (android development eclipse) -
i making own game. have made splash screen , main menu. have made button "play" in menu can't link main game, while can link other classes.
here code of class can't open:
package com.mygdx.papermadness.screens; import com.badlogic.gdx.applicationlistener; import com.badlogic.gdx.gdx; import com.badlogic.gdx.inputadapter; import com.badlogic.gdx.preferences; import com.badlogic.gdx.screen; import com.badlogic.gdx.graphics.color; import com.badlogic.gdx.graphics.texture; import com.badlogic.gdx.graphics.texture.texturewrap; 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.glutils.shaperenderer; import com.badlogic.gdx.math.vector2; public class papermadness extends inputadapter implements applicationlistener, screen { float timer; public static preferences prefs; public static int counter; // variable want save private bitmapfont font; //public int counter = 0; boolean touch = false; spritebatch batch; spritebatch spritebatch; texture spritetexture; sprite sprite; float scrolltimer = 0.0f; player player; paper paper; huiz huiz; lijn lijn; string money = string.valueof(counter); shaperenderer sr; public boolean kukar = false; public void create() { font = new bitmapfont(); gdx.input.setinputprocessor(this); player = new player(new vector2(50, 100), new vector2(100, 100)); huiz = new huiz(new vector2(200, 300), new vector2(110, 110)); // huiz = new huiz(new vector2(200, 300), new vector2(110, 110)); paper = new paper(new vector2(gdx.input.getx(), gdx.graphics.getheight() - gdx.input.gety()), new vector2(50, 50)); lijn = new lijn(new vector2(0, 200), new vector2(600, 2)); // sr = new shaperenderer(); spritebatch = new spritebatch(); spritetexture = new texture("b9.png"); spritetexture.setwrap(texturewrap.repeat, texturewrap.repeat); sprite = new sprite(spritetexture); sprite.setsize(gdx.graphics.getwidth(), gdx.graphics.getheight()); batch = new spritebatch(); } @override public void render(float delta) { // bounds.set(gdx.input.getx(), // gdx.graphics.getheight() - gdx.input.gety(), // secondtexture.getwidth(), secondtexture.getheight()); scrolltimer += gdx.graphics.getdeltatime(); if (scrolltimer > 1.0f) scrolltimer = 0.0f; sprite.setv(scrolltimer + 2); sprite.setv2(scrolltimer); player.update(); paper.update(); lijn.update(); huiz.update(); /* * if (tree.getbounds().overlaps(ball.getbounds())) { * system.out.println("swendley programinateur"); } * * if (tree.getbounds().overlaps(paper.getbounds())) { * system.out.println("souk programinateur"); } */ spritebatch.begin(); sprite.draw(spritebatch); spritebatch.end(); batch.begin(); player.draw(batch); huiz.draw(batch); // paper.draw(batch); if (gdx.graphics.getheight() / 1.25 < gdx.input.gety() && gdx.graphics.getwidth() / 2.7 < gdx.input.getx() && gdx.graphics.getwidth() / 1.7 > gdx.input.getx() && gdx.input.istouched() && kukar == false && touch == false) { kukar = true; touch = true; } else if (gdx.input.istouched() && kukar == true && touch == true) { paper.draw(batch); if (paper.getbounds().overlaps(huiz.getbounds()) || paper.getbounds().overlaps(huiz.getbounds1())) { // system.out.println("huis geraakt!"); touch = false; counter++; checkspeed(); money = integer.tostring(counter); // system.out.println(counter); } } if (huiz.getbounds().overlaps(lijn.getbounds()) || huiz.getbounds1().overlaps(lijn.getbounds())){ //system.out.println("game over"); } font.draw(batch, money, gdx.graphics.getwidth() / 2.06f, gdx.graphics.getheight() / 1.05f); font.setcolor(color.black); font.setscale(2, 2); // house.draw(batch); // house1.draw(batch); lijn.draw(batch); batch.end(); // sr.begin(shapetype.filled); // sr.setcolor(color.yellow); // sr.rect(gdx.input.getx(), gdx.graphics.getheight() - // gdx.input.gety(), // paper.getsize().x, paper.getsize().y); // sr.setcolor(color.black); // sr.rect(huiz.getposition().x, huiz.getposition().y, // huiz.getsize().x, huiz.getsize().y); // sr.rect(house1.getposition().x, house1.getposition().y, // house1.getsize().x, house1.getsize().y); // sr.end(); } public static void saveprefs(){ prefs = gdx.app.getpreferences("game-prefs"); // name of prefs files prefs.putinteger("counter", counter); prefs.flush(); system.out.println(prefs); } public static void loadprefs(){ prefs = gdx.app.getpreferences("game-prefs"); counter = prefs.getinteger("counter",0); //load counter, default 0 if not found } public void checkspeed() { if (counter <= 7) { huiz.huisvelocity = 500f; } if (counter > 7 && counter <= 17) { huiz.huisvelocity = 550f; } if (counter > 17 && counter <= 30) { huiz.huisvelocity = 650f; } if (counter > 30 && counter <= 50) { huiz.huisvelocity = 750; } if (counter > 50 && counter <= 75) { huiz.huisvelocity = 900; } if (counter > 75 && counter <= 100) { huiz.huisvelocity = 1000; } } @override public boolean touchup(int screenx, int screeny, int pointer, int button) { kukar = false; touch = false; return true; } @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() { } @override public void render() { } }
here menu class:
package com.mygdx.papermadness.screens; import com.badlogic.gdx.game; import com.badlogic.gdx.gdx; import com.badlogic.gdx.screen; import com.badlogic.gdx.graphics.color; import com.badlogic.gdx.graphics.gl20; import com.badlogic.gdx.graphics.g2d.bitmapfont; import com.badlogic.gdx.graphics.g2d.textureatlas; import com.badlogic.gdx.scenes.scene2d.inputevent; import com.badlogic.gdx.scenes.scene2d.stage; import com.badlogic.gdx.scenes.scene2d.ui.label; import com.badlogic.gdx.scenes.scene2d.ui.label.labelstyle; import com.badlogic.gdx.scenes.scene2d.ui.skin; import com.badlogic.gdx.scenes.scene2d.ui.table; import com.badlogic.gdx.scenes.scene2d.ui.textbutton; import com.badlogic.gdx.scenes.scene2d.ui.textbutton.textbuttonstyle; import com.badlogic.gdx.scenes.scene2d.utils.clicklistener; import com.mygdx.papermadness.papermadness; public class mainmenu implements screen { private stage stage;// done private textureatlas atlas;// done private skin skin;// done private table table;// done private textbutton buttonplay, buttonexit; private bitmapfont white, black;// done private label heading; @override public void render(float delta) { gdx.gl.glclearcolor(0, 0, 0, 1); gdx.gl.glclear(gl20.gl_color_buffer_bit); table.drawdebug(stage); stage.act(delta); stage.draw(); } @override public void resize(int width, int height) { } @override public void show() { stage = new stage(); gdx.input.setinputprocessor(stage); atlas = new textureatlas("ui/button.pack"); skin = new skin(atlas); table = new table(skin); table.setbounds(0, 0, gdx.graphics.getwidth(), gdx.graphics.getheight()); white = new bitmapfont(gdx.files.internal("font/white.fnt"), false); black = new bitmapfont(gdx.files.internal("font/black.fnt"), false); // maakt buttons textbuttonstyle textbuttonstyle = new textbuttonstyle(); textbuttonstyle.up = skin.getdrawable("button.up"); textbuttonstyle.down = skin.getdrawable("button.down"); textbuttonstyle.pressedoffsetx = 1; textbuttonstyle.pressedoffsety = -1; textbuttonstyle.font = black; buttonexit = new textbutton("exit", textbuttonstyle); buttonexit.addlistener(new clicklistener() { @override public void clicked(inputevent event, float x, float y) { gdx.app.exit(); } }); buttonexit.pad(15); buttonplay = new textbutton("play", textbuttonstyle); buttonplay.addlistener(new clicklistener() { @override public void clicked(inputevent event, float x, float y) { ((game) gdx.app.getapplicationlistener()) .setscreen(new papermadness()); } }); buttonplay.pad(15); // maakt header heading = new label(papermadness.title, new labelstyle(white, color.white)); heading.setfontscale(2); table.add(heading); table.getcell(heading).spacebottom(100); table.row(); table.add(buttonplay); table.getcell(buttonplay).spacebottom(15); table.row(); table.add(buttonexit); // table.debug(); stage.addactor(table); } @override public void hide() { } @override public void pause() { } @override public void resume() { } @override public void dispose() { stage.dispose(); atlas.dispose(); skin.dispose(); white.dispose(); black.dispose(); } }
public class mygdxgame extends game{ public static menuscreen menuscreen; public static gamescreen gamescreen; @override public void create(){ menuscreen = new menuscreen(this); gamescreen = new gamescreen (this); setscreen(menuscreen); } }
here menuscreen
public class menuscreen implements screen{ mygdxgame game; public menuscreen(mygdxgame game){ this.game = game; } ////////////when want change screen type game.setscreen(game.gamescreen) ........... ........... }
and gamescreen
public class gamescreen implements screen{ ........... ........... ........... }
this simple example , try same in code.
here tutorial https://github.com/libgdx/libgdx/wiki/extending-the-simple-game if helped , let me know it.
Comments
Post a Comment