Learning systems cocos2d-x cocos (2) interacting

Alternately

Play games, we need to interact with the input device and its games, the game requires players accordingly, for example by pressing up and down the keyboard, the role of moves in the direction corresponding to the pressed key skills, roles, is released skill

Keyboard monitor

response

Cocos2d-x function in order to achieve a keyboard monitor, write your first game on the keyboard to make a response

"HelloWorldScene.h"
void Press(EventKeyboard::KeyCode keycode, Event *event);//按下
void Release(EventKeyboard::KeyCode keycode, Event *event);//释放

The two function declarations in HelloWorldScene.h
the next two functions are defined

"HelloWorldScene.cpp"
void HelloWorld::Press(EventKeyboard::KeyCode keycode, Event* event)
{
    using KC = EventKeyboard::KeyCode;
    switch (keycode)
    {
    case KC::KEY_UP_ARROW:
        log("up press");
        break;
    case KC::KEY_DOWN_ARROW:
        log("down press");
        break;
    case KC::KEY_RIGHT_ARROW:
        log("right press");
        break;
    case KC::KEY_LEFT_ARROW:
        log("left press");
        break;
    case KC::KEY_Z:
        log("Explosion!");
        break;
    default:
        break;
    }
}

void HelloWorld::Release(EventKeyboard::KeyCode keycode, Event* event)
{
    using KC = EventKeyboard::KeyCode;
    switch (keycode)
    {
    case KC::KEY_UP_ARROW:
        log("up release");
        break;
    case KC::KEY_DOWN_ARROW:
        log("down release");
        break;
    case KC::KEY_RIGHT_ARROW:
        log("right release");
        break;
    case KC::KEY_LEFT_ARROW:
        log("left release");
        break;
    case KC::KEY_Z:
        log("Boom!");
        break;
    default:
        break;
    }
}

These two functions of the left and right and up and down keys of the keyboard accordingly z

Sign up listening

In HelloWorld::init()addition of the function code

"HelloWorldScene.cpp"
auto *dispatcher = Director::getInstance()->getEventDispatcher();
auto* keyListener = EventListenerKeyboard::create();//创建键盘按键监听器

keyListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::Press, this);//设置按键按下的响应
keyListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::Release, this);//设置按键释放的响应
//键盘按键被弹回时响应
dispatcher->addEventListenerWithSceneGraphPriority(keyListener,this);

Compile, if such a mistake, then

do not panic, is HelloWorldScene.hnot used in cocos2d namespace, in #include "cocos2d.h"this add the following line of code USING_NS_CC;just fine, USING_NS_CC;is a macro equivalentusing namespace cocos2d

"HelloWorldScene.h"
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Scene
{
...
}

Into this, or directly to declare the scope resolution parameter

"HelloWorldScene.h"
#include "cocos2d.h"
//USING_NS_CC;
class HelloWorld : public cocos2d::Scene
{
public:
    ...
    void Press(cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event* event);//按下
    void Release(cocos2d::EventKeyboard::KeyCode keycode, cocos2d::Event* event);//释放
    ...
};

Program successfully up and running, press the button to view the output window of

successful interaction

Mouse listener

response

Keyboard and monitor of the same steps, we first HelloWorldScene.hfunction declaration two mouse response, of course, if we only need to press a function, then release function you can also do not need to write a bundled relationship between these two functions do not exist

"HelloWorldScene.h"
void MouseDown(cocos2d::Event* event);
void MouseUp(cocos2d::Event* event);

In HelloWorldScene.cppthe definition of these two functions

"HelloWorldScene.cpp"
void HelloWorld::MouseDown(cocos2d::Event* event)
{
    EventMouse* e = static_cast<EventMouse*>(event);
    log("press (%f, %f)", e->getCursorX(), e->getCursorY());
}

void HelloWorld::MouseUp(cocos2d::Event* event)
{
    EventMouse* e = static_cast<EventMouse*>(event);
    log("release (%f, %f)", e->getCursorX(), e->getCursorY());
}

Functions of these two functions is pressed or released position output of the mouse

Sign up listening

In HelloWorld::init()adding this code registration monitor function, keyboard and monitor basic principle the same, but the type of callback is CC_CALLBACK_1not the keyboard CC_CALLBACK_2, type the callback parameter as the difference according to the type and quantity

"HelloWorldScene.cpp"
auto listener = EventListenerMouse::create();
listener->onMouseDown = CC_CALLBACK_1(HelloWorld::MouseDown,this);
listener->onMouseUp = CC_CALLBACK_1(HelloWorld::MouseUp,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

Run the program and click the mouse test function

click on the top right and bottom left nearly two locations can be found in the mouse event coordinates are stored coordinates in the game world, and the range is related to the resolution of the game.

Guess you like

Origin www.cnblogs.com/NightFrost/p/11704179.html