尝试获取用户输入以在 Cocos2D-X 中移动精灵
Trying to get user input to move sprite in Cocos2D-X
我正在尝试让 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);
有四个错误,它们都指向不同的 playerX/Y +=/-= 5.0f
尝试使用mutable
说明符(允许lambdas主体修改copy捕获的对象,并调用它们的非常量成员函数):
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);
我正在尝试让 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);
有四个错误,它们都指向不同的 playerX/Y +=/-= 5.0f
尝试使用mutable
说明符(允许lambdas主体修改copy捕获的对象,并调用它们的非常量成员函数):
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);