For an internship project I'm making an app based on Foley sound effects. I made a game for it. One button generates the effect, one plays the sound and four buttons for possible awnsers.
Somehow, the text displays ('helaas' and 'juist!') aren't consistent. The awnser could be right and it will display 'helaas', but when clicken multiple times, it will display 'juist!'. Can anyone help me with what I am doing wrong here?
int kiesnummer()
{
Random randomSound = new Random();
int theSound = randomSound.Next(1, 4);
return theSound;
}
NewSound.Click += delegate
{
kiesnummer();
if (kiesnummer() == 1)
{
welk.Text = "Open haard";
}
else if (kiesnummer() == 2)
{
welk.Text = "Regen";
}
else if (kiesnummer() == 3)
{
welk.Text = "Hondenpootjes op hout";
}
else if (kiesnummer() == 4)
{
welk.Text = "Paardenhoeven op beton";
}
};
Play.Click += delegate
{
if (kiesnummer() == 1)
{
_chips.Start();
}
else if (kiesnummer() == 2)
{
_rain.Start();
}
else if (kiesnummer() == 3)
{
_doggo.Start();
}
else if(kiesnummer() == 4)
{
_koko.Start();
}
};
//Parameters aan functie koppelen bij klikken op de knop
Aw1.Click += delegate
{
kiesknop(1);
};
Aw2.Click += delegate
{
kiesknop(2);
};
Aw3.Click += delegate
{
kiesknop(3);
};
Aw4.Click += delegate
{
kiesknop(4);
};
//Beoordelen of keuze juist of onjuist is
bool kiesknop(int knop)
{
if (knop == kiesnummer())
{
end.Text = "Juist!";
return true;
}
else
{
end.Text = "Helaas!";
return false;
}
}
(I left the button and media declerations out, since it didn't seem relevant)
Calling kiesnummer(); in every if condition will give you different results as it generates new random value every time.
Call it once and use its value through-out:
int value = kiesnummer();
if (value == 1)
{
welk.Text = "Open haard";
}
else if (value == 2)
{
welk.Text = "Regen";
}
else if (value) == 3)
{
welk.Text = "Hondenpootjes op hout";
}
else if (value == 4)
{
welk.Text = "Paardenhoeven op beton";
}
Related
I am experiencing an error that I cannot understand, can anyone explain to me what I am miserably doing wrong?
Database Table:
CREATE TABLE "posti_auto_mil" (
"n_posto" TEXT NOT NULL UNIQUE,
"id_utente" INTEGER NOT NULL,
"targa_1" TEXT NOT NULL,
"targa_2" TEXT NOT NULL,
"targa_3" TEXT NOT NULL,
"targa_4" TEXT NOT NULL,
"targa_5" TEXT NOT NULL,
"locazione" TEXT NOT NULL
);
LoadCar.cs
public static bool CheckTarga(int idUtente)
{
using (IDbConnection cnn = new SQLiteConnection(LoadConnectionString()))
{
var output = cnn.Query($"select * from utenti where id = {idUtente}").FirstOrDefault();
if (output != null)
{
var targa = cnn.Query($"select * from posti_auto_mil where id = {idUtente}").FirstOrDefault();
if (targa != null)
{
var checkpresenza = cnn.Query($"select locazione from posti_auto_mil where id_utente = {idUtente} limit 1");
if (checkpresenza.ToString() == "interno")
{
targaicon = 1;
}
else if (checkpresenza.ToString() == "esterno")
{
targaicon = 2;
}
else if (checkpresenza.ToString() == "provvisorio")
{
targaicon = 2;
}
else targaicon = 3;
}
return true;
}
else
{
targaicon = 3;
return false;
}
}
Form1.cs
//Parking
//Visibilità
listView_targheinfo.Items.Clear();
listView_targheinfo.Visible = true;
pictureBox_infobox.Visible = true;
if (Classi.LoadCar.CheckTarga(Int32.Parse(home_textBox_id.Text)) == true)
{
if (Classi.LoadCar.targaicon == 1)
{
pictureBox_infobox.BackgroundImage = global::RegistroAccessi_8Rgt.Properties.Resources.parking_32px_green;
}
else if (Classi.LoadCar.targaicon == 2)
{
pictureBox_infobox.BackgroundImage = global::RegistroAccessi_8Rgt.Properties.Resources.no_parking_32px;
}
else if (Classi.LoadCar.targaicon == 3)
{
pictureBox_infobox.BackgroundImage = global::RegistroAccessi_8Rgt.Properties.Resources.parking_32px_yellow;
}
else
{
pictureBox_infobox.BackgroundImage = global::RegistroAccessi_8Rgt.Properties.Resources.no_parking_32px;
}
}
else
{
pictureBox_infobox.BackgroundImage = global::RegistroAccessi_8Rgt.Properties.Resources.no_parking_32px;
}
Error:
I've done various tests but I don't understand what I'm doing wrong... can someone clarify this for me?
I can do a personnel search in the same way but I can't do it for vehicle number plates.
I am clicking a button accordingly models display in the scene.When I click the same button again the same model appears twice.How to stop this. i.e. If model/Gameobject already loaded then no need to load again.
I am using Gameobject.find here.When I click one button models corresponding model comes and other model present in the scene disappears.At this time Gameobject.find will not work since other model is hidden.Any better solution.Please look at the code below too many if else. :)
private string _assetname;
public void LoadAsstBundles(int choice)
{
if (choice == 1)
{
_assetname = “Chair1”;
}
else if (choice == 2)
{
_assetname = “Chair2”;
}
else if (choice == 3)
{
_assetname = “Chair3”;
}
if (_assetsBundle == null)
{
Debug.Log("Could Not load AssetBundles");
}
else
{
if (GameObject.Find(_assetname + "(Clone)"))
{
Debug.Log("Already Loaded");
}
else
{
var asset = _assetsBundle.LoadAsset(_assetname);
int childcounts = ParentTransform.childCount;
Debug.Log("Asset name nw ==" + asset.name);
Debug.Log("Asset Bundles Loaded");
var go = (GameObject)Instantiate(asset, ParentTransform);
int option = go.transform.GetSiblingIndex();
int childcount = ParentTransform.childCount;
for (int i = 0; i < childcount; i++)
{
if (option == i)
{
ParentTransform.GetChild(i).gameObject.SetActive(true);
continue;
}
ParentTransform.GetChild(i).gameObject.SetActive(false);
}
}
}
}
Instead of using Find at all rather work with variables, store the references you get when instantiating the objects and reuse them later e.g.
// Here you store the reference from
// Assetbundle.LoadAsset
private object loadedAsset;
// Here you store the reference to the assets instance itself
private GameObject assetInstance;
public void LoadAsstBundles(int choice)
{
// Is _assetBundle available?
if (!_assetsBundle)
{
Debug.Log("Could Not load AssetBundles", this);
return;
}
// Was the bundle loaded before?
if (!loadedAsset)
{
loadedAsset = _assetsBundle.LoadAsset(_assetname);
if(!loadedAsset)
{
Debug.LogError("unable to load asset", this);
return;
}
Debug.Log("Asset Bundles Loaded", this);
}
// Is the object Instantiated in the scene?
if(!assetInstance)
{
assetInstance = (GameObject)Instantiate(loadedAsset, ParentTransform);
Debug.LogFormat(this, "Instantiated {0}", assetInstance.name);
}
// no need to go by names
// simply get the correct child by index
var selected = assetInstance.transform.GetChild(choice);
// Enable or disable the childs
// simply run through all childs no need to get their names etc
foreach(Transform chair in assetInstance.transform)
{
chair.gameObject.SetActive(chair == selected);
}
}
I used the Parenttransform.find to get the child hidden objects.Bit lengthy.Any updated code are welcome.
public void LoadAsstBundles(int choice)
{
if(choice==1)
{
_assetname = "Chair1";
}
else if(choice == 2)
{
_assetname = "Chair2";
}
else if(choice == 3)
{
_assetname = "Chair3";
}
if (_assetsBundle==null)
{
Debug.Log("Could Not load AssetBundles");
}
else
{
var asset= _assetsBundle.LoadAsset(_assetname);
//if (GameObject.Find(_assetname + "(Clone)"))
//{
// Debug.Log("Already Loaded");
//}
if(ParentTransform.Find(_assetname+ "(Clone)")== true)
{
Debug.Log("Already Loaded");
int childcount = ParentTransform.childCount;
for (int i = 0; i < childcount; i++)
{
if (choice == i)
{
ParentTransform.GetChild(i).gameObject.SetActive(true);
continue;
}
ParentTransform.GetChild(i).gameObject.SetActive(false);
}
}
else
{
Debug.Log("Asset Bundles Loaded");
var go = (GameObject)Instantiate(asset, ParentTransform);
int option = go.transform.GetSiblingIndex();
_loadednames.Add(go.name);
Debug.Log("Sibling index == " + go.transform.GetSiblingIndex());
int childcount = ParentTransform.childCount;
for (int i = 0; i < childcount; i++)
{
if (option == i)
{
ParentTransform.GetChild(i).gameObject.SetActive(true);
continue;
}
ParentTransform.GetChild(i).gameObject.SetActive(false);
}
}
}
}
I have code elsewhere that puts a pointer in a position based on the AttackInfo.Target value. I also have code that determines whether my characters are dead, using the Stats[lifeStatus] array reference, where 0 is dead and 1 is alive.
I want the Target to skip over the values of dead characters so you can only select living characters to attack. However, it seems to have no effect.
Why does it not work?
else if (state == CHARACTER1TARGETCHOICE) {
attackButton.enabled = false;
abilitiesButton.enabled = false;
itemButton.enabled = false;
defendButton.enabled = false;
fleeButton.enabled = false;
if (pointer.GetComponent<SpriteRenderer> ().enabled == false) {
pointer.GetComponent<SpriteRenderer> ().enabled = true;
}
if (Input.GetKeyDown (KeyCode.UpArrow)) {
character1AttackInfo.Target -= 1;
bool checkTargetAlive = false;
while (checkTargetAlive) {
if (enemy1Stats [lifeStatus] == 0 && character1AttackInfo.Target == 1) {
character1AttackInfo.Target -= 1;
} else if (enemy2Stats [lifeStatus] == 0 && character1AttackInfo.Target == 2) {
character1AttackInfo.Target -= 1;
} else if (enemy3Stats [lifeStatus] == 0 && character1AttackInfo.Target == 3) {
character1AttackInfo.Target -= 1;
} else if (enemy4Stats [lifeStatus] == 0 && character1AttackInfo.Target == 4) {
character1AttackInfo.Target -= 1;
} else if (character1AttackInfo.Target == 0) {
character1AttackInfo.Target = 4;
} else {
checkTargetAlive = false;
}
}
}
In this part of the script, the lower the enemy's number, the higher they are on the screen. Therefore, when the up arrow is pressed, the target's number goes down so the cursor will move up. The cursor moving to the different places above the characters already works perfectly. The target just wont skip for some reason. Once an enemy dies and so enemy1Stats == 0 is true and the Target is 1 also, it still doesn't skip.
That block of code isn't called:
bool checkTargetAlive = false;
followed by
while (checkTargetAlive)
As a general comment, I don't quite understand how your game logic is supposed to work, but that block in the question has some definite code smell. That can't possibly be the cleanest way to implement your game logic.
So I just accidentally set checkTargetAlive to false ahead of time. That made it so the while loop never ran, which made the cursor not work correctly. Just setting it to true ahead of time made it work like a charm:
else if (state == CHARACTER1TARGETCHOICE) {
attackButton.enabled = false;
abilitiesButton.enabled = false;
itemButton.enabled = false;
defendButton.enabled = false;
fleeButton.enabled = false;
if (pointer.GetComponent<SpriteRenderer> ().enabled == false) {
pointer.GetComponent<SpriteRenderer> ().enabled = true;
}
if (Input.GetKeyDown (KeyCode.UpArrow)) {
character1AttackInfo.Target -= 1;
bool checkTargetAlive = true;
while (checkTargetAlive) {
if (enemy1Stats [lifeStatus] == 0 && character1AttackInfo.Target == 1) {
character1AttackInfo.Target -= 1;
} else if (enemy2Stats [lifeStatus] == 0 && character1AttackInfo.Target == 2) {
character1AttackInfo.Target -= 1;
} else if (enemy3Stats [lifeStatus] == 0 && character1AttackInfo.Target == 3) {
character1AttackInfo.Target -= 1;
} else if (enemy4Stats [lifeStatus] == 0 && character1AttackInfo.Target == 4) {
character1AttackInfo.Target -= 1;
} else if (character1AttackInfo.Target == 0) {
character1AttackInfo.Target = 4;
} else {
checkTargetAlive = false;
}
}
}
i have a form with more than one NumericUpDown as controls to input answer. i want every input is true for an operation (multiplication, sum etc), NumericUpDown for that operation will be disable. i have used the code below (just for sum operation), but i think its not efficient because i have to make a method to check every operation.
private void IsSumTrue() {
if (add1 + add2 == sum.Value)
{
sum.Enabled = false;
}
}
private void IsDifferenceTrue()
{
if (add1 - add2 == difference.Value)
{
difference.Enabled = false;
}
}
private void IsProductTrue()
{
if (add1 * add2 == product.Value)
{
product.Enabled = false;
}
}
private void IsQuotientTrue()
{
if (add1 / add2 == quotient.Value)
{
quotient.Enabled = false;
}
}
anyone have idea how to make it more efficient with just a method for all operation?
below is my idea, but to check the value is true for every NumericUpDown i don't know how.
private void DisableIfValueIsTrue()
{
foreach(Control control in this.Controls)
{
NumericUpDown value = control as NumericUpDown;
// if(value [NEED HELP]
}
}
Considering your situtaion, you can set a tag for each NumericUpDown in design mode like this:
sum.Tag=1;
square.Tag=2;
etc
Then define some int variables:
int iSum=add1+add2;
int iSquare= //Whatever you want
etc
And finally loop through your controls this way:
foreach (NumericUpDown control in this.Controls.OfType<NumericUpDown>())
{
int intCondition = Convert.ToInt32(control.Tag) == 1
? iSum
: Convert.ToInt32(control.Tag) == 2
? iSquare
: Convert.ToInt32(control.Tag) == 3
? i3
: i4; //You should extend this for your 8 controls
control.Enabled = intCondition == control.Value;
}
OK! Second way I offer
Since you will have to always check 8 different conditions, you could simply forget about looping through the controls and just change your method like this:
private void DisableIfValueIsTrue()
{
sum.Enabled = add1 + add2 != sum.Value;
difference.Enabled= add1 - add2 != difference.Value;
product.Enabled= add1 * add2 != product.Value;
quotient.Enabled= (add2 !=0) && (add1 / add2 != quotient.Value);
//etc
}
I came across this while doing some research and would like to give my solution I used for my situation and hope it helps people. I needed minimum and maximum numbers for a calculation, so mine are named appropriately and I correlated these with some CheckBoxes. I used null in beginning of minimum and end of maximum to account for empty. I also had to create an event handler SubscribeToEvents() shown below.
In my load event for my form:
SubscribeToEvents();
_checkBoxs = new[] { cbXLight, cbLight, cbMedium, cbHeavy, cbXHeavy, cbXXHeavy, cbXXXHeavy };
_minimumsNumericUpDowns = new[] { null, nLightMin, nMediumMin, nHeavyMin, nXHeavyMin, nXXHeavyMin, nXXXHeavyMin };
_maximumsNumericUpDowns = new[] { nXLightMax, nLightMax, nMediumMax, nHeavyMax, nXHeavyMax, nXXHeavyMax, null };
then I created a method:
private void DisableNumericUpDowns()
{
// disable everything:
foreach (var n in _minimumsNumericUpDowns)
{
if (n != null)
n.Enabled = false;
}
foreach (var n in _maximumsNumericUpDowns)
{
if (n != null)
n.Enabled = false;
}
}
The event handler:
private bool _eventsSubscribed;
private void SubscribeToEvents()
{
if (_eventsSubscribed)
return;
_eventsSubscribed = true;
cbXXHeavy.CheckedChanged += CheckBox_NumericState;
cbXHeavy.CheckedChanged += CheckBox_NumericState;
cbXLight.CheckedChanged += CheckBox_NumericState;
cbHeavy.CheckedChanged += CheckBox_NumericState;
cbLight.CheckedChanged += CheckBox_NumericState;
cbMedium.CheckedChanged += CheckBox_NumericState;
cbXXXHeavy.CheckedChanged += CheckBox_NumericState;
}
Now I can used this to check when they are enabled and if they are greater than or less than 0 if needed in the method CheckBox:
private void CheckBox_NumericState(object sender, EventArgs e)
{
// disable everything
DisableNumericUpDowns();
// see if more than one checkbox is checked:
var numChecked = _checkBoxs.Count((cb) => cb.Checked);
// enable things if more than one item is checked:
if (numChecked <= 1) return;
// find the smallest and enable its max:
var smallest = -1;
for (var i = 0; i < _checkBoxs.Length; i++)
{
if (!_checkBoxs[i].Checked) continue;
if (_maximumsNumericUpDowns[i] != null)
{
_maximumsNumericUpDowns[i].Enabled = true;
}
smallest = i;
break;
}
// find the largest and enable its min:
var largest = -1;
for (var i = _checkBoxs.Length - 1; i >= 0; i--)
{
if (!_checkBoxs[i].Checked) continue;
if (_minimumsNumericUpDowns[i] != null)
{
_minimumsNumericUpDowns[i].Enabled = true;
}
largest = i;
break;
}
// enable both for everything between smallest and largest:
var tempVar = largest - 1;
for (var i = (smallest + 1); i <= tempVar; i++)
{
if (!_checkBoxs[i].Checked) continue;
if (_minimumsNumericUpDowns[i] != null)
{
_minimumsNumericUpDowns[i].Enabled = true;
}
if (_maximumsNumericUpDowns[i] != null)
{
_maximumsNumericUpDowns[i].Enabled = true;
}
}
}
So I can check each state as required:
I want to check if Extra Light is check:
// Extra Light
if (!cbXLight.Checked) return;
if (nXLightMax.Enabled == false)
{
_structCategoryType = XLight;
CheckStructureSheets();
}
else
{
if (nXLightMax.Value > 0)
{
_dMax = nXLightMax.Value;
_structCategoryType = XLight;
CheckStructureSheets();
}
else
{
MessageBox.Show(#"Extra Light Max cannot be zero (0)");
}
}
and next light checks both:
// Light
if (cbLight.Checked)
{
if (nLightMin.Enabled == false && nLightMax.Enabled == false)
{
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
if (nLightMin.Enabled && nLightMin.Value > 0)
{
if (nXLightMax.Enabled && nLightMin.Enabled && nLightMax.Enabled == false)
{
_dMin = nLightMin.Value;
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
if (nLightMax.Value > 0)
{
_dMin = nLightMin.Value;
_dMax = nLightMax.Value;
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
MessageBox.Show(#"Light Max cannot be zero (0)");
return;
}
}
}
else if (nLightMin.Enabled == false && nLightMax.Enabled)
{
if (nLightMax.Value > 0)
{
_dMax = nLightMax.Value;
_structCategoryType = Light;
CheckStructureSheets();
}
else
{
MessageBox.Show(#"Light Max cannot be zero (0)");
}
}
else
{
MessageBox.Show(#"Light Min cannot be zero (0)");
return;
}
}
}
Hope this helps someone.
Tim
thanks for #AlexJoliq and #BrettCaswell. just want to inform that before Alex edited his answer from using "==" to "!=", i (thought) already solved the problem. but i don't know where is the more effective and efficient way, alex's or mine.
below is my code for DisableIfValueIsTrue():
if (add1 + add2 == sum.Value) sum.Enabled = false;
if (add1 - add2 == difference.Value) difference.Enabled = false;
if (add1 * add2 == product.Value) product.Enabled = false;
if (add1 / add2 == quotient.Value) quotient.Enabled = false;
I have written the below function to select a numeric string such as 1,23,000.00
In the WebBrowser I am trapping Double_Click Event and then passing the selected range to the below function.
lets say the initial selection was 000 and my target is to select the whole string as mentioned above.
myRange=doc.selection.createRange()
myRange=SelectCSNumbers(myRange)
I am returning a Range object from the below function. The issue here is
return tmpRange.duplicate();//here it should terminate
count++;
when I am returning the final range this method is getting called again
How I dont know, Can anyone pointout my mistake.
private mshtml.IHTMLTxtRange SelectCSNumbers(mshtml.IHTMLTxtRange myRange)
{
mshtml.IHTMLTxtRange tmpRange = myRange.duplicate();
string[] strInt = tmpRange.text.Split(',');
bool result = false;
result = CheckText(tmpRange, strInt, result);
if (result && count==0)//
{
//Expand the Range with a single Character
tmpRange.expand("character");
if (tmpRange.text.Length > myRange.text.Length)
{
if (tmpRange.text.IndexOf(' ') == -1) //if no space is found that means the selection is not proper
{
//Check for ,/.
if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
{
if (tmpRange.text.IndexOf('.') == -1)
{
//EOS
}
else
{
//. is found
SelectCSNumbers(tmpRange.duplicate());
}
}
else
{
SelectCSNumbers(tmpRange.duplicate());
}
}
else if (tmpRange.text.IndexOf(' ') != -1)
{
tmpRange = myRange.duplicate();
tmpRange.moveStart("character", -1);
if (tmpRange.text.IndexOf(' ') == -1) //if no space is found that means the selection is not proper
{
//Check for ,/.
if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
{
if (tmpRange.text.IndexOf('.') == -1)
{
//EOS
}
else
{
//. is found
SelectCSNumbers(tmpRange.duplicate());
}
}
else
{
SelectCSNumbers(tmpRange.duplicate());
}
}
}
}
else if (tmpRange.text.Length == myRange.text.Length)
{
tmpRange = myRange.duplicate();
tmpRange.moveStart("character", -1);
if (tmpRange.text.Length == myRange.text.Length)
{
//tmpRange = null;
return tmpRange.duplicate();//here it should terminate
count++;
}
else if (tmpRange.text.IndexOf(' ') == -1) //if no space is found that means the selection is not proper
{
if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
{
if (tmpRange.text.IndexOf('.') == -1)
{
//EOS
}
else
{
//. is found
SelectCSNumbers(tmpRange.duplicate());
}
}
else
{
SelectCSNumbers(tmpRange.duplicate());
}
}
}
}
return tmpRange.duplicate();
}
This doesn't help immediately, but addresses a bigger problem
This code needs to be refactored. It will cause problems for you down the line. You have copy-pasted code that will be a pain to take care of. And also, it makes it harder for others to help.
Here is a suggestion for a refactoring (Not Tested)
private mshtml.IHTMLTxtRange SelectCSNumbers(mshtml.IHTMLTxtRange myRange)
{
mshtml.IHTMLTxtRange tmpRange = myRange.duplicate();
string[] strInt = tmpRange.text.Split(',');
bool result = false;
result = CheckText(tmpRange, strInt, result);
if (result && count==0)//
{
//Expand the Range with a single Character
tmpRange.expand("character");
if (tmpRange.text.Length > myRange.text.Length)
{
if (tmpRange.text.IndexOf(' ') == -1) //if no space is found that means the selection is not proper
{
SomeOtherFunction(tmpRange);
}
else if (tmpRange.text.IndexOf(' ') != -1)
{
tmpRange = myRange.duplicate();
tmpRange.moveStart("character", -1);
SomeOtherFunction(tmpRange);
}
}
else if (tmpRange.text.Length == myRange.text.Length)
{
tmpRange = myRange.duplicate();
tmpRange.moveStart("character", -1);
if (tmpRange.text.Length == myRange.text.Length)
{
//tmpRange = null;
return tmpRange.duplicate();//here it should terminate
count++;
}
else if (tmpRange.text.IndexOf(' ') == -1) //if no space is found that means the selection is not proper
{
SomeOtherFunction(tmpRange);
}
}
}
return tmpRange.duplicate();
}
private void SomeOtherFunction(mshtml.IHTMLTxtRange tmpRange)
{
if (tmpRange.text.IndexOf(',') == -1)//if NO Comma is found
{
if (tmpRange.text.IndexOf('.') == -1)
{
//EOS
}
else
{
//. is found
SelectCSNumbers(tmpRange.duplicate());
}
}
else
{
SelectCSNumbers(tmpRange.duplicate());
}
}
Random guess:
if (tmpRange.text.Length == myRange.text.Length)
{
count++;
return tmpRange.duplicate();
}
If you put count++ after the return statement, it will never be executed.