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.引入库后,加一个按钮代码段如下
#include"cocos-ext.h" using namespace cocos2d::extension;
<span style="white-space:pre"> </span>auto* nine = Scale9Sprite::create("CloseNormal.png"); auto* ninedown = Scale9Sprite::create("CloseSelected.png"); Label *title = Label::create("Touch me!","Marker Felt",30); auto* ctlbtn = ControlButton::create(title,nine); ctlbtn->setBackgroundSpriteForState(ninedown,Control::State::HIGH_LIGHTED); ctlbtn->setPosition(Point(200,200)); this->addChild(ctlbtn);
3.CocoStudio UI 编辑器
(1)在画布加载好控件后,直接Ctrl+E 导出画布,把export中的三个文件放到工程的resource目录下。
(2)导入libCocosStudio.vcxproj 和libGui.vcxproj库,加"$(EngineRoot)\cocos\editor-support",$(EngineRoot)\cocos,$(EngineRoot).
(3)代码注意加的片段
#include"editor-support\cocostudio\CCSGUIReader.h" #include"ui\CocosGUI.h" using namespace cocos2d::ui; using namespace cocostudio;
auto UI = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("ForLearning_1.ExportJson"); UI->setPosition(Point(200,200)); this->addChild(UI);
4.Cocos2dx添加有米广告
(1)把有米SDK 里面YoumiSdk_v4.10_2014-09-25.jar加进eclipse的lib目录下
(2)Coco2dx 加广告,需要通过JNI来调用java端的方法
java端代码如下
public class AppActivity extends Cocos2dxActivity { public static AppActivity youmiads ; protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); /*start:有米广告代码*/ youmiads = this; //最后的 boolean 值为是否开启测试模式,true 为是,false 为否。(上传有米审核及发布到市场版本,请设置为 false) AdManager.getInstance(this).init("xxxxxxxxx","xxxxxxxxxxxxx",false); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; AdView adView = new AdView(this,AdSize.FIT_SCREEN); this.addContentView(adView,layoutParams); adView.setAdListener(new AdViewListener() { @Override public void onSwitchedAd(AdView arg0) { Log.d("AppActivity","onSwitchedAd"); } @Override public void onReceivedAd(AdView arg0) { Log.d("AppActivity","onReceivedAd"); } @Override public void onFailedToReceivedAd(AdView arg0) { Log.d("AppActivity","onFailedToReceivedAd"); } }); SpotManager.getInstance(this).loadSpotAds(); //设置展示超时时间,加载超时则不展示广告,默认0,代表不设置超时时间 SpotManager.getInstance(this).setSpotTimeout(5000);// 设置5秒 SpotManager.getInstance(this).setShowInterval(20);// 设置20秒的显示时间间隔 /*end:有米广告代码*/ } public static Object getActivity(){ System.out.println("object"); return youmiads; } public void showAds(){ Log.d("AppActivity","show ads"); /*start:有米广告*.*/ SpotManager.getInstance(this).showSpotAds( this,new SpotDialogListener() { @Override public void onShowSuccess() { Log.d("AppActivity","onShowSuccess"); } @Override public void onShowFailed() { Log.d("AppActivity","onShowFailed"); } @Override public void onSpotClosed() { Log.e("AppActivity","onSpotClosed"); } }); //end:有米广告*/ //SpotManager.getInstance(this).showSpotAds(this); } public void missionads(){ SpotManager.getInstance(this).disMiss(true); } @Override protected void onStop(){ SpotManager.getInstance(this).disMiss(true); super.onStop(); } }
(3) cocos2dx端C++代码调用
#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include<jni.h> #include"platform/android/jni/JniHelper.h" #define CLASS_NAME "org.cocos2dx.cpp.AppActivity" #define DEBUG 1 #endif void AppDelegate::showAds() { #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo minfo; bool isHava; #ifdef DEBUG { isHava = JniHelper::getStaticMethodInfo(minfo,"org.cocos2dx.cpp.AppActivity","getJavaActivity","()Ljava/lang/object"); jobject activityObj; if(isHava) { activityObj = minfo.env->CallStaticObjectMethod(minfo.classID,minfo.methodID); } isHava = JniHelper::getMethodInfo(minfo,"showAds","()V"); if(isHava) { minfo.env->CallVoidMethod(minfo.classID,minfo.methodID); } } #endif #endif }