1. 头文件

#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__

2. 代码

#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
}

cocos2d-x-3.3rc0事件处理的更多相关文章

  1. 详解iOS游戏开发中Cocos2D的坐标位置关系

    这篇文章主要介绍了iOS游戏开发中Cocos2D的坐标位置关系,Cocos2D是专门用来开发iOS游戏的开源框架,文中示例代码采用Objective-C语言,需要的朋友可以参考下

  2. 剖析iOS开发中Cocos2d-x的内存管理相关操作

    这篇文章主要介绍了剖析iOS开发中Cocos2d-x的内存管理相关操作,Cocos2d-x是开发游戏的利器,需要的朋友可以参考下

  3. iOS开发中使用cocos2d添加触摸事件的方法

    这篇文章主要介绍了iOS开发中使用cocos2d添加触摸事件的方法,cocos2d是制作iOS游戏的利器,需要的朋友可以参考下

  4. ios – cocos2d 2.0-rc2:结束导演并重启

    我有一个使用UIKitmenues的cocos2d驱动游戏,所以我只使用一个viewcontroller的框架,这是游戏本身.此外,它只有一个场景.由于cocos2d2.0导演本身是一个UIViewController子类,所以我只需在用户点击一个开始按钮时将其推送到我的MenuViewController中:当用户启动第一个游戏时,第一次调用该方法时,这很好.当游戏结束时,我调用[[CCDire

  5. cocos2d-iphone – CCNode和CCLayer之间的区别?

    Cocos2D中的CCNode和cclayer有什么区别?

  6. cocos2d-iphone – 跟踪cocos2d中的轨迹路径

    如何跟踪cocos2d中球流动的路径例如在愤怒的小鸟游戏视频中鸟儿流动的动作.他们显示路径.同样我想要您可以通过使用NSTimer或CAdisplayLink重复使用物体在其移动路径中的坐标并在该坐标处显示一个小点来实现此目的.计时器将开始,例如在愤怒的小鸟中,当鸟首次发射时,然后当鸟击中某物时停止.>启动鸟>此时每隔1/6的点就会在鸟坐标处绘制一个点>当鸟撞到某物时,计时器停止,因此不再绘制点

  7. Cocos2D OR libgdx for Android游戏开发

    本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请发送邮件至dio@foxmail.com举报,一经查实,本站将立刻删除。

  8. Cocos2d 2.0将UIView添加到CClayer openglview已弃用

    我有一些麻烦添加一个UIView到cocos2dv2.0,因为openGlView已被弃用,我无法添加视图.我在寻找另外一种方法来做到这一点,但是找不到任何东西.任何人都可以告诉我在所有cc层之前添加一个UIView的新方法?或者我该如何完成这个?使用这个而不是openGLView:

  9. cocos2d-iphone – Cocos2d – 一种获取当前场景的方法?

    我正在寻找一种获取当前场景的方法,以便我可以随时了解哪个场景正在运行.谢谢!

  10. cocos2d-iphone – Cocos2d Box2d或Chipmunk

    只需打开名为“cocos2d-iphone”的Xcode项目,并查看Box2dtest和Chipmunkdemo。Box2d更强大,但是我觉得比Chipmunk更难使用。

随机推荐

  1. 【cocos2d-x 3.x 学习笔记】对象内存管理

    Cocos2d-x的内存管理cocos2d-x中使用的是上面的引用计数来管理内存,但是又增加了一些自己的特色。cocos2d-x中通过Ref类来实现引用计数,所有需要实现内存自动回收的类都应该继承自Ref类。下面是Ref类的定义:在cocos2d-x中创建对象通常有两种方式:这两中方式的差异可以参见我另一篇博文“对象创建方式讨论”。在cocos2d-x中提倡使用第二种方式,为了避免误用第一种方式,一般将构造函数设为protected或private。参考资料:[1]cocos2d-x高级开发教程2.3节[

  2. 利用cocos2dx 3.2开发消灭星星六如何在cocos2dx中显示中文

    由于编码的不同,在cocos2dx中的Label控件中如果放入中文字,往往会出现乱码。为了方便使用,我把这个从文档中获取中文字的方法放在一个头文件里面Chinese.h这里的tex_vec是cocos2dx提供的一个保存文档内容的一个容器。这里给出ChineseWords,xml的格式再看看ChineseWord的实现Chinese.cpp就这样,以后在需要用到中文字的地方,就先include这个头文件然后调用ChineseWord函数,获取一串中文字符串。

  3. 利用cocos2dx 3.2开发消灭星星七关于星星的算法

    在前面,我们已经在GameLayer中利用随机数初始化了一个StarMatrix,如果还不知道怎么创建星星矩阵请回去看看而且我们也讲了整个游戏的触摸事件的派发了。

  4. cocos2dx3.x 新手打包APK注意事项!

    这个在编译的时候就可以发现了比较好弄这只是我遇到的,其他的以后遇到再补充吧。。。以前被这两个问题坑了好久

  5. 利用cocos2dx 3.2开发消灭星星八游戏的结束判断与数据控制

    如果你看完之前的,那么你基本已经拥有一个消灭星星游戏的雏形。开始把剩下的两两互不相连的星星消去。那么如何判断是GameOver还是进入下一关呢。。其实游戏数据贯穿整个游戏,包括星星消除的时候要加到获得分数上,消去剩下两两不相连的星星的时候的加分政策等,因此如果前面没有做这一块的,最好回去搞一搞。

  6. 利用cocos2dx 3.2开发消灭星星九为游戏添加一些特效

    needClear是一个flag,当游戏判断不能再继续后,这个flag变为true,开始消除剩下的星星clearSumTime是一个累加器ONE_CLEAR_TIME就是每颗星星消除的时间2.连击加分信息一般消除一次星星都会有连击信息和加多少分的信息。其实这些combo标签就是一张图片,也是通过控制其属性或者runAction来实现。源码ComboEffect.hComboEffect.cpp4.消除星星粒子效果消除星星时,为了实现星星爆裂散落的效果,使用了cocos2d提供的粒子特效引擎对于粒子特效不了

  7. 02 Cocos2D-x引擎win7环境搭建及创建项目

    官网有搭建的文章,直接转载记录。环境搭建:本文介绍如何搭建Cocos2d-x3.2版本的开发环境。项目创建:一、通过命令创建项目前面搭建好环境后,怎样创建自己的Cocos2d-x项目呢?先来看看Cocos2d-x3.2的目录吧这就是Cocos2d-x3.2的目录。输入cocosnew项目名–p包名–lcpp–d路径回车就创建成功了例如:成功后,找到这个项目打开proj.win32目录下的Hello.slnF5成功了。

  8. 利用cocos2dx 3.2开发消灭星星十为游戏添加音效项目源码分享

    一个游戏,声音也是非常的重要,其实cocos2dx里面的简单音效引擎的使用是非常简单的。我这里只不过是用一个类对所有的音效进行管理罢了。Audio.hAudio.cpp好了,本系列教程到此结束,第一次写教程如有不对请见谅或指教,谢谢大家。最后附上整个项目的源代码点击打开链接

  9. 03 Helloworld

    程序都有一个入口点,在C++就是main函数了,打开main.cpp,代码如下:123456789101112131415161718#include"main.h"#include"AppDelegate.h"#include"cocos2d.h"USING_NS_CC;intAPIENTRY_tWinMain{UNREFERENCED_ParaMETER;UNREFERENCED_ParaMETER;//createtheapplicationinstanceAppDelegateapp;return

  10. MenuItemImage*图标菜单创建注意事项

    学习cocos2dx,看的是cocos2d-x3.x手游开发实例详解,这本书错误一大把,本着探索求知勇于发现错误改正错误的精神,我跟着书上的例子一起调试,当学习到场景切换这个小节的时候,出了个错误,卡了我好几个小时。

返回
顶部