Game Over Screen with Libgdx [Switching 2 screens] - libgdx

I have 2 implemented screen, MainScreen and OverScreen in create() of MainClass. In MainScreen I implemented game, in the end of game, I want to show OverScreen, in OverScreen, user clicks into it, and it brings MainScreen back. How to switch 2 screen?

You can pass your GameClass object to your current screen in the constructor and then in your touchDown() method of your InputProcessor you can call gameClass.setScreen();
Lets suppose GameClass is the name of your class that extends Game. Then the constructor of your MainScreen would look like this:
GameClass game;
public class MainScreen(GameClass game){
this.game = game;
}
In your game class, you will pass "this" as argument.
setScreen(new MainScreen(this));
Now you can call this anywhere to change screen:
game.setScreen(new OverScreen());

Related

Handling Screens of Libgdx App on Android

This my way to deal with screens
I have class GameMain extends Game I created instance of it in each screen so I can use something like this gameMain.setScreen()
My question is How to move properly from one screen to another ?
I have the following :
In GameMain I setScreen(new SplashScreen(this))
In splash screen I setScreen(new MenuScreen(this)) in hide() of splash I call its dispose() and so on as when player click play button in menu screen I setScreen() to new playScreen I also call dispose() in hide() and when he returns to Menu I setScreen(new Menu)
Is what I am doing wrong ?
What I normally do is I have in my main Game class a function for example called setPlayScreen(), and in that method I do something like:
public void setPlayScreen(params){
//Do something with params
setScreen(new PlayScreen())
}
If you have your dispose() inside your screens hide() method you shouldn't have any problems.

Scrubbing timeline of movieclip that executes actionscript within

I have an as3 script that will scrub the timeline of a movieclip on the stage.
This scrubs the timeline and displays the frame by frame animation but does not execute any actionscript placed on key frames within that movieclip. Users can scrub backwards or forward. I would like to load the script as well as unload depending on the direction the user scrubs. Can this be done?
What you can do is to make new class and extend MovieClip, Inside of that class you can create public methods that you can call from timeline:
public class MyMC extends MovieClip {
public function myMC() {
// constructor
}
public function load():void{
// Do something
}
public function unload():void{
// Do something
}
}
Than if you used "interface" to to add movie clip, click on movie clip and change instance from MovieClip to your class MyMC. Give it also a name (variable name) for example my_mc Than you can call your function from action on timeline
my_mc.load();
my_mc.unload();
Or if you are using just actions to create movie clip you can do like this:
var my_mc:MyMC = new MyMC();
my_mc.load();
my_mc.unload();

Purpose of class extends MovieClip AS3

I have just started learning AS3 and my school has provided some Class files with the basic structure already written. Here is an example:
package {
//Add in your import statements here
//...
public class MathsQuiz extends MovieClip
{
//Add in your class variables here
//...
public function MathsQuiz()
{
}
public function startGame()
{
//Get the game loop to execute
addEventListener(Event.ENTER_FRAME,update);
}
public function update(evt:Event)
{
//This is the game loop
//Handle user input
//Handle game logic
//Handle display
}
}//end class
}//end package
My Question is, what does "extends MovieClip" actually do?
Thank you for your time!
The MovieClip class inherits from the following classes: Sprite, DisplayObjectContainer, InteractiveObject, DisplayObject (can be added to the display list, moved around via its x and y properties as Marcela said), and EventDispatcher, but unlike the Sprite object, a MovieClip object is Dynamic (A dynamic class defines an object that can be altered at run time by adding or changing properties and methods. A class that is not dynamic, such as the String class, is a sealed class. You cannot add properties or methods to a sealed class at run time.) and has a timeline.
If your class doesn't use timeline (looks like that is the case), you can extends Sprite and will have the same results and better performance. If you want a dynamic class you can just use the dynamic attribute when you declare a class.
The extends keyword allows a class to inherit any publicly-accessible or protected member variables and functions of a base class (in this case, MovieClip).
In this example, this means that MathsQuiz, on top of any functionality you may add, will also function as a MovieClip. On a basic level, this means that it is a display object that can be added to the display list and moved around via its x and y properties.
For a more in-depth understanding, do some research on OOP Inheritance.

GameStateManager LibGDX

I started a project with libgdx and I have a GameStateManager for my GameStates Menu and Play.
If I run the project it shows the Menu and then I can click a button to open the Play GameState.
The Problem is, that when I ended the Game it should show the Menu State again, but I get a black screen. I tested if the render() method is started (with System.out...) and the render() method in Menu is starting.
I am not shure why I get a black screen when I "reopen" the Menu state. Maybe its not working because I use Box2D in Play but I dont know.
Here some code:
This is the method in Play which should open the Menu if the player is at the end:
public void playerEnded() {
gsm.setState(GameStateManager.MENU);
}
Maybe you can tell me, if I have to end box2d things or so.
I hope someone can help me, and if you want more code - no problem.
Your custom GameStateManager should extend this class:
http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/Game.html
To change screens you should be using Game.setScreen(Screen screen)
Each different screen should be an implementation of Screen.
So the way it works in my libGDX projects is such that GameScreen extends Screen, and MenuScreen extends Screen. That way I can change what draws on what screen.
This all goes back to interfaces and polymorphism, so if you don't get those concepts, just give it a quick google and you'll get an idea what you need to do.
Chances are that your are defining a
Stack<GameState> gameStates
in your GameStateManager to manage your GameStates.
If so, you could do some reading on using a Stack. Anyway, here's what could do to solve your problem:
I'm assuming you have the following structure in your GamestateManager;
public void setState(int state){
popState();
pushState(state);
}
public void pushState(int state){
gameStates.push(getState(state));
}
public void popState(){
GameState g = gameStates.pop();
g.dispose();
}
private GameState getState(int state){
if(state == MENU) return new Menu(this);
if(state == PLAY) return new Play(this);
return null;
}
When you click your start button in your Menu-GameState, you'll want to launch the Play-GameState.
Now, instead of using the setState method, which removes the top state (pop) and then pushes the new state. Try using only the pushState method. This will put your new Play-GameState on top of your Menu-GameState in the GameState-Stack.
When you're done playing, you can pop your Play-GameState and the Menu-GameState should reappear. But this time it won't instantiate a new Object, but reuse the one you used when you started the game.
// in the Menu-GameState:
public void startPlay() {
gsm.pushState(GameStateManager.PLAY);
}
// in the Play-GameState:
public void playerEnded() {
gsm.popState();
}
This won't affect gameplay performance as you would render and update your game with only the GameState that is on top of the Stack, like this:
public void update(float dt) {
gameStates.peek().update(dt);
}
public void render() {
gameStates.peek().render();
}
The peek method takes the GameState from the top of the Stack, but leaves it there.
The only downside is that the Menu-Gamestate would stay in-memory during your Play-State.
Also, if this method works, you could still do it your way, but you should check the way your Menu-GameState is instantiated and identify problems when it's instantiated twice.
I implemented a similar StageManager in my game. If you are using viewports/cameras in your game, then it is likely that when you go back to your menu state, the viewport is still set to the Game state's viewport.
For my app, I had my State class have an activate() method which would be called whenever a state became the active state:
public void activate(){
stage.getViewport().update(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}

Error 1180 at AS3

i am building a game on flash cs5. i am making the start screen, and now i try to load the game but I get an error 1180 at my play game function. here is how it works
this is the function were i get the error at line this.stageRef. This class is my mainMenu which extends basemenu.
private function playGame(e:MouseEvent) : void
{
unload();
this.stageRef.dispatchEvent(new Event("gameSTART"));
}
and here is my engine function
public function Engine()
{
preloader = new ThePreloader(474, this.loaderInfo);
stage.addChild(preloader);
preloader.addEventListener("loadComplete", loadAssets);
preloader.addEventListener("preloaderFinished", showMenu);
stage.addEventListener("gameSTART", fGameStart);
}
private function fGameStart(e:Event):void
{
.......... here is all my game code
}
It seems, your stageRef is not proper EventDispatcher object. Either you have another custom Stage class, or when you get stage property with Stage object the owner of this property is not on the stage yet. So try to get stage property after Event.ADDED_TO_STAGE event of the source object. Or show your code where you get Stage and pass to the MainMenu.
make stageRef class implement IEventDispatcher