SRL Archive - C/C++ Help and Tutorials

[C#/XNA] Why doesn't my updateMonster work?


JPHamlett

2011-09-22 13:22:52

Ok, so in the game Mario, the monster I am working on moves on a set y axis and changes x axis. However, when I start to run my program my monsterSprite moves to the left (as it should) till it hits the coord (0, 480) then it starts to go right (as it should). Now this is where my problem is…when it hits the xMax (100) IT DOESNT GO BACK LEFT!!!!!!!

Here's my monster class

http://pastebin.com/qXEdrFbP



Keep in mind it is just a test class so it has test positions….

The function that isnt working is

public void updateMonster()
{
if ((monsterPos.X >= xMax) || (monsterPos.X <= xMin)){

if (left.Equals(true)){
left = false;
}
if (left.Equals(false)){
left = true;
}
}
if (!left)
{
monsterPos.X -= speed;
}

if (left)
{

monsterPos.X += speed;
}

}

sf411

2011-09-22 14:16:53

I do not understand what the problem is, can you be a little more descriptive?

Brandon

2011-09-22 19:36:41

Hmm my vs 2010 ultimate won't open the content.proj file.. ahh well..

sf411

2011-09-22 19:51:27

You need to install XNA. I had the same issue, but I recently reformatted and had not re-installed it yet.

JPHamlett

2011-09-23 00:10:51

Updated my problem….

JPHamlett

2011-09-23 10:29:59

Any help?

Brandon

2011-09-23 10:38:37

I believe it may be that .Equals your using.. in C#/C++ that is usually only used for strings and when comparing something, you should use the == comparison operator. I have personally never seen it used for anything other than strings (usually system::string^). I could be wrong though.

But try changing that left.Equals to if(left == true){…..} and lemmie know if that works.

So:


if (left == true){
left = false;
}
if (left == false){
left = true;
}

Frement

2011-09-23 10:44:51

From: ggzz


if (left == true){
left = false;
}
if (left == false){
left = true;
}


That would make it always return true.

You need an ELSE statement to make it work. Theres your problem, first it checks if left is true, if it is, it sets it to false, and next it checks if its false, and as set by the first if…then it was false, and it sets it back to true.

Hope this helps.

sf411

2011-09-23 12:05:29

From: Frement
That would make it always return true.

You need an ELSE statement to make it work. Theres your problem, first it checks if left is true, if it is, it sets it to false, and next it checks if its false, and as set by the first if…then it was false, and it sets it back to true.

Hope this helps.


This is correct, you will just have to adjust your bounding box for the monster since he only goes to 100 on the x-axis.

masterBB

2011-09-23 20:15:52

               if (left.Equals(true)){
left = false;
}
if (left.Equals(false)){
left = true;
}

Should be:

left = !left;

Frement

2011-09-23 21:38:21

From: masterBB
               if (left.Equals(true)){
left = false;
}
if (left.Equals(false)){
left = true;
}

Should be:

left = !left;


The problem has already been pointed out, and that wouldn't fix it.

masterBB

2011-09-24 08:04:12

From: Frement
The problem has already been pointed out, and that wouldn't fix it.


That will fix it and is a lot shorter and cleaner than using else.

Brandon

2011-09-24 09:11:55

From: masterBB
That will fix it and is a lot shorter and cleaner than using else.


That will not fix it… else is easier.. boolean true and false is better than left = !left.. how does that even make sense?

if(left == !left) explain.. there has to be two situations.. if he's already going left.. and if he isn't..

Frement

2011-09-24 09:12:14

From: masterBB
That will fix it and is a lot shorter and cleaner than using else.


Yeah sorry, I misread your post and I was drunk :)

masterBB

2011-09-24 09:42:52

From: ggzz
That will not fix it… else is easier.. boolean true and false is better than left = !left.. how does that even make sense?

if(left == !left) explain.. there has to be two situations.. if he's already going left.. and if he isn't..


You're making asumptions, we're both no proffesional programmers(I'm a bit).

Explanation of the code; Instead of using 6 lines of code you will only use 1. Just left = !left; . This will set the left var to the opposite of it previous value. Like if a Boolean is -1 or 1, this will multiply it by -1, always returning the opposite.

@Frement: The drunk excuse is has always been one of my weaknesses, it's ok ;)

Brandon

2011-09-24 11:25:29

From: masterBB
You're making asumptions, we're both no proffesional programmers(I'm a bit).

Explanation of the code; Instead of using 6 lines of code you will only use 1. Just left = !left; . This will set the left var to the opposite of it previous value. Like if a Boolean is -1 or 1, this will multiply it by -1, always returning the opposite.

@Frement: The drunk excuse is has always been one of my weaknesses, it's ok ;)


Yeah ur right.. I just tried it and it works. I wasn't thinking like that.. I was thinking if u were putting the !left in the if-else statement.