so just to be more specific this question isn't in regards to how to target enemies or anything like that rather its purposed towards a specific targeting issue i have with me enemies in its current state.
So the issue im having is that if i fire a bullet towards an enemy and then before that bullet hits the desired enemy it was initially set too, if my tower changes its target then suddenly my bullet does a u-turn and then targets this new enemy. Which isn't what i want.
I'm using C++. I know this is a pointer issue, that my pointer's memory address changes to point at the new enemy which the tower would be facing, the problem is i just don't know how to solve this, personally for me i don't use pointers that much and unfortunately im just not very good at using them it seems :/. Any help on this issue is greatly appreciated, its been annoying me now for the better part of a few hours, and if anyone could explain what I'm doing wrong here as-well that would be great! So here's my targeting code
for (int numOfEnemies = 0; numOfEnemies < lib.numberOfEnemies; numOfEnemies++)
{
float y = pow(enemies[numOfEnemies].position.y - position.y, 2);
float x = pow(enemies[numOfEnemies].position.x - position.x, 2);
if (sqrt(y + x) < range && enemies[numOfEnemies].alive)
{
cEnemy = &enemies[numOfEnemies];
acquiredTarget = true;
break;
}
}
Simple enough, then this is how i'm passing it in my Projectile class;
bullets.push_back(Projectile(bulletPosX, bulletPosY, 8, 8, damage, 1, speed, *cEnemy, currentUpgradeLevel));
Which specifically then uses this constructor;
Projectile::Projectile(float x, float y, int w, int h, int dmg, int type, float mxSpeed, Enemies& bulletTarget, int towerLevel)
{
position.x = x;
position.y = y;
width = w;
height = h;
bulletAngle = 0;
renderingAngle = 0;
damage = dmg;
active = true;
ySpeed = 0;
xSpeed = 0;
maxSpeed = mxSpeed;
typeOfProjectile = type;
target = &bulletTarget;
}
There you see target is then a pointer to an enemy address, the cEnemy (currentEnemy). What my desired effect would be to have my Projectile store the enemy that I pass into it which then until it hits that enemy, the targeted enemy will forever stay focused on the enemy passed in when the projectile was first created.
Again any help greatly appreciated!
For reference
Enemies = Class Name. enemies = vector array of active Enemies.
cEnemy = (Pointer to Enemies Object)/Enemies* cEnemy.
Enemiesandenemies? – Miro Jul 14 '12 at 13:35