
用户交互这里指的就是用户在手机上的点击,滑动以及晃动手机等行为,从而得到相应的反馈。今天学习Cocos2dx,遇到交互问题,所以就写出来和大家分享一下。我这里是以AndroID连接为例的,因为目前我只会AndroID相关的开发。好了,不多说,看下面步骤:
第一步:在AndroID中,交互 *** 作的入口在SurfaceVIEw或是GLSurfaceVIEw中的ontouchEvent时间中。本例代码所在位置org.cocos2dx.lib---->Cocos2dxGLSurfaceVIEw.java
[java] view plain copy publicbooleanontouchEvent(finalMotionEventpMotionEvent){ //thesedataareusedinACTION_MOVEandACTION_CANCEL finalintpointerNumber=pMotionEvent.getPointerCount(); int[]IDs=newint[pointerNumber]; float[]xs=float[pointerNumber]; float[]ys=float[pointerNumber]; for(inti=0;i<pointerNumber;i++){ IDs[i]=pMotionEvent.getPointerID(i); xs[i]=pMotionEvent.getX(i); ys[i]=pMotionEvent.getY(i); } switch(pMotionEvent.getAction()&MotionEvent.ACTION_MASK){ caseMotionEvent.ACTION_POINTER_DOWN: intindexPointerDown=pMotionEvent.getAction()>>MotionEvent.ACTION_POINTER_INDEX_SHIFT; intIDPointerDown=pMotionEvent.getPointerID(indexPointerDown); floatxPointerDown=pMotionEvent.getX(indexPointerDown); floatyPointerDown=pMotionEvent.getY(indexPointerDown); this.queueEvent(newRunnable(){ @OverrIDe voIDrun(){ Cocos2dxGLSurfaceVIEw.this.mCocos2dxRenderer.handleActionDown(IDPointerDown,xPointerDown,yPointerDown); } }); break; caseMotionEvent.ACTION_DOWN: //thereareonlyonefingeronthescreen intIDDown=pMotionEvent.getPointerID(0); floatxDown=xs[0]; floatyDown=ys[0]; this.mCocos2dxRenderer.handleActionDown(IDDown,xDown,yDown); caseMotionEvent.ACTION_MOVE: newRunnable(){ @OverrIDe voIDrun(){ Cocos2dxGLSurfaceVIEw.this.mCocos2dxRenderer.handleActionMove(IDs,xs,ys); }); break; caseMotionEvent.ACTION_POINTER_UP: intindexPointUp=pMotionEvent.getAction()>>MotionEvent.ACTION_POINTER_INDEX_SHIFT; intIDPointerUp=pMotionEvent.getPointerID(indexPointUp); floatxPointerUp=pMotionEvent.getX(indexPointUp); floatyPointerUp=pMotionEvent.getY(indexPointUp); this.mCocos2dxRenderer.handleActionUp(IDPointerUp,xPointerUp,yPointerUp); caseMotionEvent.ACTION_UP: //thereareonlyonefingeronthescreen intIDUp=pMotionEvent.getPointerID(0); floatxUp=xs[floatyUp=ys[this.mCocos2dxRenderer.handleActionUp(IDUp,xUp,yUp); caseMotionEvent.ACTION_CANCEL: this.mCocos2dxRenderer.handleActionCancel(IDs,ys); returntrue; }
第二步:Cocos2dxGLSurfaceVIEw.this.mCocos2dxRenderer.handleActionDown(IDDown,yDown)等相关语句的方法在org.cocos2dx.lib------>Cocos2dxRender.java,代码如下:
copy privatestaticnativevoIDnativetouchesBegin(intID,floatx,153); background-color:inherit; Font-weight:bold">floaty); voIDnativetouchesEnd(floaty); voIDnativetouchesMove(int[]IDs,153); background-color:inherit; Font-weight:bold">float[]xs,153); background-color:inherit; Font-weight:bold">float[]ys); voIDnativetouchesCancel(float[]ys); voIDhandleActionDown(floaty){ Cocos2dxRenderer.nativetouchesBegin(ID,x,y); voIDhandleActionUp( Cocos2dxRenderer.nativetouchesEnd(ID,153); background-color:inherit; Font-weight:bold">voIDhandleActionCancel(float[]ys){ Cocos2dxRenderer.nativetouchesCancel(IDs,ys); voIDhandleActionMove( Cocos2dxRenderer.nativetouchesMove(IDs,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }
copy protectedvoIDonLoadNativelibrarIEs(){ try{ ApplicationInfoai=getPackageManager().getApplicationInfo(getPackagename(),PackageManager.GET_Meta_DATA); Bundlebundle=ai.MetaData; Stringlibname=bundle.getString("androID.app.lib_name"); System.loadlibrary(libname); }catch(Exceptione){ e.printstacktrace(); } bundle.getString("androID.app.lib_name")文件是找出.so文件。和它相关内容在AndroID工程中AndroIDManifest.xml里,内容如下:
[HTML] copy <?xmlversion="1.0"enCoding="utf-8"?> <manifestxmlns:androID="http://schemas.androID.com/apk/res/androID" package="org.cocos.CocosProject4" androID:versionCode="1" androID:versionname="1.0" androID:installLocation="auto"> uses-sdkandroID:minSdkVersion="9"/> uses-featureandroID:glEsversion="0x00020000"/> applicationandroID:label="@string/app_name" androID:icon="@drawable/icon" <!--TellCocos2dxActivitythenameofour.so--> Meta-dataandroID:name="androID.app.lib_name" androID:value="cocos2dcpp"activityandroID:name="org.cocos2dx.cpp.AppActivity" androID:label="@string/app_name" androID:screenorIEntation="landscape" androID:theme="@androID:style/theme.NoTitlebar.Fullscreen" androID:configChanges="orIEntation"intent-filteractionandroID:name="androID.intent.action.MAIN"categoryandroID:name="androID.intent.category.LAUNCHER"</> activityapplicationsupports-screensandroID:anyDensity="true" androID:smallScreens="true" androID:normalScreens="true" androID:largeScreens="true" androID:xlargeScreens="true"uses-permissionandroID:name="androID.permission.INTERNET"manifest>
第四步:cocos2dcpp.so文件的生成,因为native 方法都在这里面。它的生成在AndroID工程中的jni文件里么的AndroID.mk文件(相关内容参考链接)。native方法所在文件路径cocos2d-x-3.6/cocos/platform/androID/jni/touchesJni.cpp,内容如下:
[cpp] copy #include"base/CCDirector.h" #include"base/CCEventKeyboard.h" #include"base/CCEventdispatcher.h" #include"platform/androID/CCGLVIEwImpl-androID.h" #include<androID/log.h> #include<jni.h> usingnamespacecocos2d; extern"C"{ JNIEXPORTvoIDJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativetouchesBegin(jnienv*env,jobjectthiz,jintID,jfloatx,jfloaty){ intptr_tIDlong=ID; cocos2d::Director::getInstance()->getopenGLVIEw()->handletouchesBegin(1,&IDlong,&x,&y); JNIEXPORTvoIDJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativetouchesEnd(jnienv*env,jfloaty){ intptr_tIDlong=ID; cocos2d::Director::getInstance()->getopenGLVIEw()->handletouchesEnd(1,&y); voIDJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativetouchesMove(jnienv*env,jintArrayIDs,jfloatArrayxs,jfloatArrayys){ intsize=env->GetArrayLength(IDs); jintID[size]; jfloatx[size]; jfloaty[size]; env->GetIntArrayRegion(IDs,size,ID); env->GetfloatArrayRegion(xs,x); env->GetfloatArrayRegion(ys,y); intptr_tIDlong[size]; for(inti=0;i<size;i++) IDlong[i]=ID[i]; cocos2d::Director::getInstance()->getopenGLVIEw()->handletouchesMove(size,IDlong,153); background-color:inherit; Font-weight:bold">voIDJNICALLJava_org_cocos2dx_lib_Cocos2dxRenderer_nativetouchesCancel(jnienv*env,jfloatArrayys){ intsize=env->GetArrayLength(IDs); jintID[size]; jfloatx[size]; jfloaty[size]; env->GetIntArrayRegion(IDs,ID); env->GetfloatArrayRegion(xs,x); env->GetfloatArrayRegion(ys,87); background-color:inherit; Font-weight:bold">intptr_tIDlong[size]; inti=0;i<size;i++) IDlong[i]=ID[i]; cocos2d::Director::getInstance()->getopenGLVIEw()->handletouchesCancel(size,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> }
第五步:cocos2d::Director::getInstance()->getopenGLVIEw()->handletouchesBegin(1,&y)这个方法是如何起作用的呢,文件位置在 cocos2d-x-3.6/cocos/platform/CCGLVIEw.cpp,内如如下:
copy voIDGLVIEw::handletouchesBegin(intnum,intptr_tIDs[],87); background-color:inherit; Font-weight:bold">floatxs[],87); background-color:inherit; Font-weight:bold">floatys[]) { intptr_tID=0; floatx=0.0f; floaty=0.0f; intunusedindex=0; EventtouchtouchEvent; inti=0;i<num;++i) ID=IDs[i]; x=xs[i]; y=ys[i]; autoiter=g_touchIDReorderMap.find(ID); //itisanewtouch if(iter==g_touchIDReorderMap.end()) { unusedindex=getUnUsedindex(); //ThetouchesismorethanMAX_touches? if(unusedindex==-1){ cclOG("ThetouchesismorethanMAX_touches,unusedindex=%d",unusedindex); continue; touch*touch=g_touches[unusedindex]=new(std::nothrow)touch(); touch->settouchInfo(unusedindex,(x-_vIEwPortRect.origin.x)/_scaleX, (y-_vIEwPortRect.origin.y)/_scaleY); cclOGINFO("x=%fy=%f",touch->getLocationInVIEw().x,touch->getLocationInVIEw().y); g_touchIDReorderMap.insert(std::make_pair(ID,unusedindex)); touchEvent._touches.push_back(touch); if(touchEvent._touches.size()==0) cclOG("touchesBegan:size=0"); return; touchEvent._eventCode=Eventtouch::EventCode::BEGAN; autodispatcher=Director::getInstance()->getEventdispatcher(); dispatcher->dispatchEvent(&touchEvent); }
第六步:使用。首先要给_eventdispathcer(在CCNote.cpp文件中)添加Listener,示例如下:
copy autoListener=EventListenertouchAllAtOnce::create(); Listener->ontouchesBegan=CC_CALLBACK_2(ParticleDemo::ontouchesBegan,this); Listener->ontouchesMoved=CC_CALLBACK_2(ParticleDemo::ontouchesMoved,153); background-color:inherit; Font-weight:bold">this); Listener->ontouchesEnded=CC_CALLBACK_2(ParticleDemo::ontouchesEnded,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> _eventdispatcher->addEventListenerWithSceneGraPHPriority(Listener,153); background-color:inherit; Font-weight:bold">this); 其次就是我们实际 *** 作,使交互产生什么样的变化,代码如下:
copy voIDParticleDemo::ontouchesBegan(conststd::vector<touch*>&touches,Event*event) ontouchesEnded(touches,event); voIDParticleDemo::ontouchesMoved(ontouchesEnded(touches,event); voIDParticleDemo::ontouchesEnded( autotouch=touches[0]; autolocation=touch->getLocation(); autopos=Vec2::ZERO; if(_background) pos=_background->convertToWorldspace(Vec2::ZERO); if(_emitter!=nullptr) _emitter->setposition(location-pos); } 上面代码是ParticleTest中的内容,通过dispathcer将参数最终传递给要变化的_mitter,就是粒子发射器的移动。 总结
以上是内存溢出为你收集整理的Cocos2d-x-3.6 用户交互原理---------如何通过JNI连接Java和C++全部内容,希望文章能够帮你解决Cocos2d-x-3.6 用户交互原理---------如何通过JNI连接Java和C++所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)