1. 程式人生 > >cocos2d-x接受鍵盤事件,左右鍵

cocos2d-x接受鍵盤事件,左右鍵

首先在AppDelegate.cpp加入以下程式碼,一定要在AppDelegate::applicationDidFinishLaunching()上,宣告用的。

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
HelloWorld *g_layer;

void Win32SetKeyLayer(HelloWorld *layer)
{
	g_layer = layer;
}

void Win32KeyHook( UINT message,WPARAM wParam, LPARAM lParam )
{
	CCLog("Win32KeyHook message %d wParam %d lParam %d", message, wParam, lParam);
	if (g_layer)
		g_layer->onWin32KeyEvent(message, wParam, lParam);
}
#endif

在AppDelegate::applicationDidFinishLaunching()中
bool AppDelegate::applicationDidFinishLaunching() {



    // initialize director
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
	// 2012.11.07 加入鍵盤處理程式碼
	pEGLView->setAccelerometerKeyHook(Win32KeyHook);///////////////////////////////////
#endif  // CC_PLATFORM_WIN32


    pDirector->setOpenGLView(pEGLView);
	
    // turn on display FPS
    pDirector->setDisplayStats(true);

    // set FPS. the default value is 1.0/60 if you don't call this
    pDirector->setAnimationInterval(1.0 / 60);

    // create a scene. it's an autorelease object
    CCScene *pScene = HelloWorld::scene();

    // run
    pDirector->runWithScene(pScene);

    return true;
}

在HelloWorldScene.cpp中
CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

    // add layer as a child to scene
    scene->addChild(layer);

	extern void Win32SetKeyLayer(HelloWorld *layer);
	Win32SetKeyLayer(layer);

    // return the scene
    return scene;
}

#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
void HelloWorld::onWin32KeyEvent( UINT message,WPARAM wParam, LPARAM lParam )
{
	 CCLog("onWin32KeyEvent message %d wParam %d lParam %d", message, wParam, lParam);
    /*
    // Up
    Win32KeyHook message 256 wParam 38 lParam 21495809
    onWin32KeyEvent message 256 wParam 38 lParam 21495809
    Win32KeyHook message 257 wParam 38 lParam -1052246015
    onWin32KeyEvent message 257 wParam 38 lParam -1052246015
    // Down
    Win32KeyHook message 256 wParam 40 lParam 22020097
    onWin32KeyEvent message 256 wParam 40 lParam 22020097
    Win32KeyHook message 257 wParam 40 lParam -1051721727
    onWin32KeyEvent message 257 wParam 40 lParam -1051721727
    // Left
    Win32KeyHook message 256 wParam 37 lParam 21692417
    onWin32KeyEvent message 256 wParam 37 lParam 21692417
    Win32KeyHook message 257 wParam 37 lParam -1052049407
    onWin32KeyEvent message 257 wParam 37 lParam -1052049407
    // Right
    Win32KeyHook message 256 wParam 39 lParam 21823489
    onWin32KeyEvent message 256 wParam 39 lParam 21823489
    Win32KeyHook message 257 wParam 39 lParam -1051918335
    onWin32KeyEvent message 257 wParam 39 lParam -1051918335
    */
    if (message == 256)
    {
        switch (wParam)
        {
        case 38:
            moveHero(1);
            break;
        case 40:
            moveHero(2);
            break;
        case 37:
            moveHero(3);
            break;
        case 39:
            moveHero(4);
            break;
        }
    }
    else if (message == 257)
    {
    }
}
#endif

void HelloWorld::moveHero( int diraction )
{
	CCLog("moveHero: %d",diraction);
}

//現在你就可以上下左右鍵,看輸出的值。