2 Replies - 247 Views - Last Post: 26 April 2013 - 08:27 PM Rate Topic: -----

#1 Shane79  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 26-April 13

Accessing

Posted 26 April 2013 - 05:08 PM

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)
        {
            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.

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing

#2 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 106
  • Joined: 20-December 11

Re: Accessing

Posted 26 April 2013 - 08:07 PM

Attacker is created in a separate class, right, but you can access it in your Game1 class if it's public. Which it is. Your problem is that you're trying to access attacker as though it's created in the Game1 class. Try accessing attacker1 under your Grid by changing

attackers.Add(attacker1);

to...
attackers.Add(myGrid.attacker1);

I'd read up more on scopes. Here's an MSDN page: http://msdn.microsof...2(v=vs.71).aspx
Was This Post Helpful? 1
  • +
  • -

#3 pharylon  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 83
  • Joined: 01-September 12

Re: Accessing

Posted 26 April 2013 - 08:27 PM

You're right. The fact that the attacker1 was instantiated in Grid means it can't be accessed in Game1. You're dealing with something called "scope." Fortunately, the solution is pretty simple. I'm not sure if you have Grid instantiating the Game class or the other way around, but if the Grid creates the Game, then you can do what Julyuary said. If it's the other way around (the Game creates the grid) then you should create the attacker in the Game class instead.

Another way to do it would be to pass the reference to the attacker between the classes (such as in the class constructors). Or, whatever class is in charge of updating the turns (this would be the class with a timer if it has one) can be in charge of getting the attacker reference out of the Grid and passing it to the Game in the method that gets called when you update turns.

In general, in a game or any project like this, you often want one central "controller" class who's job it is to tell the other classes to update (because a turn passed or it received user input) and to pass them references as needed.

This post has been edited by pharylon: 26 April 2013 - 08:39 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1