Sign in to follow this  

I am not sure why this is not working.

This topic is 1656 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I have been pondering an error I have had for a few days now and I just cannot seem to figure out the problem. I have an abstract base class called Entity which my PlayerEntity class is inheriting from. Entity has a function called setPosition and takes no parameters I am trying to use the setPosition function in my PlayerEntity's constructor however when I do I get a window that says Debug Assertion Failed! and then says vector subscript is out of range. I have narrowed down the problem to my setPosition function. Thank you for any help.

 

Entity::setPosition:

void Entity::setPositon(){
	
	//set the choords of the vertecies on the vertex array
	m_sfEntityBox[0].position = sf::Vector2f(m_sfPosition.x, m_sfPosition.y);
	m_sfEntityBox[1].position = sf::Vector2f(m_sfPosition.x + m_sfSize.x, m_sfPosition.y);
	m_sfEntityBox[2].position = sf::Vector2f(m_sfPosition.x + m_sfSize.x, m_sfPosition.y + m_sfSize.y);
	m_sfEntityBox[3].position = sf::Vector2f(m_sfPosition.x, m_sfPosition.y + m_sfSize.y);
}

Share this post


Link to post
Share on other sites

But it is not a vector. sf_EntityBox is a sf::VertexArray of sf::Quads. Sorry, I probably should have said that.

Share this post


Link to post
Share on other sites

Internally, SFML's VertexArray uses a vector for storage. (Well at least some versions of SFML anyways.) And like a vector, it needs to be sized properly before you use operator[] on it. 

Share this post


Link to post
Share on other sites
Well, in this case have you ensured that m_sfEntityBox.getVertexCount() returns at least four and called resize if that is not the case? On a side note, according to code available online, sf::VertexArray is just an extremely thin wrapper around an std::vector.

Share this post


Link to post
Share on other sites

Well, in this case have you ensured that m_sfEntityBox.getVertexCount() returns at least four and called resize if that is not the case? 

Yes that was the problem thanks. I set the sf::VectorArray in the constructor of the Entity, and the way I was doing it before worked, which was sloppy and looked bad so I changed it. Thanks.

 

Edit: For the function void Entity::setPosition() do I need to use "this" for all the variables? I would think so because I am not using the function for the Entity but for a child class. It works as is, I am just wondering.

Edited by Crusable

Share this post


Link to post
Share on other sites

I would think so because I am not using the function for the Entity but for a child class. It works as is, I am just wondering.


You do not need to, neither is it common to write it. I would strongly suggest getting on a more firm footing with C++ before adding the additional complication of external libraries like SFML.

Share this post


Link to post
Share on other sites
Sign in to follow this