在忍者来袭的小游戏中,有一个非常大的缺憾,就是主角一直是静止不动,只能呆呆的发飞镖。这对于玩家的交互体验是非常差的。
那么,这节我们就让主角的忍者动起来,让他可以灵活移动,躲避敌人的进攻。
涉及到的知识点是键盘的监听事件。
cocos2dx提供了监听键盘的两种事件,键盘按下和键盘抬起。
具体的代码如下,也比较简单:
auto listener = EventListenerKeyboard::create();
listener->onKeyPressed = CC_CALLBACK_2(KeyboardTest::onKeyPressed, this);
listener->onKeyReleased = CC_CALLBACK_2(KeyboardTest::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
void KeyboardTest::onKeyPressed(EventKeyboard::KeyCode keyCode, Event* event)
{
log(