http://www.2cto.com/kf/201409/339508.html
本文使用万普广告平台往Cocos2d-x3.1项目中添加广告。万普的SDK for Cocos2d-x功能挺全的,只是官方文档的使用说明中使用的Cocos2d-x引擎是2.0.1,平台是Windows下的,年代有点久远,本文主要介绍Mac10.9.5+XCode6.0.1+Cocos2d-x使用万普添加广告。首先赞一下万普的工作人员,头一天晚上注册使用,第二天就打电话问候有没有问题~
很简单,输入应用名称和应用平台,点击下一步即可获得APP_ID,不需要再点SDK下载。
这个无需解释,控制台下的cocos new 命令创建。
首先出现两个问题:1)Unable to resolve target 'android-10'。解决办法:修改project.properties中的target属性为你ADT存在的。其次,修改AndroidManifest.xml为android:minSdkVersion='11' android:maxSdkVersion='18',同样改为你ADT中存在的。2)org.cocos2dx.cpp下的AppActivity.java报错。解决方案:点击工程目录->Properties->Java Build Path->Source/Link Source/Browse。选择引擎目录下cocos/platform/android/java/src。OK,修改Folder name,随便写一个即可。OK。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<span style=
"font-size:14px;"
>
import
org.cocos2dx.lib.Cocos2dxActivity;
import
org.cocos2dx.lib.Cocos2dxEditText;
import
org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import
org.cocos2dx.lib.Cocos2dxRenderer;
import
android.app.ActivityManager;
import
android.content.Context;
import
android.content.pm.ConfigurationInfo;
import
android.os.Bundle;
import
android.util.Log;
import
android.view.Gravity;
import
android.view.ViewGroup;
import
android.widget.FrameLayout;
import
android.widget.LinearLayout;
import
cn.waps.AppConnect;
public
class
AppActivity
extends
Cocos2dxActivity {
private
Cocos2dxGLSurfaceView mGLView;
private
static
Context mContext;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
mContext =
this
;
if
(detectOpenGLES20()) {
// 第一个参数就是应用的id,第二个参数是渠道标识,参考手册里面有渠道标识的对照表
AppConnect.getInstance(
"</span><span style="
font-family: Arial, 微软雅黑;
"><strong><span style="
font-size:12px;color:#cc0000;
">510a67751f53b4c4d05ed9f56373cc92</span></strong></span><span style="
font-size:14px;
">"
,
//APP_ID
"default"
,
this
);
// FrameLayout
ViewGroup.LayoutParams framelayout_params =
new
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
FrameLayout framelayout =
new
FrameLayout(
this
);
framelayout.setLayoutParams(framelayout_params);
// Cocos2dxEditText layout
ViewGroup.LayoutParams edittext_layout_params =
new
ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Cocos2dxEditText edittext =
new
Cocos2dxEditText(
this
);
edittext.setLayoutParams(edittext_layout_params);
// ...add to FrameLayout
framelayout.addView(edittext);
// Cocos2dxGLSurfaceView
mGLView =
new
Cocos2dxGLSurfaceView(
this
);
// ...add to FrameLayout
framelayout.addView(mGLView);
mGLView.setEGLContextClientVersion(
2
);
mGLView.setCocos2dxRenderer(
new
Cocos2dxRenderer());
// mGLView.setTextField(edittext);
// Set framelayout as the content view
setContentView(framelayout);
//迷你广告
LinearLayout adMiniLayout =
new
LinearLayout(
this
);
adMiniLayout.setOrientation(LinearLayout.VERTICAL);
FrameLayout.LayoutParams lp_mini =
new
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
//设置adMiniLayout的悬浮位置,具体的位置开发者根据需要设置
lp_mini.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
this
.addContentView(adMiniLayout, lp_mini);
LinearLayout miniLayout =
new
LinearLayout(
this
);
miniLayout.setGravity(Gravity.CENTER_HORIZONTAL);
adMiniLayout.addView(miniLayout);
// 添加迷你广告
AppConnect.getInstance(
this
).showMiniAd(
this
, miniLayout,
10
);
}
else
{
Log.d(
"activity"
,
"don't support gles2.0"
);
finish();
}
}
private
boolean
detectOpenGLES20() {
ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
ConfigurationInfo info = am.getDeviceConfigurationInfo();
return
(info.reqGlEsVersion >=
0x20000
);
}
static
{
System.loadLibrary(
"cocos2dcpp"
);
}
} </span>
|
1
2
3
4
5
6
|
<uses-permission android:name=
"android.permission.INTERNET"
>
//允许程序打开网络套接字
<uses-permission android:name=
"android.permission.ACCESS_NETWORK_STATE"
>
//允许程序访问有关GSM网络信息
<uses-permission android:name=
"android.permission.ACCESS_WIFI_STATE"
>
//允许程序访问wifi网络信息
<uses-permission android:name=
"android.permission.READ_PHONE_STATE"
>
//读取手机状态权限
<uses-permission android:name=
"android.permission.WRITE_EXTERNAL_STORAGE"
>
//手机SD卡写入权限
<uses-permission android:name=
"android.permission.GET_TASKS"
>
//允许一个程序获取信息相关当前或最近运行的任务。一个缩略的任务状态,是否活动等待</uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission>
|
1
|
|
编译过程中可能会遇到各种问题。
1)CDT问题:点击项目名称->Properties->Builders,去掉CDT Builder的勾选。
2)NDK路径问题:ADT置于屏幕最前端,ADT->Preferences->Android->NDK->NDK Location框中添加NDK的相对路径。
3)NDK命令问题:点击项目名称->Properties->C/C++ Build->Environment,Add,添加name:NDK_ROOT,value:NDK相对路径,获取可以通过命令行获取。
4)NDK模块问题:点击项目名称->Properties->Java Build Path/Source/Link Source。实质上这一步的问题在第6步解决了。
5)其他问题:如果还是提示target问题,提高AndroidManifest.xml的最小NDK值,或者重启一下ADT就好了。