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. cocos2dx之飞机大战简单版1

    先说下版本vs2010+cocos2dx2.2本章主要是告诉大家如何实现创建背景、飞机、***精灵,并且然后他们动起来,然后做一个碰撞测试,当***和敌方飞机碰撞时就销毁精灵并且加一个爆炸的精灵。所以我们只需要在GameLogic中调用addMonster,在GameLogicaddBullet1中调用addBullet1就可以了注意一定要在参数中加上floatdt,它是表明多少秒调用一次这个函数。

  6. cocos2d: fullPathForFilename: No file found at /cc_2x2_white_image. Possible missing file.

    程序运行的时候输出这条信息cocos2d:fullPathForFilename:Nofilefoundat/cc_2x2_white_image.Possiblemissingfile.并没有影响程序正常运行产生的原因可能是某个精灵未配置纹理导致的,不需要关心。参考cc_2x2_white_imageismissing,givingwarningatruntime:http://www.coco

  7. Cocos2dx学习笔记3---LittleRunner

    4.创建场景类5启动6.不要在写代码的时候,把所有的功能都往一个类里面写,多分开几个类来写,多写一些注释,以便后面要修改或者查阅看不懂。

  8. 寒風的Cocos2dx之旅之添加按钮

    要在Cocos2d中使用Button,首先引入头文件#include"ui/CocosGUI.h"usingnamespaceuil;引入完毕后,就创建button。功能要比MenuItem要强大的多。他可以使按钮放大等功能。

  9. cocos2d实现CCLabelTTF真正字体描边效果

    在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。

  10. cocos2d刚体会被穿透

    在调试Coco2d-javaEditor的BoxShape刚体碰撞时,发现有时候没有碰撞效果,直接穿透过去了。后来发现,刚体碰撞必须在没有外在干扰下才正常,而我每次碰撞的时候,都执行了Action动作,这个action动作会优先于碰撞,所以,会先执行action,这个action导致碰撞无效。为了防止穿透,最好不要用action。

随机推荐

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

返回
顶部