1、coco2d-x 没有浏览器控件,或者有没有方法在cocos内直接显示一个 html页面(别告诉我弹出浏览器这方式)
2、coco2d-x 中怎么使用一些特定设备的函数或功能,比如 iphone有指纹、android有日期提醒等跟系统有关的特殊功能
最好有个demo代码
1、coco2d-x 没有浏览器控件,或者有没有方法在cocos内直接显示一个 html页面(别告诉我弹出浏览器这方式)
2、coco2d-x 中怎么使用一些特定设备的函数或功能,比如 iphone有指纹、android有日期提醒等跟系统有关的特殊功能
最好有个demo代码
自己研究了一些,提供给大家分享
我在3.9下找到一个WebView的类,貌似是Web浏览器。
我先测试一下,到时候跟大家分享
第一个问题算是解决了,我直接新建一个项目,只修改“HelloWorldScene.h”、“HelloWorldScene.cpp”
具体代码如下:
HelloWorldScene.h部分
#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H
#include “cocos2d.h”
#include “ui/CocosGUI.h”
class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
void btnClickCallback(cocos2d::Ref* pSender);
//用于webview事件处理
bool onWebViewShouldStartLoading(cocos2d::experimental::ui::WebView *sender, const std::string &url);
void onWebViewDidFinishLoading(cocos2d::experimental::ui::WebView *sender, const std::string &url);
void onWebViewDidFailLoading(cocos2d::experimental::ui::WebView *sender, const std::string &url);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
cocos2d::Label * labMsg;
cocos2d::experimental::ui::WebView * webMain;
};
#endif // HELLOWORLD_SCENE_H
HelloWorldScene.cpp部分
#include “HelloWorldScene.h”
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// ‘scene’ is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
labMsg = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
labMsg->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - labMsg->getContentSize().height));
// add the label as a child to this layer
this->addChild(labMsg, 1);
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// add the sprite as a child to this layer
this->addChild(sprite, 0);
//后面加入的代码
auto btntest = ui::Button::create();
btntest->setTag(1);
btntest->setTitleText("Test");
btntest->setTitleFontSize(24);
btntest->addClickEventListener(CC_CALLBACK_1(HelloWorld::btnClickCallback,this));
btntest->setPosition(Vec2(50,visibleSize.height -10 ));
btntest->setAnchorPoint(Vec2(0.5f,0.5f));
addChild(btntest);
//加入WebView
webMain = cocos2d::experimental::ui::WebView::create();
webMain->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
webMain->setContentSize(Size(visibleSize.width-150, visibleSize.height-50));
// webMain->loadURL(“http://www.baidu.com”);
webMain->setScalesPageToFit(true);
webMain->setOnShouldStartLoading(CC_CALLBACK_2(HelloWorld::onWebViewShouldStartLoading, this));
webMain->setOnDidFinishLoading(CC_CALLBACK_2(HelloWorld::onWebViewDidFinishLoading, this));
webMain->setOnDidFailLoading(CC_CALLBACK_2(HelloWorld::onWebViewDidFailLoading, this));
this->addChild(webMain);
return true;
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
void HelloWorld::btnClickCallback(cocos2d::Ref* pSender)
{
webMain->loadURL(“http://www.baidu.com”);
labMsg->setString(“JustTest”);
}
bool HelloWorld::onWebViewShouldStartLoading(experimental::ui::WebView *sender, const std::string &url)
{
CCLOG(“onWebViewShouldStartLoading, url is %s”, url.c_str());
return true;
}
void HelloWorld::onWebViewDidFinishLoading(experimental::ui::WebView sender, const std::string &url)
{
// auto node = (ui::Button)this->getChildByName(“evalJs”);
// node->setTitleText(“start loading…”);
CCLOG(“onWebViewDidFinishLoading, url is %s”, url.c_str());
}
void HelloWorld::onWebViewDidFailLoading(experimental::ui::WebView *sender, const std::string &url)
{
CCLOG(“onWebViewDidFailLoading, url is %s”, url.c_str());
}
以上代码只能在 android 和 iphone 上运行,我的cocos2d-x是3.9,由于每个版本都有所不同,请大家自行修改。
另外,如果是在xcode 7下,运行后WebView会没有反应,报以下错误:
“App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app’s Info.plist file”
需要做以下修改
1、在Info.plist中添加 App Transport Security Settings 类型 Dictionary ;
2、在 App Transport Security Settings 下添加 Allow Arbitrary Loads 类型Boolean ,值设为 YES;
第二个问题,根据两位大大的帖子
http://www.cocoachina.com/bbs/read.php?tid-224616.html
http://blog.csdn.net/qinning199/article/details/11750763
获得了在android下特殊功能的开发思路,利用cocos与android的相互调用方法达到独有功能的开发方法。
等写好demo再与大家分享。。。。
不过,iphone、mac、windows方面还没有找到资料,有人指点一二不?