记录cocos2dx学习中的笔记

前端之家收集整理的这篇文章主要介绍了记录cocos2dx学习中的笔记前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.解决使用extensions库编译问题

(1)首先在工程解决方案中右键->属性->add existing project->到目录..../cocosed/extensions/proj.win32/libExtensions.vcxproj

(2) 在自己建的project添加新引用,属性->common properities->add new reference.....

(3)在自己建的project 属性->Configuration properties->C/C++->Additional Include Directires->add "$(EngineRoot)"

2.引入库后,加一个按钮代码段如下

  1. #include"cocos-ext.h"
  2. using namespace cocos2d::extension;
  1. <span style="white-space:pre"> </span>auto* nine = Scale9Sprite::create("CloseNormal.png");
  2. auto* ninedown = Scale9Sprite::create("CloseSelected.png");
  3.  
  4. Label *title = Label::create("Touch me!","Marker Felt",30);
  5.  
  6. auto* ctlbtn = ControlButton::create(title,nine);
  7. ctlbtn->setBackgroundSpriteForState(ninedown,Control::State::HIGH_LIGHTED);
  8.  
  9. ctlbtn->setPosition(Point(200,200));
  10.  
  11. this->addChild(ctlbtn);

3.CocoStudio UI 编辑器

(1)在画布加载好控件后,直接Ctrl+E 导出画布,把export中的三个文件放到工程的resource目录下。

(2)导入libCocosStudio.vcxproj 和libGui.vcxproj库,加"$(EngineRoot)\cocos\editor-support",$(EngineRoot)\cocos,$(EngineRoot).

(3)代码注意加的片段

  1. #include"editor-support\cocostudio\CCSGUIReader.h"
  2. #include"ui\CocosGUI.h"
  3. using namespace cocos2d::ui;
  4. using namespace cocostudio;
  1. auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("ForLearning_1.ExportJson");
  2. UI->setPosition(Point(200,200));
  3. this->addChild(UI);

4.Cocos2dx添加有米广告

(1)把有米SDK 里面YoumiSdk_v4.10_2014-09-25.jar加进eclipse的lib目录下

(2)Coco2dx 加广告,需要通过JNI来调用java端的方法

java端代码如下

  1. public class AppActivity extends Cocos2dxActivity {
  2. public static AppActivity youmiads ;
  3. protected void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);
  5. /*start:有米广告代码*/
  6. youmiads = this;
  7. //最后的 boolean 值为是否开启测试模式,true 为是,false 为否。(上传有米审核及发布到市场版本,请设置为 false)
  8. AdManager.getInstance(this).init("xxxxxxxxx","xxxxxxxxxxxxx",false);
  9. FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
  10. layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT;
  11. AdView adView = new AdView(this,AdSize.FIT_SCREEN);
  12. this.addContentView(adView,layoutParams);
  13. adView.setAdListener(new AdViewListener() {
  14. @Override
  15. public void onSwitchedAd(AdView arg0) {
  16. Log.d("AppActivity","onSwitchedAd");
  17. }
  18. @Override
  19. public void onReceivedAd(AdView arg0) {
  20. Log.d("AppActivity","onReceivedAd");
  21.  
  22. }
  23. @Override
  24. public void onFailedToReceivedAd(AdView arg0) {
  25. Log.d("AppActivity","onFailedToReceivedAd");
  26. }
  27. });
  28. SpotManager.getInstance(this).loadSpotAds();
  29. //设置展示超时时间,加载超时则不展示广告,默认0,代表不设置超时时间
  30. SpotManager.getInstance(this).setSpotTimeout(5000);// 设置5秒
  31. SpotManager.getInstance(this).setShowInterval(20);// 设置20秒的显示时间间隔
  32. /*end:有米广告代码*/
  33. }
  34. public static Object getActivity(){
  35. System.out.println("object");
  36. return youmiads;
  37. }
  38. public void showAds(){
  39. Log.d("AppActivity","show ads");
  40. /*start:有米广告*.*/
  41. SpotManager.getInstance(this).showSpotAds(
  42. this,new SpotDialogListener() {
  43. @Override
  44. public void onShowSuccess() {
  45. Log.d("AppActivity","onShowSuccess");
  46. }
  47.  
  48. @Override
  49. public void onShowFailed() {
  50. Log.d("AppActivity","onShowFailed");
  51. }
  52.  
  53. @Override
  54. public void onSpotClosed() {
  55. Log.e("AppActivity","onSpotClosed");
  56. }
  57.  
  58. });
  59. //end:有米广告*/
  60. //SpotManager.getInstance(this).showSpotAds(this);
  61. }
  62. public void missionads(){
  63. SpotManager.getInstance(this).disMiss(true);
  64. }
  65. @Override
  66. protected void onStop(){
  67. SpotManager.getInstance(this).disMiss(true);
  68. super.onStop();
  69. }
  70. }

(3) cocos2dx端C++代码调用
  1. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  2. #include<jni.h>
  3. #include"platform/android/jni/JniHelper.h"
  4. #define CLASS_NAME "org.cocos2dx.cpp.AppActivity"
  5. #define DEBUG 1
  6. #endif
  7.  
  8. void AppDelegate::showAds()
  9. {
  10.  
  11. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  12.  
  13. JniMethodInfo minfo;
  14. bool isHava;
  15. #ifdef DEBUG
  16. {
  17. isHava = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity","getJavaActivity","()Ljava/lang/object");
  18. jobject activityObj;
  19. if(isHava)
  20. {
  21. activityObj = minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID);
  22. }
  23. isHava = JniHelper::getMethodInfo(minfo,"showAds","()V");
  24. if(isHava)
  25. {
  26. minfo.env->CallVoidMethod(minfo.classID,minfo.methodID);
  27. }
  28. }
  29. #endif
  30. #endif
  31. }

猜你在找的Cocos2d-x相关文章