XNA how to check if the game window cross "X" is clicked - xna

Hi am making a game for Windows PC using XNA and I want to check when the game window cross "X" is clicked, so I can instead draw a menu and do other stuff instead of the game closing.
So yea, how can I check when the cross is clicked.
Thanks!

There is nothing special needed for XNA, you can do this the same way as you could without it.
In your Initialize or LoadContent method need to find the Windows form reference your game is using, and add a Closing event to it.
Form form = Form.FromHandle(Window.Handle) as Form;
form.FormClosing += OnClosing;
Then using this OnClosing method, you can cancel the close event, and run your own menu screen. (Probably by changing the game state and drawing something else)
void OnClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
ShowMenuScreen();
}

Related

GWT: Textbox doesn't show Cursor on Ipad

I am trying to implement a Textbox that can show fractions with GWT.
Therefor I have an Canvas were I can draw what I want and receive KeyEvents and MouseEvents.
But on Ipad (Safarie and Chrome) the software keyboard does not show, so I created an Composite and combined the Canvas with a Textbox witch gets the focus after each key or mouse Event on the Canvas.
But the softkeyboard does not show up every time so I tried a bit and can see, that the Textbox seems to get the focus (it gets a blue boarder) but does not always show the cursor.
This does not happen on my Notebook.
Is there any difference between being focused and showing the cursor?
I tried:
Setting the Cursor position
set the Text of the Textbox.
Any help would be appreciated,
Christoph
public void setFocus(boolean b) {
// if (hasFocus) {
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
public void execute () {
t.setFocus(b);
}
});
Scheduler.get().scheduleDeferred(new ScheduledCommand() {
public void execute () {
box.setFocus(true);
box.setText("x");
box.setCursorPos(0);
// box.setVisible(false);
// box.setVisible(true);
}
});
// t.setFocus(b);
// box.setFocus(b);
// }
}
The iOS browsers don't allow the focus to be set programmatically unless directly in response to a user interaction (i.e. a touch). I believe the reason is to prevent websites bringing up the virtual keyboard for no reason.
The downside is that it clobbers setFocus() for websites that want to use it for legitimate reasons. You can't call setFocus() in a deferred command because that doesn't count as a direct response to the user interaction.
(To be more precise, you can call setFocus() in a deferred command, but it won't have the desired effect as you found out.)

Xcode/Objective-C implementing a players turn toggle

I'm new to the whole VCM programming structure and more specifically to objective-c.
Right now i'm working on an iOS app that is like the game "connect 4." This Xcode project is my practice project that i've been using to play around/learn how to make iOS apps so there are several files and lots of commented out code, so Ill try my best to provide the necessary content from my project.
App State:
Right now I have a 10x10 grid of blue square/buttons. I'm still in the early stages of implementing the functionality of the game. Rather than making each square its own button, I created 10 frames and 10 buttons. Each row is a single button, which is broken up into 10 squares.
I currently have the squares change their color to red and become disabled once clicked. If I click multiple times.. the squares turn red when pressed. Heres a screenshot example run:
What Im wanting to do:
I want to build off of what I have, but not exactly sure how to keep track of which "players turn" it is. For example.. I want the first clicked square to turn red, representing player1's move. I want the second click to turn the button, say green, representing player2's move.. then the next move will be player 1's move.
I know Ill have to make sure the button is/was ENABLED and valid before switching which players turn it is.
How should I go about doing this? This is more of a general question, but I'll provide needed code snippets if necessary. Thanks!
In your .h file add an integer (variable)
int Turn = 1;
In your .m file you can do:
if(Turn == 1)
{
// Player one is playing
Turn = 2;
}
else if(Turn == 2)
{
// Player two is playing
Turn = 1;
}

onMouseMove listener only starts receiving events after cursor leaves and re-enters the document

I have CanvasElement which requests pointer lock on click, and then repeatedly passes movementX and movementY to a function using an onMouseMove listener (I'm testing first person controls).
It works fine the first time (immediately after page load), but if I press escape and click the canvas again it successfully locks the cursor but doesn't generate a MouseEvent to pass to the onMouseMove listener.
If I press escape and move the mouse outside of the document (anywhere that's not governed by the markup), move it back into the document and click the canvas, everything works perfectly again.
Any ideas what's causing this odd behaviour?
_canvas.onMouseMove.listen((MouseEvent e) {
if (document.pointerLockElement == _canvas) {
print(e.movement); // debug print
orientateCamera(e.movement);
}
});
_canvas.onClick.listen((e) {
if (document.pointerLockElement == null) {
_canvas.requestPointerLock();
}
});
All the described behaviour is contained within these lines of code.
I have no idea what element.requestPointerLock() does but
MDN - Element.requestPointerLock states that you should listen to pointerlockchange and pointerlockerror to see if the request was successful. There is also a document.exitPointerLock() which you don't seem to use.
MDN Pointer Lock API seems to be a good introduction.

XNA loading/splash screen on game start

I'm trying to have a loading screen that pops up as soon as you run that executable that launches the game, whilst the game loads I want to display an image on the desktop much like how Gamemaker allows you to have a loading image.
I've had a brief look around and most people want an image to be shown whilst they load content which is not what I want, or perhaps it is... =P
My guess would be to set the game window borderless at the start, load the splash screen, draw it, then begin all the main loading (Specifically loading DirectInput through SlimDX takes a while). However would this support having a transparent/irregular shaped image? E.G if I wanted a borderless circle to be displayed whilst the game loads, would that be possible?
Thanks.
It's very simple. Just implement System.Threading and you're done.
Here's how it works, first add:
using System.Threading;
to the top of your class, then add the following things:
//the enum for state types
enum GameState
{
Loading,
StartMenu
}
//Init our gameState enum as variable
GameState gameState;
protected override void LoadContent()
{
gameState = GameState.Loading;
spriteBatch = new SpriteBatch(GraphicsDevice);
device = GraphicsDevice;
Window.Title = "Your Window";
//Other stuff that you had here before, now go in LoadGame()
loadingscreen = Content.Load<Texture2D>("loading");
Thread bgLoad = new Thread(new ThreadStart(LoadGame));
bgLoad.IsBackground = true;
bgLoad.Start();
}
public void LoadGame()
{
//Loading stuff here
gameState = GameState.StartMenu; //or GameState.Playing
}
and then put a loading texture in your Content Project
If I understand you correctly, you essentially want to have a window opened that is not full screen that also lacks the regular border and buttons that accompanies a window.
I can't test this but this should be a good start.
Here!
I would start by creating a bare bones Game1 class that can track an image and keep track of time for a timer.
Later you could add logic that actually tracks the loading of assets in the main program.
To start I'd just test it for a fixed time like 3 seconds.

XNA - Cannot See Mouse in Game

I've recently started checking out XNA. All is going well and I've even got a working sample started. One thing that's been bugging me is that whenever I place my mouse over the game window it disappears. I don't know if this is the default behavior but I would like to change this so that I can see my mouse. Any and all advice is appreciated.
Simply set IsMouseVisible to true in your main game class(preferably in your Initialize method or Game class constructor) :
IsMouseVisible = true;

Resources