#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp,so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
// Event Listener
virtual void onEnter() override;
void onTouchesBegan(const std::vector<Touch*>& touches,Event *event);
void onTouchesMoved(const std::vector<Touch*>& touches,Event *event);
void onTouchesEnded(const std::vector<Touch*>& touches,Event *event);
void onKeyPressed(EventKeyboard::KeyCode keyCode,Event* event);
void onKeyReleased(EventKeyboard::KeyCode keyCode,Event* event);
void onMouseDown(Event* event);
void onMouseUp(Event* event);
void onMouseMove(Event* event);
void onMouseScroll(Event* event);
Vec2 _beginPos;
void playTest(float dt);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
#include "MediaManager.h"
//USING_NS_CC;
void HelloWorld::onEnter()
{
Layer::onEnter();
auto s = Director::getInstance()->getWinSize();
// Myboard can receive mouse Left and middle and touchscreen
auto touchListener = EventListenerTouchAllAtOnce::create();
touchListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan,this);
touchListener->onTouchesMoved = CC_CALLBACK_2(HelloWorld::onTouchesMoved,this);
touchListener->onTouchesEnded = CC_CALLBACK_2(HelloWorld::onTouchesEnded,this);
_eventDispatcher->addEventListenerWithSceneGraPHPriority(touchListener,this);
// Process Mouse Right (KEYCODE_BACK) only onKeyReleased
auto keyboardListener = EventListenerKeyboard::create();
keyboardListener->onKeyPressed = CC_CALLBACK_2(HelloWorld::onKeyPressed,this);
keyboardListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased,this);
_eventDispatcher->addEventListenerWithSceneGraPHPriority(keyboardListener,this);
// Myboard can't receive any mouse events
auto mouseListener = EventListenerMouse::create();
mouseListener->onMouseDown = CC_CALLBACK_1(HelloWorld::onMouseDown,this);
mouseListener->onMouseMove = CC_CALLBACK_1(HelloWorld::onMouseMove,this);
mouseListener->onMouseUp = CC_CALLBACK_1(HelloWorld::onMouseUp,this);
mouseListener->onMouseScroll = CC_CALLBACK_1(HelloWorld::onMouseScroll,this);
_eventDispatcher->addEventListenerWithSceneGraPHPriority(mouseListener,this);
}
void HelloWorld::onTouchesBegan(const std::vector<Touch*>& touches,Event *event)
{
auto touch = static_cast<Touch*>(touches[0]);
_beginPos = touch->getLocation();
log("***EVENT:%s: x=%f,y=%f\n",__FUNCTION__,_beginPos.x,_beginPos.y);
}
void HelloWorld::onTouchesMoved(const std::vector<Touch*>& touches,Event *event)
{
auto touch = static_cast<Touch*>(touches[0]);
auto touchLocation = touch->getLocation();
log("***EVENT:%s: x=%f,touchLocation.x,touchLocation.y);
}
void HelloWorld::onTouchesEnded(const std::vector<Touch*>& touches,touchLocation.y);
}
void HelloWorld::onKeyPressed(EventKeyboard::KeyCode keyCode,Event* event)
{
CC_UNUSED_PARAM(event);
log("***EVENT:%s: keyCode=%d\n",keyCode);
}
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode,keyCode);
if(EventKeyboard::KeyCode::KEY_BACK == keyCode){
Director::getInstance()->end();
}
}
template <typename T> std::string tostr(const T& t) { std::ostringstream os; os<<t; return os.str(); }
void HelloWorld::onMouseDown(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "Mouse Down detected,Key: ";
str += tostr(e->getMouseButton());
//_labelAction->setString(str.c_str());
log("***EVENT:%s:%s\n",str.c_str());
}
void HelloWorld::onMouseUp(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "Mouse Up detected,Key: ";
str += tostr(e->getMouseButton());
log("***EVENT:%s:%s\n",str.c_str());
}
void HelloWorld::onMouseMove(Event *event)
{
EventMouse* e = (EventMouse*)event;
std::string str = "MousePosition X:";
str = str + tostr(e->getCursorX()) + " Y:" + tostr(e->getCursorY());
log("***EVENT:%s:%s\n",str.c_str());
}
void HelloWorld::onMouseScroll(Event *event)
{
EventMouse* e = (EventMouse*)event;
//auto e = static_cast<EventMouse*>(event);
std::string str = "Mouse Scroll detected,X: ";
str = str + tostr(e->getScrollX()) + " Y: " + tostr(e->getScrollY());
log("***EVENT:%s:%s\n",str.c_str());
}
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();
log("TEST: x=%f,y=%f,w=%f,h=%f\n",origin.x,origin.y,visibleSize.width,visibleSize.height);
/////////////////////////////
// 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,nullptr);//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
auto label = Label::createWithTTF("Hello World","fonts/Marker Felt.ttf",24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label,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);
//MediaManager::getInstance()->play("/mnt/sdcard/video/cg720p.mp4");
// every 10s,call HelloWorld::playTest
//this->schedule(CC_SCHEDULE_SELECTOR(HelloWorld::playTest),10);
return true;
}
void HelloWorld::playTest(float dt)
{
//MediaManager::getInstance()->seekTo(20*1000);
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
原文链接:https://www.f2er.com/cocos2dx/346534.html