What I need is that when the character collides with the door jump to the next level, how do I do this?

0

This is one of the levels, it is declared as global.

String Capitulo_1[] = {
"OTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTO",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"P                                 p",
"OBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBO",
};

So I draw the level:

void Capitulo_1(Sprite &tile, RenderWindow &window)
{
 for(unsigned int i=0; i<Alto_map; i++)
    for(unsigned int j=0; j<Ancho_map; j++){
if(TileMap[i][j]=='B') tile.setTextureRect(IntRect(  0, 0, 32, 32));//piso
if(TileMap[i][j]=='T') tile.setTextureRect(IntRect( 32, 0, 32, 32));//techo
if(TileMap[i][j]=='O') tile.setTextureRect(IntRect( 64, 0, 32, 32));//simiento
if(TileMap[i][j]=='s') tile.setTextureRect(IntRect( 96, 0, 32, 32));//punta izq
if(TileMap[i][j]=='d') tile.setTextureRect(IntRect(128, 0, 32, 32));//punta der
if(TileMap[i][j]=='p') tile.setTextureRect(IntRect(224, 0, 32, 32));//pared der
if(TileMap[i][j]=='P') tile.setTextureRect(IntRect(256, 0, 32, 32));//pared izq
if(TileMap[i][j]=='Z') tile.setTextureRect(IntRect(32, 0, 32, 32));//ENERGIA Fal
if(TileMap[i][j]=='z') tile.setTextureRect(IntRect(591, 0, 12, 15));//ENERGIA
if(TileMap[i][j]=='D') tile.setTextureRect(IntRect(507, 207, 71, 78));//PUERTA 
if(TileMap[i][j]==' ' || TileMap[i][j]=='X') continue;

         tile.setPosition(j*32 - offsetX, i*32 - offsetY);
         window.draw(tile);
       }
}

This is the function that loads the levels:

void cargarCapitulo(Image &i, RenderWindow &window, int Numero_del_Capitulo)
{
    int cap = Numero_del_Capitulo;
    Sprite tile;
    Texture txt;
    txt.loadFromImage(i);
    tile.setTexture(txt);

    switch (cap)
     {
        case 1: Capitulo_1(tile, window); break;
        case 2: Capitulo_Boss(tile, window); break;

        case 3: Capitulo_2(tile, window); break;
        case 4: Capitulo_Boss(tile, window); break;

        case 5: Capitulo_3(tile, window); break;
        case 6: Capitulo_Boss(tile, window); break;

        case 7: Capitulo_4(tile, window); break;
        case 8: Capitulo_Boss(tile, window); break;

        case 9: Capitulo_5(tile, window); break;
        case 10: Capitulo_Boss(tile, window); break;

        case 11: Capitulo_6(tile, window); break;
        case 12: Capitulo_Boss(tile, window); break;

        //default : fin_del_juego(); break;
     }
}

This is what I call it in main :

int main(){
    cargarCapitulo(t, w, l);
}

This is how I locate the character:

personaje.rect.left o personaje.spt.getPosition().y

Class of Personaje :

PERSONAJE::PERSONAJE(Image &i, int lvl)
{
    t.loadFromImage(i);
    spt.setTexture(t);

    cap = lvl;
    Cargar_Personaje();

    vida = true; poder = 3; dispara = 0; dx = dy = 0.1; cframe = 0;
}

void PERSONAJE::Cargar_Personaje()
{
    switch (cap)
     {
        case 1:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 2:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 3:  rect = FloatRect(1*32, 10*32, 25, 32); break;
        case 4:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 5:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 6:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 7:  rect = FloatRect(1*32,  9*32, 25, 32); break;
        case 8:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 9:  rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 10: rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 11: rect = FloatRect(1*32, 11*32, 25, 32); break;
        case 12: rect = FloatRect(1*32, 11*32, 25, 32); break;
     }
}

void PERSONAJE::control()
{
    if(Keyboard::isKeyPressed(Keyboard::Left)) {stado = left; dx = -0.1; }
    if(Keyboard::isKeyPressed(Keyboard::Right)) {stado = right; dx = 0.1; }
    if(Keyboard::isKeyPressed(Keyboard::Z)) if(sTierra) {dy = -0.36; sTierra = false;}
    if (Keyboard::isKeyPressed(Keyboard::X)) { dispara = true; }
}

void PERSONAJE::actualizar(float tiempo)
{
     if(vida) control();
    //vida = p;
    rect.left += dx * tiempo;
    Colision(0);

    if(!sTierra) dy = dy + 0.0005 * tiempo;
    rect.top += dy*tiempo;
    sTierra = false;
    Colision(1);

    cframe += 0.005*tiempo;
    if(cframe > 3) cframe -=3;

    if(dx>0) spt.setTextureRect(IntRect(0+32*int(cframe), 232, 32, 32));
    if(dx<0) spt.setTextureRect(IntRect(5+32*int(cframe), 200, 32, 32));

    if (!vida) spt.setTextureRect(IntRect(0, 0, 1, 1));
    spt.setPosition(rect.left - offsetX, rect.top - offsetY);

    dx = 0;
}

void PERSONAJE::Colision(int dir)
{
   if(cap == 1)
   {
    for (int i = rect.top/32 ; i<(rect.top+rect.height)/32; i++)
        for (int j = rect.left/32; j<(rect.left+rect.width)/32; j++)
        {
            if(TileMap[i][j]=='B' || TileMap[i][j]=='P' || TileMap[i][j]=='p' || TileMap[i][j]=='O' ||
           TileMap[i][j]=='T' || TileMap[i][j]=='s' || TileMap[i][j]=='d' || TileMap[i][j]=='Z' || TileMap[i][j]=='D')
            {

            if ((dx>0) && (dir==0)) rect.left =  j*32 -  rect.width;
            if ((dx<0) && (dir==0)) rect.left =  j*32 + 32;
            if ((dy>0) && (dir==1))  { rect.top =   i*32 -  rect.height;  dy=0;   sTierra=true; }
            if ((dy<0) && (dir==1))  { rect.top = i*32 + 32;   dy=0;}
            }
        }
    }

Thank you very much already.

    
asked by ZeroMyD 25.05.2017 в 04:11
source

1 answer

0

Seeing the PERSONAJE::Colision method, I understand that you can know if the character collides with the door if TileMap[i][j]=='D' , then, in that same method after checking collision (or creating another method only for doors), you can indicate that if collides with a door change level, something like if(TileMap[i][j]=='D') cargarCapitulo(loQueSea); .

    
answered by 25.05.2017 в 22:23