Results 1 to 8 of 8
  1. #1
    Join Date
    Jan 2011
    Posts
    3

    Can't figure out what's wrong. Stuck on the evilMonkeys tutorial.

    So, i am stuck on enemy tutorial, when i try to run my game, it crashes. I debugged it and this pops out: http://puu.sh/BAJu

    It displays at this line:
    Code:
    bool Sprite::isValidLevelMove(int xpos, int ypos)
    {
    	if (level->level[xpos][ypos] != TILE_WALL)   //here it gets stuck
    		return true;
    
    	return false;
    }
    Here is full code of sprite.cpp
    Code:
    #include "sprite.h"
    
    
    Sprite::Sprite(Level *l, DrawEngine *de, int s_index, float x, float y, int i_lives)
    {
    	drawArea = de;
    
    	pos.x = x;
    	pos.y = y;
    	
    	spriteIndex = s_index;
    
    	numLives = i_lives;
    
    	facingDir.x = 1;
    	facingDir.y = 0;
    
    	classID = SPRITE_CLASSID;
    
    	level = l;
    }
    
    Sprite::~Sprite()
    {
    	erase(pos.x, pos.y);
    }
    
    vec Sprite::getPos(void)
    {
    	return pos;
    }
    
    float Sprite::getX(void)
    {
    	return pos.x;
    }
    
    float Sprite::getY(void)
    {
    	return pos.y;
    }
    
    void Sprite::addLives(int num)
    {
    	numLives += num;
    }
    
    int Sprite::getLives(void)
    {
    	return numLives;
    }
    
    bool Sprite::isAlive(void)
    {
    	return (numLives > 0);
    }
    
    bool Sprite::move(float x, float y)
    {
    	int xpos = (int)(pos.x + x);
    	int ypos = (int)(pos.y + y);
    
    	if(isValidLevelMove(xpos, ypos))
    	{
    		//wyamz sprite
    		erase(pos.x, pos.y);
    
    		pos.x += x;
    		pos.y += y;
    
    		facingDir.x = x;
    		facingDir.y = y;
    
    		//stworz sprite
    		draw(pos.x, pos.y);
    		return true;
    	}
    	return false;
    }
    
    void Sprite::draw(float x, float y)
    {
    	drawArea->drawSprite(spriteIndex, (int)x, (int)y);
    }
    
    void Sprite::erase(float x, float y)
    {
    	drawArea->eraseSprite((int)x, (int)y);
    }
    
    bool Sprite::isValidLevelMove(int xpos, int ypos)
    {
    	if (level->level[xpos][ypos] != TILE_WALL)
    		return true;
    
    	return false;
    }
    
    void Sprite::idleUpdate(void)
    {
    
    }
    Do you need some more code ?

  2. #2
    Join Date
    Jun 2003
    Location
    Trier, Germany
    Posts
    1,350
    You're trying to access the level array out of its valid bounds. This is a common error in the VTMs, but you don't have to repeat it.

    If you debug the code by setting a breakpoint to that line, you will notice that whenever it crashes either xpos or ypos or both have invalid values. Therefore, be sure to check the values for sanity manually and act accordingly. Checking for sanity means you use an if-clause to check whether the index value is between 0 and (size of the array - 1). Acting accordingly can mean a lot of things: Return from the function with an error code, throw an exception or call abort() (either directly or indirectly via something like assert()). Just don't try to access the array with an invalid index. Very strange things may happen, of which the crash you experienced is kind of the best-case scenario

  3. #3
    Join Date
    Jan 2011
    Posts
    3
    Okay, i know why it crashes xpos and ypos have invalid values, but ComicSansMS, could you explain checking for sanity ? Give some examples ? I would appreciate that

  4. #4
    Join Date
    Oct 2006
    Location
    Melbourne
    Posts
    743
    Quote Originally Posted by Spaj View Post
    Okay, i know why it crashes xpos and ypos have invalid values, but ComicSansMS, could you explain checking for sanity ? Give some examples ? I would appreciate that
    Well, apart from the obvious example with this code - another situation may be found when getting bitmap dimensions and using them to calculate the number of pixels in an image.
    Bitmaps may be stored in either top-down or bottom-up formats. The two are differentiated by the sign of the image's height.

    If I simply calculated numPixelsInImage = imgWidth * imgHeight; I would get the right result when the image was stored in one of the two formats (I forget which) & an impossible figure for the other.

    numPixelsInImage = imgWidth * abs(imgHeight)

    Is a way of enforcing sanity on the values without checking or caring what the original value is.

  5. #5
    Join Date
    Jan 2011
    Posts
    3
    Okay i fixed this. Thanks for help guys

  6. #6
    Join Date
    Apr 2012
    Posts
    2
    Hello there,
    i have also a problem with the tutorial. Using Visual C++ I did the whole Tutorial. Everything worked fine except for the last thing. A problem occured when i try to debug the code. It says:
    Debug Assertion failed
    Program:...visual studio
    2010\Projects\EvilMonkeys\Debug\EvilMonkeys.exe
    File:f\dd\vctools\crt_bld\self_x86\crt\src\dbgdel. cpp
    Line:52

    Expression:_BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

    For information on how your programs cause an assertion failure, see the Visual C++ documenation on asserts.


    I think it has something to do with the "void Level::update(void)" function. It says


    // here is where we will be able to update fireballs and enemies
    for (Iter = npc.begin(); Iter != npc.end(); Iter++)
    {
    // THIS CAUSES SOME MEMORY PROBLEMS DONT KNOW WHY
    delete (*Iter);
    /*(*Iter)->idleUpdate();

    if((*Iter)->isAlive() == false)
    {
    Sprite *temp = *Iter;

    Iter--;
    delete temp;
    npc.remove(temp);
    }*/

    }

    any ideas what is going wrong? And sry for my english, iam german
    Last edited by Blend; 06-29-2012 at 04:24 PM.

  7. #7
    Join Date
    Jul 2006
    Location
    San Diego, CA, USA
    Posts
    897
    Your iterator is pointing to a bad location when you just delete it like that. One way to fix it is to use erase and that method will return the new location of the element after the one that was just removed. Check out this thread for that and other fixes.

    Code:
    for (Iter = npc.begin(); Iter != npc.end(); Iter++)
    {
       // Delete the object
       Sprite *temp = *Iter;
       delete temp;
    
       // Now remove the vector element
       Iter = npc.erase(Iter);
    
    }
    http://www.3dbuzz.com/vbforum/showth...129#post994129
    Last edited by smjones; 06-29-2012 at 06:50 PM.
    Code Fearlessly!

  8. #8
    Join Date
    Apr 2012
    Posts
    2
    It's me again
    @smjones thx for hep. As i have tried your solution, the problem remains the same. Debug Assertion Failure but refering to another file that causes problem.
    I am just new to c++ and have to learn more about pointer, so i cant really tell anything more about the problem. Hopefully it takes only some time that i can figure it out. Thanks for help

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •