emscripten icon indicating copy to clipboard operation
emscripten copied to clipboard

Cannot read undefined property "locked" of undefined

Open DirkWillem opened this issue 12 years ago • 3 comments

Hello

I'm developing a simple tic-tac-toe game with emscripten, and I use a 2-dimensional array of integers to store what value a cell of a field has.

Then in Game::Init, I do this:

void Game::Init(){
    SDL_Init(SDL_INIT_EVERYTHING);
    screen = SDL_SetVideoMode(640,400,32,SDL_SWSURFACE);

    Game::ResetGame();

    emscripten_set_main_loop(Game::Loop, 30, 1);
}

and Game::ResetGame():

void Game::ResetGame(){
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            field[i][j] = FIELD_EMPTY;
        }
    }
}

Although this seems like perfect C++ to me, (and the compiler too), I get the following error in the browser console:

exception thrown: TypeError: Cannot read property 'locked' of undefined,TypeError: Cannot read property 'locked' of undefined
    at Object.SDL_gfx.drawRectangle (http://localhost/emscripten/tictactoe/index.js:4480:25)
    at _boxRGBA (http://localhost/emscripten/tictactoe/index.js:4524:22)
    at __ZN8Graphics8FillRectEP11SDL_Surfaceffff5color (http://localhost/emscripten/tictactoe/index.js:5864:10)
    at __ZN4Game9DrawBoardEv (http://localhost/emscripten/tictactoe/index.js:5981:2)
    at Array.__ZN4Game4LoopEv (http://localhost/emscripten/tictactoe/index.js:5960:2)
    at Object.Runtime.dynCall (http://localhost/emscripten/tictactoe/index.js:366:33)
    at Browser_mainLoop_runner (http://localhost/emscripten/tictactoe/index.js:4627:19) 

Uncaught TypeError: Cannot read property 'locked' of undefined 

What am I doing wrong?

EDIT: looking at the errors, something with my board drawing code seems to be wrong; It works perfectly if I don't call the ResetGame function. Here it is:

void Game::DrawBoard(){
    color black = {0,0,0,255};

    Graphics::FillRect(screen,240,128,640,136,black);
    Graphics::FillRect(screen,240,264,640,272,black);
    Graphics::FillRect(screen,368,0,376,400,black);
    Graphics::FillRect(screen,504,0,512,400,black);
}

and Graphics::FillRect:

void Graphics::FillRect(SDL_Surface* surf, float x, float y, float w, float h, color c){
        boxRGBA(surf, x, y, w, h, c.r, c.g, c.b, c.a);
}

DirkWillem avatar Feb 10 '14 17:02 DirkWillem

Seems like a bad SDL_Surface could be being passed to FillRect. How is it created and passed to FillRect?

inolen avatar Feb 10 '14 18:02 inolen

as you can see, I pass screen into FillRect. It is created on the second line of Game::Init:

screen = SDL_SetVideoMode(640,400,32,SDL_SWSURFACE);

DirkWillem avatar Feb 10 '14 18:02 DirkWillem

OK, So i got what the problem was. For some reason the compiler accepts something that is just completely wrong in C++, you have to declare the private member variables in the implementation:

int Game::field[3][3];
SDL_Surface* Game::screen;

this solved everything

DirkWillem avatar Feb 10 '14 19:02 DirkWillem