[RESOLVED] Beginner Identifier Help
Hi all,
I'm currently working on a project and i have found myself stuck. I am aware that I need to make an identifier to fix those errors but I can seem to make it in the right place (or make it right at all.) I'm sorry if you find my code confusing as i'm still a beginner. I hope you are able to guide me in the right direction. My moveUp, moveDown etc, getMonsterX(and y) and my x and y are all errors. I have tried to correct these by identifying them in the .h files but i must have done it wrong.
Sorry if my post is in the wrong place etc, i have just joined this site (:
my main GameWorld.cpp file
Code:
#include <iostream>
#include "GameWorld.h"
#include <windows.h>
#include "Vector2.h"
#include "Monster.h"
using namespace std;
int option; //allowing the user to exit the menu and begin the game
//-----GAME MENU-------
void GameWorld::menu(){
Vector2 vec; //calling the Vector2 func
vec.SetX(1); // Setting the X Values of the player
vec.SetY(1); // Setting the Y Values of the player
system("cls");
cout << "Computer Games Development AS1" << endl; //Title
cout << "" << endl;
cout << "Your Starting X,Y Coordinates Are:" << endl; //Starting Coordinates Displayed To Player
cout << "" << endl;
cout << "X = " << vec.GetX() << endl; //vector get function to display the X,Y coordanites
cout << "" << endl;
cout << "Y = " << vec.GetY() << endl;
cout << "" << endl;
cout << "Press 1 and Enter to Start The Game" << endl; //Option funtion to exit the menu and begin the game
cout << "" << endl;
cin >> option;
system("PAUSE");
}
void GameWorld::Move(){
///----------------------------ADD IF OPTION -------------------------
cout << "Your current X and Y Coordinates are:" << endl;
cout << "" << endl;
cout << "" << endl;
char map[10][11] = {
"##########",
"# #",
"#--- #",
"# #",
"# ---#",
"# #",
"#--- #",
"# #",
"# #",
"##########"
};
}
void player(Player*player){
char direction; //Stores user input
cin >> direction; //Get's user input
switch (direction) {
case 'a': moveLeft(player);
case 'A':
break;
case 'd': moveRight(player);
case 'D':
break;
case 'w': moveUp(player);
case 'W':
break;
case 's': moveDown(player);
case 'S':
break;
}
}
//----MONSTER-------
void Monster::monster(){
if (x == 0) { x++; } //Monster will spawned within x - 0 to 11
if (x == 11){ x--; }
int getMonsterX(){ return x; }
if (x == 0) { y++; } //Monster will spawned within y - 0 to 11
if (x == 11){ y--; }
int getMonsterX(){ return y; }
//Monster(int playX, int playY){ x = playX; y = playY; } --------FIX
};
//----CLEAN UP-------
void GameWorld::cleanup(){
std::cout << "Game Is Now Closing" << std::endl; //cout statement for game closing
}
My Monster.h file
Code:
#include "Vector2.h"
class Monster{
private:
int x, y; //Monster Location Storage
public:
///----MONSTER X COORDINATES----
void monster();
void setX(int newX){ //set monster X coordinate
x = newX;
if (x == 0) { x++; } //set monsters X coordanite between 0-11
if (x == 11){ x--; }
}
int getMonsterX(){ return x; }
///----MONSTER Y COORDINATES----
void setY(int newY){ //set monster Y coordinate
y = newY;
if (y == 0) { y++; }//set monsters Y coordanite between 0-11
if (y == 11){ y--; }
}
int getMonsterY(){ return y; }
Monster(int startX, int startY){ x = startX; y = startY; }
};
class Player{
private:
int x, y; //Monster Location Storage
public:
void player();
void setX(int newX){ x = newX; } // setting Players X coordinate
int getPlayerX(){ return x; }
void setY(int newy){ y = newy; }// setting Players Y coordinate
int getPlayerY(){ return y; }
//Player Functions that allow them to move
void moveLeft(Player *player)
{
player->setY(player->getPlayerY() - 1);
}
void moveRight(Player *player)
{
player->setY(player->getPlayerY() + 1);
}
void moveUp(Player *player)
{
player->setX(player->getPlayerX() - 1);
}
void moveDown(Player *player)
{
player->setX(player->getPlayerX() + 1);
}
};
and finally my Gameworld.h file
Code:
#pragma once
class GameWorld{
private:
public:
GameWorld(int x, int y);
~GameWorld();
bool active = true;
int x = 1;
int y = 1;
void Move();
void init();
void menu();
void gameLoop();
void cleanup();
};
Thanks for your time.
Re: Beginner Identifier Help
Code:
int option; //allowing the user to exit the menu and begin the game
This is a bad idea, as you are defining option at global scope. Shouldn't this be a private variable of the class?
moveLeft() etc are functions of class player - so need to be called from an instance of Player.
Code:
void player(Player*player){
It's not recommended to have a function name the same as a variable name.
Consider
Code:
case 'a': player->moveLeft(player);
Note
Code:
void moveLeft(Player *player)
{
player->setY(player->getPlayerY() - 1);
}
This is not really good OOP design. moveLeft() is a function of class Player, so why are you passing a pointer to a parameter of type *Player?
Don't you just need
Code:
void moveLeft()
{
if (y > 0)
--y;
}
and the same for the other functions? This would then have
Code:
case 'a': player->moveLeft();
and the same for the others.
Re: Beginner Identifier Help
Sorry for the late reply.
Thanks for your help, you cleared a few things up for me.