우선 헤더(.h)에
void onKeyPressed(EventKeyboard::KeyCode keyCode, Event *event);
void onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event);
해주고
cpp 에서 init 함수에
auto keylistener = EventListenerKeyboard::create();
keylistener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed, this);
keylistener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keylistener, this);
해준다. onKeyPressed,onKeyReleased 함수가 필요함으로
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode, Event *event){
auto sprPlayer = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER);
switch (keyCode)
{
case cocos2d::EventKeyboard::KeyCode::KEY_A:{
aKeyCheck = false;
aMoveCheck = false;
auto action = (Action*)sprPlayer->getActionByTag(TAG_SPRITE_PLAYER_ACTION_A);
sprPlayer->getActionManager()->removeAction(action);
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_D:{
dKeyCheck = false;
dMoveCheck = false;
auto action = (Action*)sprPlayer->getActionByTag(TAG_SPRITE_PLAYER_ACTION_D);
sprPlayer->getActionManager()->removeAction(action);
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_S:
{
sKeyCheck = false;
sMoveCheck = false;
auto action = (Action*)sprPlayer->getActionByTag(TAG_SPRITE_PLAYER_ACTION_S);
sprPlayer->getActionManager()->removeAction(action);
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_W:
{
wKeyCheck = false;
wMoveCheck = false;
auto action = (Action*)sprPlayer->getActionByTag(TAG_SPRITE_PLAYER_ACTION_W);
sprPlayer->getActionManager()->removeAction(action);
break;
}
default:
break;
}
}
void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode, Event *event){
auto sprPlayer = (Sprite*)this->getChildByTag(TAG_SPRITE_PLAYER);
switch (keyCode)
{
case cocos2d::EventKeyboard::KeyCode::KEY_A:{
aKeyCheck = true;
aMoveCheck = true;
auto action_0 = MoveBy::create(0.1, Point(-TAG_SPRITE_PLAYER_SPEED, 0));
auto action_1 = RepeatForever::create(action_0);
action_1->setTag(TAG_SPRITE_PLAYER_ACTION_A);
sprPlayer->runAction(action_1);
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_D:{
dKeyCheck = true;
dMoveCheck = true;
auto action_0 = MoveBy::create(0.1, Point(TAG_SPRITE_PLAYER_SPEED, 0));
auto action_1 = RepeatForever::create(action_0);
action_1->setTag(TAG_SPRITE_PLAYER_ACTION_D);
sprPlayer->runAction(action_1);
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_E:{
CCPoint playerPos = sprPlayer->getPosition();
CCPoint tileCoord = this->tileCoordForPosition(playerPos);
int tileGid = _meta->tileGIDAt(tileCoord);
auto properties = _tileMap->propertiesForGID(tileGid);
ValueMap map = properties.asValueMap();
String propertyValue = map["Eat"].asString();
if (propertyValue.compare("True") == 0) {
_meta->removeTileAt(tileCoord);
_tree->removeTileAt(tileCoord);
}
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_S:
{
sKeyCheck = true;
sMoveCheck = true;
auto action_0 = MoveBy::create(0.1, Point(0, -TAG_SPRITE_PLAYER_SPEED));
auto action_1 = RepeatForever::create(action_0);
action_1->setTag(TAG_SPRITE_PLAYER_ACTION_S);
sprPlayer->runAction(action_1);
break;
}
case cocos2d::EventKeyboard::KeyCode::KEY_W:
{
wKeyCheck = true;
wMoveCheck = true;
auto action_0 = MoveBy::create(0.1, Point(0, TAG_SPRITE_PLAYER_SPEED));
auto action_1 = RepeatForever::create(action_0);
action_1->setTag(TAG_SPRITE_PLAYER_ACTION_W);
sprPlayer->runAction(action_1);
break;
}
default:
break;
}
}
이처럼 키보드의 각 키의 경우 마다 실행할 조건들을 넣어주면 된다.
int tileGid = _meta->tileGIDAt(tileCoord); 여기서 _meta는 뭘뜻하는 건가요?
답글삭제