Save the Best time in Level Selection scene in Unity - c#

enter image description hereI want to save the best time meaning the lowest amount of time taken to complete a level in the game. When I try to do that the timer begins from the start in the Level Selection screen. What I want to do exactly is save the time taken to complete the level and store that amount of time in the level selection scene. If the time taken to complete the level is less when we replay the level, that amount of time shows instead of the first. I have looked for about 2 Days and couldn't find a solution.
I tried to do it but when I load the level selection screen the timer starts from 0. I even tried to make bools such that when the game starts it is set to true but still it doesn't work. Please help!

It's not clear what you're trying to do here;
Do you want to save the best time on persistent storage (PlayerPrefs or a save system) or just for the current scene (score is lost as soon as the scene unloads)?
I assume this is only a timer script; you need a method to save the score on level end (but only if the current score is lower than the already present value) and maybe another script to visualize it on the level selection screen, as you mentioned.

Related

Screen flashes when loading new scene

I have a level selection menu, inside this I have buttons to load my scene levels.
I am using the next method to switch the scenes:
UnityEngine.SceneManagement.SceneManager.LoadScene();
The problem is that I have dark-gray faders in my scene and when I load the different scenes the screen flashes white. Instead of staying in the same dark-gray tone like I would want it.
I will gladly supply any further information that you need.
Does anyone know a solution?
The last frame of the previous scene is contiguous with the first frame of the next scene so one needs to ensure that there is nothing in either frame that could make a visible difference e.g. resetting the state of something like a material colour via a script called from an Awake function.

XNA Slow down Animation

I am making the character run but the animation is extremely quick as I am doing:
_frameIndex++;
_frameIndex; is the value which points to the image in the SpriteSheet. Does anyone know how I can use gameTime.ElapsedGameTime.TotalMilliseconds to slow the animation down?
I saw that you've asked a couple of questions tonight concerning animation and spritesheets, so here's an example from Aaron Reed's "Learning XNA 4.0," from Chapter 3 under the "Adjusting the Animation Speed" heading.
First, create two class-level variables to track time between animation frames:
int timeSinceLastFrame = 0;
int millisecondsPerFrame = 50;
The first variable tracks time passed since the animation frame was changed, and the second is an arbitrary amount of time that you specify to wait before moving the frame index again. So making millisecondsPerFrame smaller will increase the animation speed, and making it larger will decrease the animation speed.
Now, in your update method, you can take advantage of game.ElapsedGameTime to check time passed since the last frame change, and change the frame when when that value greater than millisecondsPerFrame, you can do work:
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > millisecondsPerFrame){
timeSinceLastFrame -= millisecondsPerFrame;
// Increment Current Frame here (See link for implementation)
}
This sort of solution is similar to what you've found works, except that you can take an extra step to specify exactly how often you want the animation to update, or even change that duration later on in your code if you like. For example, if have some condition that would "speed up" the sprite (like a power-up) or likewise slow it down, you could do so by changing millisecondsPerFrame.
I removed the code that actually updates the current frame, since you should already have something that does that since you have a working animation. If you'd like to see the example in-full, you can download it from the textbook's website.
I debugged the code and noticed that the gameTime.ElapsedGameTime.TotalMilliseconds always equated to 33. So I did the following:
milliSeconds += gameTime.ElapsedGameTime.Milliseconds;
if (milliSeconds > 99)
{
_frameIndex++;
milliSeconds = 0;
}
Which basically means that if this is the THIRD frame of the game, then make he _frameIndex go up. Reset the milliseconds to start over.

Passing large amount of data between pages wont load the page in wp7

I'm trying to pass some text between pages to a textblock. It works fine with small amount of data, but with large amount of data the page won't load.
Page 1:
NavigationService.Navigate(new Uri("/Pages/DetailPage.xaml?gameGuide=" + gameGuide, UriKind.Relative));
Page 2:
if (NavigationContext.QueryString.TryGetValue("gameGuide", out gameGuide))
guideTxtBlk.Text = gameGuide;
The data source is from a XML file which I'm downloading to Page 1.
Here is the amount of data that work:
In the level "Project Nova", after you fight the British and
escape, you must set an explosive. Make sure you clear out all the
enemies before planting it. There should be a Mosin–Nagant gun after
killing the enemies in the first room. This should help you clear out
the enemies in the second room, especially the enemies on the higher
walkways. You can shoot through the metal gratings. After setting the
explosive, shoot the highlighted beam and make your way out. As you
head out, there will be an enemy outside the door on your right and
another coming on your left. Kill these two and then sprint down the
walkway. There will be a few more enemies climbing onto the ship but
you should have no issues killing them and proceeding. A last enemy
should be up high so be forewarned. If you die at any time during this
run, you may still have enough time to make it, as long as there is
2:15 left on the timer.
Here is the one that don't work:
This is easiest to do on Recruit difficulty and is done on the level
"WMD". You will be in control of the dispatcher in the SR71 plane and
a soldier on the ground, switching between them from time to time. To
zoom in you press (LT) and to zoom out you press (RT). To command your
squad on where to go, press (A) on the location. Use (LS) to move the
cursor around the area. After you find the squad on your screen, you
will be given an insertion point that is to the east of you labeled
"1602". After you hear that vehicles are coming in from the north, you
will be told to put your team into the building labeled "Safe House".
Select the Safe House with (A). You will be put into the soldier’s
scenario to kill the enemies entering the house. After the encounter
with the enemies that entered the building, you will be put back into
the hands of the dispatcher. Make sure all 4 of your teammates are
alive on the screen. After this, select the back of the Safe House
building. To the right you will see two enemies next to a barrel.
Scroll over these enemies and select them with (A) to have your team
attack them. Directly behind them is an objective point. Select the
objective point so that your team moves to this location. After that,
you will be given another insertion point just below this one. You
will see bushes halfway between your current location and the
objective point. Select the bushes with (A) to have your team move
there (you should be 500-600 away from the insertion point).
Immediately after they have moved, your dispatch will tell you that a
large group of enemies is coming around. You will be told to hold down
(RS) to make your team go into a prone position and not be seen by the
enemies. After you get the all clear, move your team to the objective
directly in front of them and you will be given an objective labeled
"Barracks". The on screen reticule should turn red, so select the
Barracks with (A) to have them move into position. Now you will be put
back into the view of the soldier. Eliminate all the enemies found
inside of the building and be sure to be in front to take as much of
the fire that is shot at you and your teammates. At the end of all
this, you should have all four of your teammates with you. You need to
clear the last room, plant and detonate the C4 and make it through the
barracks. You will be given a new insertion point once you leave the
barracks, so guide your team there. When you have reached the second
insertion point outside of the building, you have safely guided the
squad without killing them. Make sure that while you are controlling
your team, no vehicles that drive by see you as your team on the
ground will be eliminated immediately. If they are eliminated, you
will need to restart the whole mission over.
There is a maximum length for a URI. I think it is somewhere around 2000 characters. You should probably adopt a different method to pass the data between the pages. Use a global variable, put the data in the ViewModel, or save and retrieve from PhoneApplicationService/IsolatedStorage are some options.

How to slow down user input?

My update function gets called like 60 times a second, and the player is meant to move in 32 pixel steps on my grid, so the player moves very quickly, i want a way to only recieve certain key inputs every like 10 frames, and still have my game run at 60fps.
You can
require user to do full keyboard clicks with releasing each key
have user's position to be sort-of float instead of int where whole part represents grid steps. While user tries to move in particular direction it will move with some defined speed (like 2 per sec) and when float position becomes next cell player jumps
you can require player to stay in particular cell some given amount of time (like .5 seconds) - thus definind speed how fast it moves.
The easiest way is to keep time on when a key was pressed. There isn't a way to slow down the speed of the input system, so you have to fake it yourself. Here is some pseudocode to get you started.
// See if the key is currently down
if (KeyIsDown(key))
{
if (gameTime.TotalGameTime >= nextTime)
{
// Move the character, and indicate that you want to wait another second for movement.
moveTheCharacter();
nextTime = gameTime.TotalGameTime + 1;
}
}
The main advantage of this approach is that it will always be based on time as opposed to the framerate of your game. So if you drop frames for some reason your character will still move at the same speed.

Which method should I use to give the perception the computer is thinking in programming?

I want to create a simple game like tic tac toe where the human user is playing against the computer. The computer function takes a couple of milliseconds to run but I would like to give the perception of the computer taking 5 seconds to make a move. Which method should I use?
1) Create two memory threads. One for the computer and one for the human user. When the computer is taking 5 seconds to imitate thinking, the human user thread is paused for 5 seconds.
2) Disable input devices for 5 seconds using timer or dispatchertimer
3) Any better methods you can think of..
Thanks!
Edit - The question is about the how and now the why. 5 seconds is just an example. I prefer 1-2 seconds but for example purposes I just chose 5. So please focus on what is the best way to do this and not on the 5 seconds. Thanks again.
Noise and Blinking lights = digital thought.
Rev up the CPU to 100% with an infinite loop to generate heat.
Start looping through all the files to get the hard drive light blinking and making spinning noises.
Run a shell command to change directory to the optical drive to make the optical drive spin.
Set caps lock, numlock and scroll lock on and off, some keyboards have lights for that.
Check to see if your motherboard supports any additional blinking lights.
Ah! Good point. Many fans have API access, so turning the fans on to 100% is cool. The revert to normal could be dangerous though, because if you accidentally turn off the fans for good, it could be serious damage.
you could just make the cursor into an hourglass and disable the main form (which will stop the user from inputting anything), start a timer for 5 secs (i'd make this time configurable), when the timer fires enable the main form and set the cursor back to normal again. Something like this:
Call this method when the user has made their move:
private void UserHasMoved()
{
Enabled = false;
timer1.Interval = 5000;
timer1.Start ();
}
then the event for the timer:
private void timer1_Tick(object sender, EventArgs e)
{
Enabled = true;
timer1.Stop ();
}
wouldn't this be annoying to the faster players? Maybe a second for the turn, but you'll soon find out that 5 seconds is too much.
Either way, do not pause the UI thread, because the interface would 'freeze'.
Because the human is using the GUI as its interface, you can't pause its thread. Pausing the GUI thread would disable your form from repainting, thus not showing the computer's move.
I'd just disable all game related input, and show an animation of the computer working its brain.
Two very important usability principles:
Don't block the UI thread! Only disable the game control so that the player cannot perform a step during the thinking.
Show a visual feedback to the user indicating the thinking and that he can't perform the step for a while.
And of course, balance the exact duration of thinking carefully. Treat the players' time as very precious.
From your question it seems you do not want the computer play to be so lightning fast that the human player can't see it. For that effect, why don't you display some random animation showing possible plays, and ultimately animating the chosen move.
Edit: I have to agree that disabling input devices is a bit harsh. Maybe switch the application to a state in which any input by the user cannot affect the board, but can affect other commands, such as save, restart or quit.

Categories

Resources