C# XNA Creating object on-click -


i creating program simulates ants running around, collecting food , depositing in nest. want user able click , add nest object @ cursor point. want object created added list of nests.

so far, have tried in update method in main game class.

        mousestatecurrent = mouse.getstate();            if (mousestatecurrent.leftbutton == buttonstate.pressed)         {             int foodwidth = 50;             int foodheight = 50;              int x = mousestatecurrent.x;             int y = mousestatecurrent.y;              foodposition = new rectangle(x, y, foodwidth, foodheight);             food = new stationaryfood(foodposition);              foodlist.add(food);                     } 

this compiles when click game crashes , error saying when food object drawn in 'draw' method, texture food null. understand why happening, have tried load in textures follows in loadcontent() method in main game class

foreach (stationaryfood f in foodlist)         {              f.characterimage = foodimage;         } 

and here set/ in separate class food object

    public texture2d characterimage     {         set         {             foodimage = value;          }                 {             return foodimage;         }     } 

here method in food object class error

 public void draw(spritebatch spritebatch, list<stationaryfood> foodlist)     {         foreach (stationaryfood food in foodlist)         {             spritebatch.draw(foodimage, foodboundingrectangle, foodcolor);         }     } 

the foodimage variable null. know because when loadcontent() loads image there nothing in list @ point! don't know how fix this! simple im new @ programming! appreciated , hope didn't explain incomprehensibly.

edit: ignore i've posted , removed. after second @ question realized there issues answer.

when create new stationaryfood in update method, never assign new stationaryfood's characterimage. should have texture member in game1 called foodimage. you'll load foodimage's texture in loadcontent method. anytime create new stationaryfoods in update, need assign new food's characterimage foodimage , position created via mouse position.

so lets stationaryfood class looks like:

class stationaryfood {     public texture2d characterimage { get; set; }     public rectangle position { get; set; }      public stationaryfood(texture2d image, rectangle position) {         characterimage = image;         position = position;     } } 

so texture member scoped game1:

texture2d foodimage; 

in loadcontent method:

foodimage = content.load<texture2d>("path texture"); 

in update:

if (mousestatecurrent.leftbutton == buttonstate.pressed) {     int foodwidth = 50;     int foodheight = 50;      int x = mousestatecurrent.x;     int y = mousestatecurrent.y;      var foodposition = new rectangle(x, y, foodwidth, foodheight);     var food = new stationaryfood(foodimage, foodposition);      // no need scope foodposition or food game1 since creating , adding list here      foodlist.add(food);             } 

in draw:

foreach (stationaryfood food in foodlist) {     spritebatch.draw(food.characterimage, food.position, food.color); } 

now i'm guessing foodlist member of game1 there's no need pass list draw. if that's not case though pass away.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -