Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » More Trouble - Loop Not Iterating Properly

This thread is locked; no one can reply to it. rss feed Print
More Trouble - Loop Not Iterating Properly
agonvs
Member #15,917
March 2015

Hi all,

My program is stuck and I've managed to figure out where the hitch is. The collide function, which is supposed to iterate through a list of tiles and return the type, is apparently not iterating properly as it prints out the same set of coordinates over and over. Could somebody look this over and see if anything jumps out?

Here's player.h:

#SelectExpand
1//#include "entity.h" 2#include <allegro5\allegro.h> 3#include <list> 4#include "globals.h" 5#include "tile.h" 6 7enum facing {RIGHT, LEFT}; 8 9class Player 10{ 11private: 12 std::list<Tile*> mTiles; 13 std::list<Tile*>::iterator mIter; 14 int mX, mY, mVelX, mVelY, bottomFrame, topFrame, fallIndex, idleCount, idleIndex; 15 bool tint, flip, justFell; 16 int mJumpInts[30]; 17 int mFallInts[30]; 18 facing mFacing; 19 direction mDir; 20 ALLEGRO_BITMAP* mImage; 21 ALLEGRO_COLOR color; 22public: 23 int getVelX(){return mVelX;} 24 int getVelY(){return mVelY;} 25 int collide(int footX, int footY); 26 void west(); 27 void none(); 28 void east(); 29 void idle(); 30 void walk(); 31 void jump(); 32 void fall(); 33 void shoot(); 34 void Initialize(int x, int y, ALLEGRO_BITMAP* image, std::list<Tile*> Tiles, std::list<Tile*>::iterator iter); 35 void Update(direction dir, ALLEGRO_JOYSTICK_STATE jst); 36 void Render(); 37 bool collide(int x, int y, int width, int height); 38};

And here's player.cpp:

#SelectExpand
1#include<iostream> 2#include<list> 3#include"player.h" 4#include"globals.h" 5#include"tile.h" 6using namespace std; 7 8void Player::Initialize(int x, int y, ALLEGRO_BITMAP* image, std::list<Tile*> Tiles, std::list<Tile*>::iterator iter) 9{ 10 mX = x; 11 mY = y; 12 mVelX = 0; 13 mVelY = 0; 14 bottomFrame = 1; 15 topFrame = 0; 16 tint = false; 17 flip = false; 18 mFacing = RIGHT; 19 mDir = EAST; 20 mImage = image; 21 mTiles = Tiles; 22 mIter = iter; 23 idleCount = 0; 24 idleIndex = 1; 25 mJumpInts[0] = -16; 26 mJumpInts[1] = -16; 27 mJumpInts[2] = -16; 28 mJumpInts[3] = -16; 29 mJumpInts[4] = -8; 30 mJumpInts[5] = -8; 31 mJumpInts[6] = -8; 32 mJumpInts[7] = -8; 33 mJumpInts[8] = -4; 34 mJumpInts[9] = -4; 35 mJumpInts[10] = -4; 36 mJumpInts[11] = -4; 37 mJumpInts[12] = -2; 38 mJumpInts[13] = -2; 39 mJumpInts[14] = -2; 40 mJumpInts[15] = -2; 41 mJumpInts[16] = -1; 42 mJumpInts[17] = -1; 43 mJumpInts[18] = -1; 44 mJumpInts[19] = -1; 45 mJumpInts[20] = -1; 46 mJumpInts[21] = -1; 47 mJumpInts[22] = -1; 48 mJumpInts[23] = -1; 49 mJumpInts[24] = 0; 50 mJumpInts[25] = 0; 51 mJumpInts[26] = 0; 52 mJumpInts[27] = 0; 53 mJumpInts[28] = 0; 54 mJumpInts[29] = 0; 55 mFallInts[0] = 0; 56 mFallInts[1] = 0; 57 mFallInts[2] = 0; 58 mFallInts[3] = 0; 59 mFallInts[4] = 0; 60 mFallInts[5] = 0; 61 mFallInts[6] = 1; 62 mFallInts[7] = 1; 63 mFallInts[8] = 1; 64 mFallInts[9] = 1; 65 mFallInts[10] = 1; 66 mFallInts[11] = 1; 67 mFallInts[12] = 1; 68 mFallInts[13] = 1; 69 mFallInts[14] = 2; 70 mFallInts[15] = 2; 71 mFallInts[16] = 2; 72 mFallInts[17] = 2; 73 mFallInts[18] = 4; 74 mFallInts[19] = 4; 75 mFallInts[20] = 4; 76 mFallInts[21] = 4; 77 mFallInts[22] = 8; 78 mFallInts[23] = 8; 79 mFallInts[24] = 8; 80 mFallInts[25] = 8; 81 mFallInts[26] = 16; 82 mFallInts[27] = 16; 83 mFallInts[28] = 16; 84 mFallInts[29] = 16; 85} 86 87void Player::Update(direction dir, ALLEGRO_JOYSTICK_STATE jst) 88{ 89 if (dir == EAST) 90 east(); 91 if (dir == NONE) 92 none(); 93 if (dir == WEST) 94 west(); 95 if (justFell && !(jst.button[0])) 96 justFell = false; 97 if (!(justFell) && jst.button[0]) 98 jump(); 99} 100 101void Player::Render() 102{ 103 if (mFacing == RIGHT) 104 { 105 al_draw_bitmap_region(mImage, (bottomFrame*128), 0, 128, 128, mX, mY, 0); 106 al_draw_bitmap_region(mImage, (topFrame*128), 0, 128, 128, mX, mY, 0); 107 } 108 else 109 { 110 al_draw_bitmap_region(mImage, (bottomFrame*128), 0, 128, 128, mX, mY, ALLEGRO_FLIP_HORIZONTAL); 111 al_draw_bitmap_region(mImage, (topFrame*128), 0, 128, 128, mX, mY, ALLEGRO_FLIP_HORIZONTAL); 112 } 113} 114 115void Player::east() 116{ 117 mFacing = RIGHT; 118 int tile = collide(102, 122); 119 if (tile == 1) 120 { 121 walk(); 122 fallIndex = 0; 123 } 124 else if (tile == 2) 125 mVelX = 0; 126 else 127 fall(); 128} 129 130void Player::west() 131{ 132 mFacing = LEFT; 133 int tile = collide(32, 122); 134 if (tile == 1) 135 { 136 walk(); 137 fallIndex = 0; 138 } 139 else if (tile == 2) 140 mVelX = 0; 141 else 142 fall(); 143} 144 145void Player::none() 146{ 147 //cout << "In function none()" << endl; 148 mVelX = 0; 149 if (mFacing == RIGHT) 150 { 151 int tile = collide(102,122); 152 //cout << "Tile: " << tile << endl; 153 if (tile == 1) 154 idle(); 155 else 156 fall(); 157 } 158 else 159 { 160 int tile = collide(32,122); 161 //cout << "Tile: " << tile << endl; 162 if (tile == 1) 163 idle(); 164 else 165 fall(); 166 } 167} 168 169 170void Player::idle() 171{ 172 if (idleCount == 5) 173 { 174 idleCount = 0; 175 if (idleIndex == 4) 176 idleIndex = 1; 177 else 178 idleIndex++; 179 } 180 idleCount++; 181 bottomFrame = 0; 182 topFrame = idleIndex; 183 cout << "In function idle()" << endl; 184} 185 186int Player::collide(int footX, int footY) 187{ 188 //cout << "mX: " << mX << " mY: " << mY << " footX: " << footX << " footY: " << footY << endl; 189 for (mIter = mTiles.begin(); mIter != mTiles.end(); ++mIter) 190 { 191 if ((*mIter)->isDrawable()) 192 { 193 cout << (*mIter)->getX() << " " << (*mIter)->getY() << endl; 194 if ((mX + footX >= (*mIter)->getX()) && (mY + footY >= (*mIter)->getY()) && (mX + footX < (*mIter)->getX() + 128) && (mY + footY < (*mIter)->getY()+128)) 195 { 196 if ((*mIter)->getType() == 1) 197 { 198 if (mY + footY > (*mIter)->getY() + 64) 199 { 200 mY = 58; 201 mVelY = 0; 202 justFell = true; 203 (*mIter)->setTint(true); 204 return 1; 205 } 206 (*mIter)->setTint(true); 207 return 0; 208 } 209 else if ((*mIter)->getType() == 2) 210 { 211 (*mIter)->setTint(true); 212 return 2; 213 } 214 } 215 else 216 { 217 (*mIter)->setTint(false); 218 return 0; 219 } 220 221 } 222 223 } 224} 225 226void Player::walk() 227{ 228 229} 230 231void Player::jump() 232{ 233 234} 235 236void Player::fall() 237{ 238 239}

I know the list itself initialized properly because the tiles display correctly on the screen. It's just that in this other Player class, things seem to get hung up in the Collide function. And yes, there is more than just one drawable tile, otherwise there would be no screen.

EDIT: I figured out what I was doing wrong. By using return statements I was exiting the loop before it had finished.

Chris Katko
Member #1,881
January 2002
avatar

Quote:

std::list<Tile*> mTiles;
//...
if ((*mIter)->isDrawable())

FYI, you're already using a list, so you don't need to use Tile pointers, you could just use Tiles. There are situations where you need pointers (polymorphism--storing a derived class in a list of base class type), but this probably isn't it.

This works just as fine:

std::list<Tile> mTiles;
//...
 if ((mIter)->isDrawable())

As for the return values you've found, don't forget that you can use "break" and "continue" to control loop flow.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs

Go to: