Page 1 of 2

Why does this code get the reticle to change back to white when you're no longer over the specified line in one directio

Posted: August 4th, 2019, 10:47 pm
by TheCollector
Why does this code get the reticle to change back to white when you're no longer over the specified line in one direction but not the other (works to the left but not to the right).

Code: Select all

if (chx >= enemy1x - 55)
{
	if (chx <= enemy1x - 49)
	{
		InhibitEnemyTargetedY = true;
		EnemyTargetedX = true;
	}
	else
	{
		chg = 255;
		chb = 255;
		chr = 255;
	}

	if (InhibitEnemyTargetedY == true)
	{
		if (EnemyTargetedX == true)
		{
			chg = 0;
			chb = 0;
			chr = 255;
		}
		else
		{
			chg = 255;
			chb = 255;
			chr = 255;
		}
	}
else
{
	chg = 255;
	chb = 255;
	chr = 255;
}

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 4th, 2019, 11:16 pm
by TheCollector
Do I need to literally code a portion so that when x equals greater than that number the reticle goes back to white? Shouldn't I just be able to do that with an else statement somewhere like I did for the left side to go back?

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 6:13 am
by albinopapa
Let me see, if enemy1x is 200 and chx is > 151, what do you think will happen?

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 6:20 am
by albinopapa
Kind of feeling trolled at this point.

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 1:29 pm
by TheCollector
It's going to run the next line of code, no? and if the next part isn't true it should go to the else?

Why do you say feeling trolled? Are my questions too stupid or just too many of them? lol probably both. I do appreciate the help. I'm new to this and sometimes a second pair of eyes really helps.

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 2:15 pm
by TheCollector
See, I'm trying to understand this better because I also coded the Y coordinates and they're not working right. No matter where I put the code for the reticle to turn back to white if you're not over enemy1y it doesn't run that code. It worked so easy with X. I tried doing basically the same thing over but for the Y and it doesn't work the same. InhibitEnemyTargetedX and EnemyTargetedY should only become true if you're within the coordinates specified. That works just fine. If you're not in the coordinates specified shouldn't it run the else portion and go back to white? Or shouldn't it run the code to go back to white if you're outside of those coordinates bc I put that in there? What am I missing here? I understand why X is working and why it goes back to white but why doesn't Y?

//Target Enemy1y
if (chy >= enemy1y - 3 && chy <= enemy1y + 5)
{
InhibitEnemyTargetedX = true;
EnemyTargetedY = true;
}
else
{
chg = 255;
chb = 255;
chr = 255;
}

if (InhibitEnemyTargetedX && EnemyTargetedY == true)
{
chg = 0;
chb = 0;
chr = 255;
}
else
{
chg = 255;
chb = 255;
chr = 255;
}

if (InhibitEnemyTargetedX && EnemyTargetedY == false)
{
chg = 255;
chb = 255;
chr = 255;
}

if (chy >= enemy1y + 6)
{
InhibitEnemyTargetedX = false;
EnemyTargetedY = false;
}

if (chy <= enemy1y - 4)
{
InhibitEnemyTargetedX = false;
EnemyTargetedY = false;
}


//Target enemy1x
if (chx >= enemy1x - 55 && chx <= enemy1x - 49)
{
InhibitEnemyTargetedY = true;
EnemyTargetedX = true;
}
else
{
chg = 255;
chb = 255;
chr = 255;
}

if (InhibitEnemyTargetedY && EnemyTargetedX == true)
{
chg = 0;
chb = 0;
chr = 255;
}
else
{
chg = 255;
chb = 255;
chr = 255;
}

if (chx >= enemy1x - 50)
{
chg = 255;
chb = 255;
chr = 255;
}

if (chx < enemy1x - 55)
{
chg = 255;
chb = 255;
chr = 255;
}

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 2:47 pm
by TheCollector
I cleaned it up bc I had quite a bit of unnecessary code in there. Look at this instead:

//Target Enemy1y
if (chy >= enemy1y - 4 && chy <= enemy1y + 6)
{
EnemyTargetedY = true;
}

if (EnemyTargetedY == true)
{
chg = 0;
chb = 0;
chr = 255;
}

if (chy >= enemy1y + 6)
{
chg = 255;
chb = 255;
chr = 255;
}

if (chy <= enemy1y - 4)
{
chg = 255;
chb = 255;
chr = 255;
}


//Target enemy1x
if (chx >= enemy1x - 55 && chx <= enemy1x - 49)
{

EnemyTargetedX = true;
}

if (EnemyTargetedX == true)
{
chg = 0;
chb = 0;
chr = 255;
}

if (chx >= enemy1x - 50)
{
chg = 255;
chb = 255;
chr = 255;
}
if (chx < enemy1x - 55)
{
chg = 255;
chb = 255;
chr = 255;
}

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 5:15 pm
by TheCollector
Nevermind. Tried out this instead and it worked:

//Target enemy1x & enemy1y
if (chx >= enemy1x - 55 && chx <= enemy1x - 51 || chy >= enemy1y - 3 && chy <= enemy1y + 5)
{
chg = 0;
chb = 0;
chr = 255;
}

if (chx >= enemy1x - 48 || chx <= enemy1x - 56)
{
chg = 255;
chb = 255;
chr = 255;
}

if (chy >= enemy1y + 6 || chy <= enemy1y - 4)
{
chg = 255;
chb = 255;
chr = 255;
}

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 5th, 2019, 6:57 pm
by albinopapa
No, not because the questions are stupid or there are too many, it's that I have given you the answer in previous posts and you just rephrase the question in a new post.

One of the lessons in the tutorials goes over how to turn the reticle red in a specific region also, so if you are following along, you should have been able to figure it out pretty easily.

Given that your last post has finally gotten rid of the boolean flags, something I had suggested on a previous post, also

Code: Select all

//Target enemy1x & enemy1y
if (chx >= enemy1x - 55 && chx <= enemy1x - 51 || chy >= enemy1y - 3 && chy <= enemy1y + 5)
{
   chg = 0;
   chb = 0;
   chr = 255;
}
this is pretty close to what I've suggested in a previous post, though I think the || should have been && in this if statement. With the logical OR "||", your cross hair should turn red if the reticle is anywhere along the Y axis if between enemy1x - 55 and enemy1x - 51, which I'm guessing is why you have the two if statements below that.

If you'd taken my suggestion from the earlier post or followed the tutorials, you could avoid this by just checking of the cross hair is between the left side and right side of the graphic bounds as well as the top and bottom of the graphic bounds.

Code: Select all

int left = enemy1x - 55;
int top = enemy1y - 3;
int right = enemy1x - 51;
int bottom = enemy1y + 5;
if( ( chx >= left && chx <= right ) &&
    ( chy >= top && chy <= bottom ) )
{
   chr = 255;
   chg = 0;
   chb = 0;
}
else
{
   chr = 255;
   chg = 255;
   chb = 255;
}
You could also make functions to make this algorithm reusable.

Code: Select all

bool IsBetween( int test, int low, int high )
{
   return ( test >= low && test <= high );
}
bool IsInsideRegion( int X, int Y, int Left, int Top, int Right, int Bottom )
{
   return IsBetween( X, Left, Right ) && IsBetween( Y, Top, Bottom );
}
Now, your game code could be:

Code: Select all

if( IsInsideRegion( chx, chy, enemy1x - 55, enemy1y - 3, enemy1x - 51, enemy1y + 5 ) )
{
   chr = 255;
   chg = 0;
   chb = 0;
}
else
{
   chr = 255;
   chg = 255;
   chb = 255;
}
Soon you'll learn about structures and classes. This will help you put pieces together to create a full object like an Enemy or CrossHair object.

Code: Select all

struct CrossHair
{
   int x, y;
   Color color;
};
struct Enemy
{
   int x, y, width, height;
   Color color;
};
struct Rect
{
   int left, top, right, bottom;
};

bool IsInsideRect( int X, int Y, Rect rect )
{
   return IsBetween( X, rect.left, rect.right ) && IsBetween( Y, rect.top, rect.bottom );
}
bool IsEnemyTargeted( CrossHair const& ch, Enemy const& enemy )
{
   Rect enemy_rect;
   enemy_rect.left = enemy.x;
   enemy_rect.top = enemy.y;
   enemy_rect.right = enemy.x + enemy.width;
   enemy_rect.bottom = enemy.y + enemy.height;

   return IsInsideRect( ch.x, ch.y, enemy_rect )
}

Enemy enemy1, enemy2, enemy3;
CrossHair ch;
if( IsEnemyTargeted( ch, enemy1 ) || IsEnemyTargeted( ch, enemy2 ) || IsEnemyTargeted( ch, enemy3 ) )
{
   ch.color = Colors::Red;
}
else
{
   ch.color = Colors::White;
}
You'll also learn about arrays, which makes it possible to use loops to iterate through your enemies.

Code: Select all

Enemy enemies[3];
bool make_red = false;
for( Enemy const& enemy : enemies )
{
   // Can't just do the if statement one at a time since the cross hair is most likely
   // only going to cover one enemy at a time.  If that heppens, it would immediately 
   // make the cross hair white again, so we store the value using an OR which builds
   // up a true if ANY of them are true.
   make_red = IsEnemyTargeted( ch, enemy ) || make_red;
}
if( make_red )
{
   ch.color = Colors::Red;
}
else
{
   ch.color = Colors::White;
}

Re: Why does this code get the reticle to change back to white when you're no longer over the specified line in one dire

Posted: August 6th, 2019, 2:51 pm
by TheCollector
Sorry for the rephrasing. In between going through the tutorial I'm also trying to come up with some ideas and do some stuff on my own ( I may incidentally be trying to do stuff that's in the tutorial but that I just haven't gotten to yet- like the crosshair turning red. I'm not there yet but it's something I decided to try to figure out on my own first). I think from now on I'm going to give my coding problems some extra time before asking for help. I did try what you suggested, actually I tried a couple things you suggested and got all messed up bc they wouldn't work bc I was using = instead of == lol. Total newb mistake. so I had given up on trying some of that and then came back to what I last posted and it worked. Maybe for now I'll just consider following along with the tutorial since most of the stuff I want to learn to do seems to be in there anyway.