2015-11-22 48 views
-1

我想在某个精灵移动屏幕时从其他类修改矩阵“matriz”。我得到的错误是“Juego不是类或名称空间”和“isVacio,isRompible等标识符未找到”。我试图在一个名为“Utilidades”的单独类中进行此操作,但在尝试调用这些方法时仍然遇到同样的错误。我不知道该怎么办。使用静态方法时出错

这是有错误(类 “邦巴”)的代码:

if (Juego::isVacio(x - 35, y)){ /*Sprite explosion*/} 
if (Juego::isVacio(x + 35, y)){/*Sprite explosion*/ } 
if (Juego::isVacio(x, y + 35)){/*Sprite explosion*/ } 
if (Juego::isVacio(x, y - 35)){/*Sprite explosion*/ } 
if (x - 35 < 35 || x + 35 > gr->VisibleClipBounds.Width - 35 || Juego::isBloque(x + 35, y) || Juego::isBloque(x - 35, y)){} 
if (y - 35 < 35 || y + 35 > gr->VisibleClipBounds.Height - 35 || Juego::isBloque(x, y + 35) || Juego::isBloque(x, y - 35)){} 
if (Juego::Juego::isRompible(x - 35, y)){ Juego::romperBloque(x - 35, y, gr); } 
if (Juego::isRompible(x + 35, y)){ Juego::romperBloque(x + 35, y, gr); } 
if (Juego::isRompible(x, y + 35)){ Juego::romperBloque(x, y + 35, gr); } 
if (Juego::isRompible(x, y - 35)){ Juego::romperBloque(x, y - 35, gr); } 
//2bloques 
if (Juego::isVacio(x - 70, y)){/*Sprite explosion*/ } 
if (Juego::isVacio(x + 70, y)){/*Sprite explosion*/ } 
if (Juego::isVacio(x, y + 70)){/*Sprite explosion*/ } 
if (Juego::isVacio(x, y - 70)){/*Sprite explosion*/ } 
if (x - 70 < 35 || x + 70 > gr->VisibleClipBounds.Width - 35 || Juego::isBloque(x + 70, y) || Juego::isBloque(x - 70, y)){} 
if (y - 70 < 35 || y + 70 > gr->VisibleClipBounds.Height - 35 || Juego::isBloque(x, y + 70) || Juego::isBloque(x, y - 70)){} 
if (Juego::isRompible(x - 70, y)){ Juego::romperBloque(x - 70, y, gr); } 
if (Juego::isRompible(x + 70, y)){ Juego::romperBloque(x + 70, y, gr); } 
if (Juego::isRompible(x, y + 70)){ Juego::romperBloque(x, y + 70, gr); } 
if (Juego::isRompible(x, y - 70)){ Juego::romperBloque(x, y - 70, gr); } 
//3bloques 
if (Juego::isVacio(x - 105, y)){} 
if (Juego::isVacio(x +105, y)){} 
if (Juego::isVacio(x, y + 105)){} 
if (Juego::isVacio(x, y - 105)){} 
if (x - 105 < 35 || x + 105 > gr->VisibleClipBounds.Width - 35 || Juego::isBloque(x + 105, y) || Juego::isBloque(x-105, y)){} 
if (y - 105 < 35 || y + 105 > gr->VisibleClipBounds.Height - 35 || Juego::isBloque(x, y + 105) || Juego::isBloque(x, y - 105)){} 
if (Juego::isRompible(x - 105, y)){ Juego::romperBloque(x - 105, y, gr); } 
if (Juego::isRompible(x + 105, y)){ Juego::romperBloque(x + 105, y, gr); } 
if (Juego::isRompible(x, y + 105)){ Juego::romperBloque(x, y + 105, gr); } 
if (Juego::isRompible(x, y - 105)){ Juego::romperBloque(x, y - 105, gr); } 

这是类JUEGO:

#pragma once 

#include <cstdio> 
#include <cstdlib> 
#include <iostream> 
#include <vector> 
#include <fstream> 
#include "Jugador.h" 
#include "Enemigo.h" 
#include "Bomba.h" 


using namespace std; 


class Juego 
{ 
    int nivel; 
    EntidadViva* objJugador; 
    //Enemigo* objEnemigo; 
public: 
    static bool isBloque(int px, int py); 
    static bool isRompible(int px, int py); 
    static bool isBomba(int px, int py); 
    static bool isVacio(int px, int py); 
    static void romperBloque(int px, int py, Graphics^ gr); 
    static int ** matriz; 
    static std::vector<Bomba*> bombas; 
    ~Juego(void); 
    Juego(void); 
    void setDireccion_Jugador(Direccion dir); 
    void Crear_Enemigo(int px, int py); 
    void Crear_Jugador(int px, int py); 
    void Mover_Entidades(Graphics^ gr); 
    void Perseguir(); 
    Jugador* getJugador(); 
    int getNivel(); 
    void setNivel(int n); 
    void cargarMatriz(); 
    void addBomba(Bomba* b); 
    Point getPrimeraPosicionJugador(); 
}; 
/*---------------------Utilidades---------------*/ 
     //Check de bloque// 
bool Juego::isBloque(int px, int py) 
{ 
    if (matriz[px][py] == 0 || matriz[px][py] == 1) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
    // Check de rompible// 
bool Juego::isRompible(int px, int py) 
{ 
    if (matriz[px][py] == 0) 
    { 
     return true; 
    } 
    else 
    { 
     return false; 
    } 
} 
    // Check de bomba// 
bool Juego::isBomba(int px, int py) 
{ 
    for each(Bomba* bmb in bombas) 
    { 
     if (bmb->getX() == px && bmb->getY() == py) 
     { 
      return true; 
     } 
    } 
    return false; 
} 

     //Rompe un bloque rompible// 
void Juego::romperBloque(int px, int py, Graphics^ gr) 
{ 
    matriz[px][py] = 3; 
    //Animacion de explosion del bloque 

} 
    // Check de Vacio// 
bool Juego::isVacio(int px, int py) 
{ 
    if (matriz[px][py] == 3){ return true; } 
    else{ return false; } 
} 

void Juego::addBomba(Bomba* b) 
{ 
    bombas.push_back(b); 
} 
void Juego::cargarMatriz() 
{ 
    ifstream fin; 

    fin.open("Level1.txt"); 


    matriz = new int *[23]; 

    for (int i = 0; i < 23; i++) 
    { 
     matriz[i] = new int[23]; 
    } 

    for (int i = 0; i < 23; i++) 
    { 
     for (int j = 0; j < 23; j++) 
     { 
      fin >> matriz[i][j]; 
     } 
    } 
} 
void Juego::setNivel(int n){ nivel = n; } 
int Juego::getNivel(){ return nivel; } 
Juego::~Juego(void) 
{ 
    delete objJugador; 
    //delete objEnemigo; 
} 
Jugador* Juego::getJugador(){ return (Jugador*)objJugador; } 
Juego::Juego(void) 
{ 
    nivel = 1; 
    cargarMatriz(); 
    //Crear_Enemigo(10, 10); 
    Point p = this->getPrimeraPosicionJugador(); 
    Crear_Jugador(50, 50); 
} 
void Juego::setDireccion_Jugador(Direccion dir) 
{ 
    objJugador->setMovimiento(dir); 
    switch (dir) 
    { 
    case ABAJO: 
     objJugador->setDx(0); objJugador->setDy(5); 
     break; 
    case ARRIBA: 
     objJugador->setDx(0); objJugador->setDy(-5); 
     break; 
    case DERECHA: 
     objJugador->setDx(5); objJugador->setDy(0); 
     break; 
    case IZQUIERDA: 
     objJugador->setDx(-5); objJugador->setDy(0); 
     break; 
    } 
} 
Point Juego::getPrimeraPosicionJugador() 
{ 
    for (int x = 0; x < 23; x++) 
    { 
     for (int y = 0; y < 23; y++) 
     { 
      if (matriz[x][y == 5]) 
      { 
       return Point(x, y); 
      } 
     } 
    } 
} 
void Juego::Crear_Jugador(int px, int py) 
{ 
    objJugador = new Jugador(px, py); 
} 
void Juego::Crear_Enemigo(int px, int py) 
{ 
    //objEnemigo = new Enemigo(px, py); 
} 
void Juego::Mover_Entidades(Graphics^ gr) 
{ 
    //((Enemigo*)objEnemigo)->Mover(gr); 
    ((Jugador*)objJugador)->Mover(gr); 
} 
+0

'matriz [x] [y == 5]'似乎不正确... – emlai

+0

这只是一个错字。错误是我无法从另一个类访问这些方法,即使我包含它并且方法是静态的和公共的。 – MartinStone

回答

0

也许我失去了一些东西,但似乎真是小巫见大巫: 您需要拆分Juego代码:Juego.hpp文件中的类声明,Juego.cpp文件中的成员实现(包括前一个)。

然后在需要“知道”Juego的文件中#include您的Juego.hpp。

你为Juego类发布的代码似乎是在一个文件中(声明&实现),所以它似乎不大可能将#包含在另一个文件中(并且不能包含它在多个文件中,它会给一个链接器错误)

+0

据我所知,在头文件中定义类之外的方法/函数是定义CPP文件中头文件声明的相同驴。 – MartinStone

+0

是的,但你想让你的Bomba类知道Juego类,以便编写你写的代码。 所以你的Bomba,cpp(或.h)必须包含包含Juego类声明的文件,但不包括类实现。 这是(使用cpp&头文件的原因之一),而不是cpp的 –