I am writing a program in Windows phone emulator Visual Studio 2010. The program is a scrolling tile engine which has a set of waypoints for an attacker to follow on a path. I have this part working and now I am trying to get a turret to rotate in the direction that the attacker is taking. I have most of the code written for this but I have a problem.
I cannot access attacker1 from the Game1 class.
public class Grid
{
public Attacker attacker1;
List<Texture2D> attacker = new List<Texture2D>();
public void AddTextures(Texture2D texture)<pre lang="text">
{
attacker.Add(texture);
}
public Grid()
{
waypoints.Enqueue(new Vector2(2, 2) * 128);
waypoints.Enqueue(new Vector2(2, 3) * 128);
waypoints.Enqueue(new Vector2(5, 3) * 128);
attacker1 = new Attacker(waypoints.Peek(), .75f);
attacker1.SetWayPoints(waypoints);
}
Public void Draw()
{
var attacker2 = attacker[0];
var newLocation = new Vector2 { X = attacker1.Location.X - (int)Camera.X,
Y = attacker1.Location.Y - (int)Camera.Y};
spriteBatch.Draw(attacker2, newLocation, Color.White);
}
So in the update method in the Game1 class I have the following code:
if (defender.TargetAttacker == null)
{
List<Attacker> attackers = new List<Attacker>();
attackers.Add(attacker1);
defender.LocateAttacker(attackers);
}
defender.Update(gameTime);
I am getting ERROR : the name attacker1 does not exist in the current context.
I think the problem is because the object is created in the Grid class so it cannot be accessed from the Game1 class.
As the attacker1 is instantiated in the Grid constructor along with the waypoints I cannot figure out what to do here.
It is my first post so and I am learning C# so I hope I explained what I am trying to do ok. I added code from the classes which I felt was appropriate. I have searched forums and I cannot find a solution so far. Any help is much appreciated.