[Dgame] Sprite loading and setting position in another class

Jack via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Aug 25 05:53:21 PDT 2015


On Tuesday, 25 August 2015 at 12:50:50 UTC, Jack wrote:
> So I've been using Dgame to learn about game development and 
> I'm having an error when I'm trying to set a sprite's position 
> initialized and stored in another class.
>
> Basically my code is:
> //Texman.d//////////////////////////////////////
>
> Class Texman
> {
>   //variables
>
> 	void addSprite(string sprite_file, string name)
> 	{
> 		Surface wiki_img = Surface(sprite_file);
> 		Texture wiki_tex = Texture(wiki_img);
> 		sprite_list[name] = new Sprite(wiki_tex);
>                 sprite_list[name].setPosition(1,1); //tried to 
> remedy by this		
> 	}
> }
>
> //GameObject.d////////////////////////////
>
> class GameObject
> {
> Sprite sprite;
> float location_x;
> float location_y;
> //other variables
>
> 	void setLocation(float x, float y)
> 	{
> 		sprite.setPosition(x,y);
> 		location_x = x;
> 		location_y = y;
> 	}
> }
>
> //main.d/////////////////////////////////
>
>
> 	void create()
> 	{
> 		wnd = Window(1024, 720, "Dgame Test");
> 		sample_object = new GameObject();
> 		texman = new TextureManager();
> 		texman.addSprite("images.png", "kirino");
> 		sample_object.setSprite(texman.getSprite("kirino"));
> 		sample_object.setLocation(500,500);
> 	}
> //////////////////////////////////////////////////////
>
> And I'm having an error namely
> "First-chance exception: core.exception.AssertError null"
> pointing at the setLocation on GameObject.d.
>
> I tried to remove the "setLocation" method and tried to set 
> Location on TextureManager itself and it worked.
>
> So is setting the position of a sprite outside of the class it 
> initialized into forbidden, or am I doing something wrong here?

Edit:

  Basically my code is:
  //Texman.d//////////////////////////////////////

  Class TextureManager
  {
   //variables

	void addSprite(string sprite_file, string name)
	{ 		
                 Surface wiki_img = Surface(sprite_file);
  		Texture wiki_tex = Texture(wiki_img);
		sprite_list[name] = new Sprite(wiki_tex);
                 sprite_list[name].setPosition(1,1); //tried to
  remedy by this		
	}
  }


More information about the Digitalmars-d-learn mailing list