尝试获取用户输入以在 Cocos2D-X 中移动精灵

Posted

技术标签:

【中文标题】尝试获取用户输入以在 Cocos2D-X 中移动精灵【英文标题】:Trying to get user input to move sprite in Cocos2D-X 【发布时间】:2021-11-02 17:52:24 【问题描述】:

我正在尝试让 WASD 的用户输入在 Cocos2D-X 中移动精灵。我很确定我做的一切都是正确的,但它给了我这个错误: expression must be a modifiable lvalue.

这是我的代码(注意:我是 Cocos2D-X 的新手,所以可能有点乱)

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);

有四个错误,它们都指向不同的玩家X/Y +=/-= 5.0f

【问题讨论】:

错误告诉你你正在尝试为你不允许的东西赋值。发生这种情况有多种原因。例如,分配给 const 变量、数组或文字。我假设您使用的是视觉工作室?如果您双击错误,或者查看输出选项卡,它应该会将您指向导致问题的行。 有四个错误,它们都指向不同的播放器X/Y +=/-= 5.0f 哦,看起来您正在按值捕获 playerX 和 playerY,并且不允许在 lambda 函数中更改它们。您需要通过引用来捕获它们。 【参考方案1】:

尝试使用mutable说明符(它允许lambdas body修改被复制捕获的对象,并调用它们的非常量成员函数):

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);

【讨论】:

以上是关于尝试获取用户输入以在 Cocos2D-X 中移动精灵的主要内容,如果未能解决你的问题,请参考以下文章

《Cocos2d-x游戏开发实战精解》学习笔记2--在Cocos2d-x中显示一行文字

《Cocos2d-x游戏开发实战精解》学习笔记1--在Cocos2d中显示图像

如何从不和谐中获取用户输入并将其存储为变量以在方程式中使用?不和谐.py

Cocos2d-x中怎么获取moveTo移动期间的坐标位置

移动端APP测试常见面试题精析1

缩放图像以在android中裁剪