So I have a Unity program, where it switches screens if the user presses ENTER.
However, whenever the user presses enter, instead of just going one screen forward, it skips all the way to the last screen.
Here is my OnGUI():
if (showScreen == true)
{
//this allows the screens to toggle
switch (pageNum)
{
case 1:
rectangle = GUI.Window(1,rectangle, Page1, "Computer");
break;
case 2:
rectangle = GUI.Window(1,rectangle, Page2, "Computer");
break;
case 3:
rectangle = GUI.Window(1,rectangle, Page3, "Computer");
break;
case 4:
rectangle = GUI.Window(1,rectangle, Page4, "Computer");
break;
case 5:
rectangle = GUI.Window(1,rectangle, Page5, "Computer");
GUILayout.Window (2,rect2, popUp, "Warning!");
break;
case 6:
rectangle = GUI.Window(1,rectangle, Page6, "Computer");
break;
case 7:
rectangle = GUILayout.Window(1,rectangle, Page7, "Computer");
break;
default:
break;
}
}
And here is where the GUIs are actually set up, and made to change:
void Page1(int windowID)
{ if (Input.GetKeyDown(KeyCode.Return))
{
GUI.FocusControl(null);
pageNum = 2;
}
}
void Page2(int windowID)
{
if (Input.GetKeyDown(KeyCode.Return))
{
pageNum = 3;
GUI.FocusControl(null);
}
}
//continue like this for 5 more Page#(int windowID)
So what can I insert into these classes to make my program stop skipping? should I put a pause into these if statements too?
So it looks like the easiest way to solve this problem is by changing how the program changes the stages. All you need to do is a boolean that changes to true once the return key has been pressed.
so just declare the boolean:
public class ClassName : MonoBehaviour
{
bool wasClicked = false;
Then to change the states just do this:
if (Input.GetKey(KeyCode.Return))
{
wasClicked = true;
}
if (wasClicked)
{
if (!Input.GetKey(KeyCode.Return))
{
GUI.FocusControl(null);
pageNum = next page number;
wasClicked = false;
}
}
Related
I have 2 2D gameobjects in unity.I want to achieve next but only with Input.GetTouch for android:
When i click on first or second object---play some sound.
When drag any of those game objets to each other combine them to one---without playing sound from first case.
Then be able to click on this combined object---without playing sound from first case
Problem is because I dont want to play sound from first case if object is dragging.
I already tried some code with couroutine but it doesnt work:
if (Input.touchCount > 0) {
touch = Input.GetTouch(0);
touchPos = Camera.main.ScreenToWorldPoint(touch.position);
RaycastHit2D hit = Physics2D.Raycast(touchPos, Camera.main.transform.forward);
switch (touch.phase) {
case TouchPhase.Began:
samoKlik = true;
if (samoKlik) {
StartCoroutine(korotinaKlika());
}
}
case TouchPhase.Moved:
isMoved = true;
if (!samoKlik) {
if (jedan) {
break;
case TouchPhase.Ended:
isMoved = false;
jedan = false;
break;
}
You need to write your switch case properly. This code will not compile. The proper switch case syntac would be somethink like the one described here:
switch (touch.phase) {
case TouchPhase.Began:
samoKlik = true;
//if (samoKlik) { // This is not needed
StartCoroutine(korotinaKlika());
//}
break;
case TouchPhase.Moved:
isMoved = true;
if (!samoKlik) { // samoKlik does not exist here
if (jedan) {
break;
}
}
break;
case TouchPhase.Ended:
isMoved = false;
jedan = false;
break;
}
Since you want to fall from one conditional to another, it looks like the switch statement is not right for you.
I would suggest to try the following:
Declare your variables before your logic
bool samoKlik = false;
bool isMoved = false;
bool jedan = false;
Start with if/else clauses.
I can identify one for you to help you going
if (touch.phase == touchPhase.Moved && !samoKlik && jedan)
I want to add to this game: https://www.mooict.com/wpf-c-tutorial-create-a-fun-balloon-popping-game-in-visual-studio/
A function ColorKeyIsDown() that is activated when some of keys: Y,R,B,G,O are pressed.
So, if any of these keys is pressed, a specific balloon will be removed from the canvas.
I made 5 possible tags for every newBalloon that is made,
because ballonSkins integer value is from 1-5, every number declares a different background of the newBallon.
In ColorKeyIsDown() I want to identify the balloons by their tags,
But it does not work.
How can I identify each different balloon(yellow/red/blue balloon)?
Can I identify the balloon by it's background?
*In XAML:
<Canvas Name="MyCanvas" Focusable="True" MouseLeftButtonDown="popBalloons" Background="White" KeyDown="ColorKeyIsDown">
<Label Name="scoreLabel" FontSize="24" Content="Score: 0" Foreground="black" FontWeight="ExtraBold" Canvas.Top="527" />
</Canvas>
*In C# Code:
private void ColorKeyIsDown(object sender, KeyEventArgs e)
{
if (gameisactive)
{
foreach(var x in MyCanvas.Children.OfType<Rectangle>())
{
switch (e.Key)
{
case Key.Y:
if ((string)x.Tag == 3.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.R:
if ((string)x.Tag == 1.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.B:
if ((string)x.Tag == 5.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.G:
if ((string)x.Tag == 4.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
case Key.O:
if ((string)x.Tag == 2.ToString()) { MyCanvas.Children.Remove(x); score++; }
break;
}
}
}
}
*In C#- in gameEngine() function:
// check which skin number is selected and change them to that number
switch (balloonSkins)
{
case 1:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon1.png"));
break;
case 2:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon2.png"));
break;
case 3:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon3.png"));
break;
case 4:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon4.png"));
break;
case 5:
balloonImage.ImageSource = new BitmapImage(new Uri("pack://application:,,,/files/balloon5.png"));
break;
}
// make a new rectangle called new balloon
// inside this it has a tag called bloon, height 70 pixels and width 50 pixels and balloon image as the background
Rectangle newBalloon = new Rectangle
{
Tag = balloonSkins.ToString(),
Height = 70,
Width = 50,
Fill=balloonImage
};
Someone wrote that instead of doing the KeyDown event in Canvas,
to do it in the window browser. solution: onkeydown event not working on canvas?
*I changed it, and it works!
I am making a snake game as my homework assignment , I've already added a code for making the snake head move depending on the user input (left , right , down , up)
But I am stuck with the timing of it , I used Thread.Sleep in order for the game not to crash and get an exception , but my instructor told me that Thread.Sleep is a horrible idea in programming because it literally adds a delay to your game.
So, I need to somehow make it that there won't be a delay in my game while avoiding Thread.Sleep
class Program
{
static void Main(string[] args)
{
Direction direction = Direction.Down;
Console.CursorVisible = false;
int x=0 , y=0 ;
int xprev = 2, yprev = 2;
char shape = 'o';
x = xprev;
y = yprev;
Console.SetCursorPosition(xprev, yprev);
Console.Write(shape);
UserInput input = new UserInput();
ConsoleKeyInfo? info;
while (true)
{
info = input.GetKey();
if (info.HasValue)
{
switch (info.Value.Key)
{
case ConsoleKey.RightArrow:
direction = Direction.Right;
break;
case ConsoleKey.LeftArrow:
direction = Direction.Left;
break;
case ConsoleKey.UpArrow:
direction = Direction.Up;
break;
case ConsoleKey.DownArrow:
direction = Direction.Down;
break;
}
}
Thread.Sleep(100);
switch (direction)
{
case Direction.Up:
y--;
break;
case Direction.Down:
y++;
break;
case Direction.Left:
x--;
break;
case Direction.Right:
x++;
break;
}
Console.MoveBufferArea(xprev, yprev, 1, 1, x, y);
xprev = x;
yprev = y;
}
As Sohaib Jundi suggests, a timer of some description is a reasonable solution here. Your objective is:
Every 100ms, update where the snake is
Rather than solve this by saying make the application pause for 100ms, then update the snake, using a timer changes it to Have something that triggers every 100ms to update the snake.
For example:
using System;
using System.Threading;
namespace Snake
{
class Program
{
static void Main(string[] args)
{
var snakeTimer = new Timer(updateTheSnake, null, 0, 100);
while(true)
{
var keypress = Console.ReadKey();
if (keypress.Key == ConsoleKey.Escape)
{
break;
}
}
}
static void updateTheSnake(object state)
{
Console.Write("#");
}
}
}
This is a very simple example that just draws a line of # across the screen until the user presses the escape key. In your code, you'd likely want to move everything below the Thread.Sleep into the updateTheSnake method. direction will probably need to be stored into shared state so that you can refer to it from the updateTheSnake method.
I am new to c# and I'm wanting to create a game that it has three button with different colors , I want to turn on and off each of them in random state for 1000 milisecond .. what should I do ? can anyone help me ? :)
there are three buttons including Red , Blue and Green colors , after click the "start" button they begin to blink randomly ... I want set 1000 millisecond for this blink !
switch (colorNum)
{
case 1:
btnRed.BackColor = Color.Red;
Thread.Sleep(milisecond);
//if (btn == btnRed)
//{
count++;
break;
//}
case 2:
btnBlue.BackColor = Color.Blue;
//{
count++;
break;
//}
case 3:
btnYellow.BackColor = Color.Yellow;
//{
count++;
break;
//}
}
private void Blink_Button()
{
Random rnd = new Random();
int colorNum = rnd.Next(1, 4);
switch (colorNum)
{
case 1:
btnRed.BackColor = Color.Red;
btnBlue.BackColor = Color.LightGray;
btnYellow.BackColor = Color.LightGray;
break;
case 2:
btnBlue.BackColor = Color.Blue;
btnRed.BackColor = Color.LightGray;
btnYellow.BackColor = Color.LightGray;
break;
case 3:
btnYellow.BackColor = Color.Yellow;
btnRed.BackColor = Color.LightGray;
btnBlue.BackColor = Color.LightGray;
break;
}
}
Call this method in the Timer tick event
I am working on MS chart, not having so much idea.
I have bind the chart, my next thing is to give different colors of each datapoint?
How can i do that?
My sample code is:
foreach (DataPoint pt in ChartRTM.Series["SeriesSeverity"].Points)
{
string sev = pt.YValues[1].ToString(); // this will be your value depending upon which you could set the color
switch (sev)
{
case "I":
pt.Color = Color.Red;
break;
default:
pt.Color = Color.Blue;
break;
}
}
I think you want to change the Marker color of the point :
To do this, change the switch statement :
switch (sev)
{
case "I":
pt.MarkerColor = Color.Red;
break;
default:
pt.MarkerColor = Color.Blue;
break;
}