Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
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.
C#
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:

C#
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.
Posted
Updated 26-Apr-13 15:03pm
v2
Comments
[no name] 26-Apr-13 21:09pm    
"I think the problem is because the object is created in the Grid class so it cannot be accessed from the Game1 class.", your thought is correct (based on the information presented here). Your game1 class does not appear to know anything about your grid class. Just because you declare attacker1 as public does not mean that other classes get access to it automatically and globally. Bad methodology btw.

You would either need to pass the existing instance of your grid class to game1, pass attacker1 to your game1 class, create an interface so that game1 can access the attacker1 variable in grid, or a bunch of other ways. Check out http://www.codeproject.com/search.aspx?q=pass+data+between+forms&doctypeid=1%3b2%3b3%3b9%3b10 these articles for more information.
Sergey Alexandrovich Kryukov 26-Apr-13 21:25pm    
After all, read a bit about .NET and the language. Why writing anything without understanding? Read first, then do some exercises, feel it...
—SA
Sergey Alexandrovich Kryukov 26-Apr-13 22:22pm    
By the way, non-private field of a class is a bad style; bad for maintenance. Make it an a property. Never allow yourself names like "attacher1". Auto-generated code style?
I don't think there is anything to discuss here. The code fragment shown does not show the context. I suggest we won't find the "error". Just learn how members, variables, access, types and instances work.
Or, if you wish, formulate the problem in a comprehensive manner and come back.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900