Xbox LIVE Indie Games
Sort Discussions: Previous Discussion Next Discussion
Page 1 of 1 (5 posts)

"...Does not contain a constructor that takes '0' arguments".

Last post 6/3/2010 5:22 PM by Bob Chambers. 4 replies.
  • 6/3/2010 11:49 AM

    "...Does not contain a constructor that takes '0' arguments".

    I'm trying to make a 2D side-scrolling space based shooter. Currently I have a ship moving around, firing it's gun. However, there's nothing to shoot. So I "borrowed" some code from chapter 8 of the beginner's guide (http://creators.xna.com/en-US/education/gettingstarted/bg2d/chapter8) to make a new Enemy class that will randomly spawn enemies and move them across the screen. The class looks like this:

    1 using System; 
    2 using System.Collections.Generic; 
    3 using Microsoft.Xna.Framework; 
    4 using Microsoft.Xna.Framework.Audio; 
    5 using Microsoft.Xna.Framework.Content; 
    6 using Microsoft.Xna.Framework.Graphics; 
    7 using Microsoft.Xna.Framework.Input; 
    8 using Microsoft.Xna.Framework.Storage; 
    9  
    10 namespace Spacegame 
    11
    12     class Enemy 
    13     { 
    14         public Texture2D sprite; 
    15         public Vector2 position; 
    16         public float rotation; 
    17         public Vector2 center; 
    18         public Vector2 velocity; 
    19         public bool alive; 
    20  
    21         Rectangle viewportRect; 
    22         Random random = new Random(); 
    23         Enemy[] theenemies; 
    24  
    25         const float maxEnemyHeight = 0.1f; 
    26         const float minEnemyHeight = 0.5f; 
    27         const float maxEnemyVelocity = 5.0f; 
    28         const float minEnemyVelocity = 1.0f; 
    29  
    30         public Enemy(Texture2D loadedTexture) 
    31         { 
    32             rotation = 0.0f; 
    33             position = Vector2.Zero; 
    34             sprite = loadedTexture; 
    35             center = new Vector2(sprite.Width / 2, sprite.Height / 2); 
    36             velocity = Vector2.Zero; 
    37             alive = false
    38         } 
    39  
    40         public void Update(GameTime theGameTime) 
    41         { 
    42             foreach (Enemy enemy in theenemies) 
    43             { 
    44                 if (enemy.alive) 
    45                 { 
    46                     enemy.position += enemy.velocity; 
    47                     if (!viewportRect.Contains(new Point( 
    48                         (int)enemy.position.X, 
    49                         (int)enemy.position.Y))) 
    50                     { 
    51                         enemy.alive = false
    52                     } 
    53                 } 
    54                 else 
    55                 { 
    56                     enemy.alive = true
    57                     enemy.position = new Vector2( 
    58                         viewportRect.Right, 
    59                         MathHelper.Lerp( 
    60                         (float)viewportRect.Height * minEnemyHeight, 
    61                         (float)viewportRect.Height * maxEnemyHeight, 
    62                         (float)random.NextDouble())); 
    63                     enemy.velocity = new Vector2( 
    64                         MathHelper.Lerp( 
    65                         -minEnemyVelocity, 
    66                         -maxEnemyVelocity, 
    67                         (float)random.NextDouble()), 0); 
    68                 } 
    69  
    70             } 
    71         } 
    72  
    73         public void Draw(SpriteBatch SpriteBatch) 
    74         { 
    75             foreach (Enemy enemy in theenemies) 
    76             { 
    77                 if (enemy.alive) 
    78                 { 
    79                     enemy.Draw(SpriteBatch); 
    80                 } 
    81             } 
    82         } 
    83  
    84     } 
    85


    However, when I declare this class in the Initialise method in Game1, I get the following error: 'Spacegame.Enemy' does not contain a constructor that takes '0' arguments. What does this mean and how can I fix it?
  • 6/3/2010 11:58 AM In reply to

    Re: "...Does not contain a constructor that takes '0' arguments".

    It means that you're doing:
    Enemy foo = new Enemy(); 
    when Enemy's constructor requires a Texture2D, as seen here in the code you posted:
    public Enemy(Texture2D loadedTexture)  
        ... 
    That means that you need to pass a texture2d into Enemy's constructor when you instantiate it, like this:
    Texture2D enemyTexture = Content.Load<Texture2D>("enemyship"); //make sure 'enemyship' is in your content project 
     
    Enemy foo = new Enemy(enemyTexture); 

  • 6/3/2010 2:32 PM In reply to

    Re: "...Does not contain a constructor that takes '0' arguments".

    Thanks! That's solved that problem. However, another problem has emerged. When I press F5, I get the "NullReferenceException was unhandled - Object reference not set to an instance of an object." message popping up in the enemy class. It's pointing at the following line:

    foreach (Enemy enemy in theenemies) 

    Specifically, it's pointing at the word in.

    I don't know much about programming, but something's telling me it's related to the array declaration "Enemy[] theenemies;"

    What do I do now?
  • 6/3/2010 2:44 PM In reply to

    Re: "...Does not contain a constructor that takes '0' arguments".

    That's because you didn't instantiate the array, you only declared it. I'd recommend sticking with lists while you're still learning, since they're more functional and give you fewer chances to screw up. ;)
    List<Enemy> theenemies = new List<Enemy>(); 
     
    ... 
     
    theenemies.Add(new Enemy(enemyTex)); 
     
    ... 
     
    foreach(Enemy enemy in theenemies) 
        ... 

    I highly encourage you to start with some basic C# and XNA tutorials before going much further. xnadevelopment.com is a terrific site for beginners, as is the Getting Started video tutorials on this site. And you have read this page, right? :)
  • 6/3/2010 5:22 PM In reply to

    Re: "...Does not contain a constructor that takes '0' arguments".

    UberGeekGames:

    I highly encourage you to start with some basic C# and XNA tutorials before going much further. xnadevelopment.com is a terrific site for beginners, as is the Getting Started video tutorials on this site. And you have read this page, right? :)

    Thanks for the advice. I have done XNA before, but that was over a year ago and it's all slipped by me.
Page 1 of 1 (5 posts) Previous Discussion Next Discussion