How do I access a function from another function?

Hi Im making a text based game in C++ console. I have a class for player, a class for monster a level class. I can add the player to the level and I can add the monster to the level but when the player takes the XY position in the level array I want the monster to disappear but I dont know how to do this. Here is my code

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145`` ``````#include "stdafx.h" #include #include using namespace std; const short player = 1; // Player is represented by the number 1 const short monster = 2; // Enemy is represented by the number 2 short randomNum(){ return rand()%10; } class Player{ private: short posX,posY; public: void randomPos(){ //Put the player in a random place in the level. posX = randomNum(); posY = randomNum(); } void message(){ cout << "Cannot move in this direction " << endl; cout << endl; } void movePlayer(char choice){ switch (choice){ case 'w': posX--; if (posX < 0){ posX = 0; message(); } break; case 's': posX++; if (posX > 9) posX = 9; message(); break; case 'a': posY--; if (posY < 0) posY = 0; message(); break; case 'd': posY++; if (posY > 9) posY = 9; message(); break; } } short getX(){return posX;} short getY(){return posY;} }; class Monster: public Player{ private: short mPosX,mPosY; public: void mRandomPos(){ //Put the monster in a random place in the level. mPosX = randomNum(); mPosY = randomNum(); } void moveMonster(){ } short getMX(){return mPosX;} short getMY(){return mPosY;} }; class Level{ private: short myLevel[10][10]; public: void setArray(){ //set the values in the array to 0. for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) myLevel[i][j] = 0; } void playerPos(int X,int Y, bool newPos){ if (!newPos){ myLevel[X][Y] = 0; } if (newPos){ if (myLevel[X][Y] == 2) { cout << "You Killed Monster" << endl; //At this point I want the XY coordinates of the monster object to 0. or stop calling the function that is adding the monster in the gameLoop. cout << endl; } myLevel[X][Y] = player; } } void monsterPos(int X, int Y){ myLevel [X][Y] = monster; } void print(){ for (int i = 0; i < 10; i++) for (int j = 0; j < 10; j++) cout << myLevel [i][j] << "\t"; } }; void gameLoop(Player p, Monster m, Level l){ bool loop = false; char choice; while (!loop) { cout << "Enter Direction "; cin >> choice; l.playerPos(p.getX(),p.getY(),false); p.movePlayer(choice); l.playerPos(p.getX(),p.getY(),true); l.monsterPos(m.getMX(),m.getMY()); //This keeps on looping and adding the monster to the level. I need to set this value to 0 when the player is in the array containing the monster. l.print(); } } int _tmain(int argc, _TCHAR* argv[]) { srand(time(NULL)); Player player; Monster monster; player.randomPos(); monster.mRandomPos(); Level level; level.setArray(); gameLoop(player, monster, level); system ("PAUSE"); return 0; }``````

Last edited on
Anyone?
closed account (D80DSL3A)
I'm having trouble understanding the problem.
Best I can get is from lines 121-123:
 ``123`` ``````l.monsterPos(m.getMX(),m.getMY()); //This keeps on looping and adding the monster to the level. I need to set this value to 0 when the player is in the array containing the monster.``````

Is "this value" = monsterPos(m.getMX(),m.getMY()) ?
As in ` monsterPos(m.getMX(),m.getMY()) = 0;` when the player
is in the array containing the monster?

What is the test for determining if "the player
is in the array containing the monster"?

"Honey, I'm gonna need my chest waders for this one!"
Last edited on
Yes that is what I want to do. At line 89,

 ``12345678`` ``````if (myLevel[X][Y] == 2) { cout << "You Killed Monster" << endl; //At this point I want the XY coordinates of the monster object to 0. or stop calling the function that is adding the monster in the gameLoop. cout << endl; } myLevel[X][Y] = player;``````

If the array contains the number 2 which represents the monster then set its value to 0 but I can set it to 0 because in the gameLoop function keeps on getting the position of the monster and setting this to 2 again. I need to set the position of the monster in the monster class to 0. I tried making a constructor but it gives me this error

Member function may not be declared outside of class.