c++cocos2d-x

Trying to get user input to move sprite in Cocos2D-X


I'm trying to make user input of WASD move a sprite around in Cocos2D-X. I'm pretty sure I'm doing everything correct but it gives me this error: expression must be a modifiable lvalue.

Here is my code (Note: I'm new to Cocos2D-X, so it might be a bit messy)

float playerX = visibleSize.width / 2 + origin.x;
float playerY = visibleSize.height / 2 + origin.y;
    
auto player = Sprite::create("sprites/player.png");
if (player == nullptr)
{
    problemLoading("'sprites/player.png'");
}
else
{
    // position the sprite on the center of the screen
    player->setPosition(Vec2(playerX, playerY));

    // add the sprite as a child to this layer
    this->addChild(player, 0);
}

    // Keyboard events
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = [playerX, playerY](EventKeyboard::KeyCode keyCode, Event* event)
{
    switch (keyCode)
    {
    case EventKeyboard::KeyCode::KEY_W:
        playerY += 5.0f;
        break;
    case EventKeyboard::KeyCode::KEY_A:
        playerX -= 5.0f;
        break;
    case EventKeyboard::KeyCode::KEY_S:
        playerY -= 5.0f;
        break;
    case EventKeyboard::KeyCode::KEY_D:
        playerX += 5.0f;
        break;
    case EventKeyboard::KeyCode::KEY_ESCAPE:
        Director::getInstance()->end();
        break;
    }
};
    _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);

There is four of the error and they all point to a different playerX/Y +=/-= 5.0f


Solution

  • Try to use mutable specifier(which allows lambdas body to modify the objects captured by copy, and to call their non-const member functions):

    float playerX = visibleSize.width / 2 + origin.x;
    float playerY = visibleSize.height / 2 + origin.y;
        
    auto player = Sprite::create("sprites/player.png");
    if (player == nullptr)
    {
        problemLoading("'sprites/player.png'");
    }
    else
    {
        // position the sprite on the center of the screen
        player->setPosition(Vec2(playerX, playerY));
    
        // add the sprite as a child to this layer
        this->addChild(player, 0);
    }
    
        // Keyboard events
    auto keyboardListener = EventListenerKeyboard::create();
    
    // add mutable specifier
    keyboardListener->onKeyPressed = [playerX, playerY, player](EventKeyboard::KeyCode keyCode, Event* event) mutable
    {
        switch (keyCode)
        {
        case EventKeyboard::KeyCode::KEY_W:
            playerY += 5.0f;
            break;
        case EventKeyboard::KeyCode::KEY_A:
            playerX -= 5.0f;
            break;
        case EventKeyboard::KeyCode::KEY_S:
            playerY -= 5.0f;
            break;
        case EventKeyboard::KeyCode::KEY_D:
            playerX += 5.0f;
            break;
        case EventKeyboard::KeyCode::KEY_ESCAPE:
            Director::getInstance()->end();
            break;
        }
        
        player->setPosition(playerX, playerY);
    };
        _eventDispatcher->addEventListenerWithSceneGraphPriority(keyboardListener, this);