Easiest way to implement a game menu in ActionScript 3? - actionscript-3

At the moment my game simply consists of a play button which, when clicked, runs the game; all of this happens in the document class as follows:
stage.addChild(playBtn);
playBtn.x = 0;
playBtn.y = stage.stageHeight/2;;
playBtn.addEventListener(MouseEvent.CLICK, playGame);
The playGame() function then removes playBtn from the stage and the event listener, and runs the game code. This has been fine for me up until now, but now I want to implement a more complex menu system, which will probably consist of many buttons, graphics, etc (basically a stage select screen). My question is, what would be a more efficient way of displaying a menu on the screen, removing it when a stage is selected and then being able to return to the menu when the game is over? Obviously I'm trying to do all of this without the use of the timeline.

Use separate classes for each screen. For example a MainMenu screen class and a Game class. Then your document class becomes a simple screen switching controller, which instantiates, adds and removes screen classes.
As long as each screen only adds content into itself, and not to the stage, the document class need not know or care what's inside the screens, and removing a screen will not require each element in the screen to be removed.
As for how to add all elements to each screen, let the screen class handle it, or a symbol linked to the screen class with all your content.

Related

When to use Screen, Stage, and Group in LibGDX

I have a main menu screen, which has 4 options, New Game, Load Game, Options and Exit.
Then:
New Game has more options such as difficulty, # of players + Start button.
Load Game has multiple saves to choose from together with Load and Delete options.
Options has Graphics level and Volume radio/ slider + Apply, Discard buttons.
At last, the game screen would have a pannable game window, together with an overlaying UI on the side of the screen.
My problem is, I don't know what class to use for those UI parts.
From this answer I understand that Screen is a full UI page, but it seems that creating a new Screen and then a Stage for each of those main menu options seems like an overkill, but maybe that's the way to go. I don't know if I should use Group and show/hide those depending on what the user clicks. I was also told to use Table to lay out the game screen. I'm utterly confused by all the guides I've found online. Every one seems different than the other.
The documentation is really good, but it never states how the individual parts are meant to be integrating with each other.
Is there any consensus on how to use those classes in LibGDX? Or is it a personal preference?
Screen is core libGdx part that represents one of many application screens, such as a mainscreen, a settingsscreen, the gamescreen and so on.
mainscreen may contains buttons like settings, play, level that help you to redirect on other screen.
settingsscreen may contain back button that redirect to previous mainscreen and other ui element like sound button, music button, language selection button.
It is hard to maintain different UI Screen in one ApplicationListenercontext with different required lifecycle.
so there is a Game class and Screen interface that helps to display one active screen at a time on device screen.
I think you've clear now, why we need different screens.
Now how I design one Screen Like MenuSceen that contains menu buttons, game name and similar element that act as HomeScreen.
You can use scene2d, a 2D scene graph for building UIs using a hierarchy of actors. Stage and Group are core classes of scene2d.
Stage class has a camera, SpriteBatch, and a root group and handles drawing the actors and distributing input events.
Group class is an actor that may have child actors.

Stage in the Screen or Game class?

I'm new to Scene2d, so i'm a little confused. Imagine that we have this two classes:
public GameScreen implements Screen {
//override methods
}
public GameX extends Game {
//override methods
}
both methods have methods like render and resize, so my question is:
Which of both is the best approach to include Stage/actors and Box2d stuff?
The Game class is actually the core of the game. It's methods are called by the GameLoop or, if some events occure.
Every game can have multiple Screens, like for example the MenuScreen, the PlayScreen, the OptionScreen or the CreditsScreen.
So a Screen repressents something like a part of a game.
The Game class can have one active Screen at a time and it calls reder() for that Screen in it's own render. The same counts for pause() and resume().
If you change the active Screen, hide() gets called for the old Screen and show() for the new Screen.
So the answer to your question is, that if your Game is really simple and does not contain different Screens, you can put the Stage inside the Game classs and treat the Game class like your one and only Screen.
If you instead have more then one Screen you should use the Screen interface and setup the Stage in one of the Screen clases.
I use both since the Game class can switch between screens setScreen. Usually I first load a loadingScreen that loads needed assets when loading is done i switch to a mainMenuScreen. But you can put some actors in the game class and load screen implementations from there, it really does not matter.
But for a clean approach I advice to just call a loading screen from the Game create method and build from there. Since each game needs at least some kind of menu and a game screen itself.

Accessing List and TextInput inside a movieClip

I have a problem accessing a List and TextInput fields placed inside an MC.
What I'm trying to do is an animation for a menu. So I placed buttons and List and a TextInput inside a MC which I want to animate.
I'm trying to access them like this:
string_variable = mc_name.textInput_Name.text;
Everything worked fine before I placed those controls inside an MC. The code was, of course, without mc_name. prefix before that.
What am I doing wrong and how can I fix this? My goal is to make a complex keyframe animation of a clip with buttons and other controls inside it, specifically List and TextInput ones.
Thanks.
Most animations, especially UI, like menu blocks, lists, could be done with tweening engines. You will have more control over animated elements. MovieClip isn't a nice choice to use as mechanism to animate UI components that should be accessed.
Anyway, If you want MovieClips in your project, give same names to the components on the timeline on every keyframe. It will help, a bit...

ActionScript 3 Best place to bind events

I am writing a small Flash game using air for android and have encountered a slight issue.
The game has 3 screens (excluding the game screen) which are main menu, load game & create new game. On the load game and create new game screens I want to add a button which take the user back to the main menu.
I know how to add event listeners to buttons but I am wondering where do I bind the event listener for back to main menu, binding in the constructor of my main class produces an error at run time and binding after going to the frame is knocking out the other buttons when back at the main screen.
At present the app is across multiple frames (probably a better structure to this instead of loading a single frame with each menu), I am new to actionscript so I am not entirely certain on best practices for this sort of thing.
Thanks.
I recommend you to use only one frame in your Fla, then use a Document Class. In that main Class, you should place all your display objects using ActionScript.
I hope you know how to instantiate Class as library clips.

How to animate showing of JPopupMenu?

I'd like to show increasingly more of a JPopupMenu so it appears to "slide out". It's not for menu items, where animation doesn't make too much sense. Instead it's for a panel with some real components (oh yeah, you can add any JComponent to JPopupMenu).
JPopupMenu has many private fields and methods, which makes it hard to extend. Plus I'm not familiar with Swing animation to begin with.
Thanks a lot for your help!
I just did this within the last few weeks. I went a slightly different approach, and Grabbed the layeredPane, and then added a Custom JPanel to the layered pane. In the jpanel i overrode the getHeight method, and had the height start at 0. I could then add whatever components I wanted to my jpanel. In the jpanel I had a method to start animation, this started a timer, and in the timer all I did was increase the height of my jpanel to a max size.
That's it. It works really well in my application, I have it tied to expand below a Jbutton on press, and collapse on another press.
One more thing, you'll want to add a mouseAdapther to the jpanel so events don't go through the components below the layer.