cocos2d-x接受键盘事件,左右键, 多点触摸

cocos2d-x接受键盘事件,左右键, 多点触摸,第1张

概述首先在AppDelegate.cpp加入以下代码,一定要在AppDelegate::applicationDidFinishLaunching()上,声明用的。 [html]  view plain copy #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)   HelloWorld *g_layer;      void Win32SetKeyLayer

首先在AppDelegate.cpp加入以下代码,一定要在AppDelegate::applicationDIDFinishLaunching()上,声明用的。

[HTML] view plain copy #if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32) HelloWorld*g_layer; voIDWin32SetKeyLayer(HelloWorld*layer) { g_layer=layer; } voIDWin32KeyHook(UINTmessage,WParaMwParam,LParaMlParam) { cclog("Win32KeyHookmessage%DWParam%dlParam%d",message,wParam,lParam); if(g_layer) g_layer->onWin32KeyEvent(message,lParam); } #endif
在AppDelegate::applicationDIDFinishLaunching()中

copy boolAppDelegate::applicationDIDFinishLaunching(){ //initializedirector 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); //turnondisplayFPS pDirector->setdisplayStats(true); //setFPS.thedefaultvalueis1.0/60ifyoudon'tcallthis >setAnimationInterval(1.0/60); //createascene.it'sanautoreleaSEObject CCScene*pScene=HelloWorld::scene(); //run >runWithScene(pScene); returntrue; }
在HelloWorldScene.cpp中

copy CCScene*HelloWorld::scene() //'scene'isanautoreleaSEObject CCScene*scene=CCScene::create(); //'layer'isanautoreleaSEObject HelloWorld*layer=HelloWorld::create(); //addlayerasachildtoscene scene->addChild(layer); externvoIDWin32SetKeyLayer(HelloWorld*layer); Win32SetKeyLayer(layer); //returnthescene returnscene; }
copy voIDHelloWorld::onWin32KeyEvent(UINTmessage,LParaMlParam) cclog("onWin32KeyEventmessage%DWParam%dlParam%d",lParam); /* //Up Win32KeyHookmessage256wParam38lParam21495809 onWin32KeyEventmessage256wParam38lParam21495809 Win32KeyHookmessage257wParam38lParam-1052246015 onWin32KeyEventmessage257wParam38lParam-1052246015 //Down Win32KeyHookmessage256wParam40lParam22020097 onWin32KeyEventmessage256wParam40lParam22020097 Win32KeyHookmessage257wParam40lParam-1051721727 onWin32KeyEventmessage257wParam40lParam-1051721727 //left Win32KeyHookmessage256wParam37lParam21692417 onWin32KeyEventmessage256wParam37lParam21692417 Win32KeyHookmessage257wParam37lParam-1052049407 onWin32KeyEventmessage257wParam37lParam-1052049407 //Right Win32KeyHookmessage256wParam39lParam21823489 onWin32KeyEventmessage256wParam39lParam21823489 Win32KeyHookmessage257wParam39lParam-1051918335 onWin32KeyEventmessage257wParam39lParam-1051918335 */ if(message==256) switch(wParam) case38: moveHero(1); break; case40: moveHero(2); break; case37: moveHero(3); case39: moveHero(4); elseif(message==257) copy voIDHelloWorld::moveHero(intdiraction) cclog("moveHero:%d",diraction);

//现在你就可以上下左右键,看输出的值。


http://www.vktone.com/articles/win32_key_process_in_cocos2dx.html

总结

以上是内存溢出为你收集整理的cocos2d-x接受键盘事件,左右键, 多点触摸全部内容,希望文章能够帮你解决cocos2d-x接受键盘事件,左右键, 多点触摸所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1057499.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-25
下一篇 2022-05-25

发表评论

登录后才能评论

评论列表(0条)

保存