Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos2d-3.x_触摸事件(单点触摸和多点触摸)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<pre class="cpp" name="code">#ifndef __HelLOWORLD_SCENE_H__
#define __HelLOWORLD_SCENE_H__

#include "cocos2d.h"

USING_NS_Cc;


class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    CREATE_FUNC(HelloWorld);

private:
	LabelTTF *pLabel;
};

#endif // __HelLOWORLD_SCENE_H__
 
#include "HelloWorldScene.h"

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::geTinstance()->getVisibleSize();
    Vec2 origin = Director::geTinstance()->getVisibLeorigin();

	pLabel = LabelTTF::create("HelloWorld","fonts/Arial.ttf",30);
	pLabel->setPosition(visibleSize.width / 2.0,visibleSize.height / 2.0);
	this->addChild(pLabel);

	auto listener = EventListenerTouchOneByOne::create();
	listener->onTouchBegan = [](Touch *t,Event *E)
	{
		if (e->getCurrentTarget()->getBoundingBox().containsPoint(t->getLOCATIOn()))
		{
			log("onTouchBegan");
		}
		// return true;   返回真,可以让触摸回调传递到onTouchMoved和onTouchEnded
		// return false;   返回假,不可以让触摸回调传递到onTouchMoved和onTouchEnded
		return false;
	};

	listener->onTouchMoved = [](Touch *t,Event *E)
	{
		log("onTouchMoved");
	};

	listener->onTouchEnded = [](Touch *t,Event *E)
	{
		log("onTouchEnded");
	};

	Director::geTinstance()->getEventDispatcher()->addEventListenerWithSceneGraphpriority(listener,pLabel);

    return true;
}

#ifndef __HelLOWORLD_SCENE_H__
#define __HelLOWORLD_SCENE_H__

#include "cocos2d.h"

USING_NS_Cc;


class HelloWorld : public cocos2d::Layer
{
public:
    static cocos2d::Scene* createScene();

    virtual bool init();

    CREATE_FUNC(HelloWorld);

private:
	LabelTTF *pLabel;
};

#endif // __HelLOWORLD_SCENE_H__
#include "HelloWorldScene.h"

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);

    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    
    Size visibleSize = Director::geTinstance()->getVisibleSize();
    Vec2 origin = Director::geTinstance()->getVisibLeorigin();

	pLabel = LabelTTF::create("HelloWorld",visibleSize.height / 2.0);
	this->addChild(pLabel);

	// 开启多点触摸
	this->setTouchEnabled(true);

	auto listener = EventListenerTouchAllAtOnce::create();
	listener->onTouchesBegan = [](std::vector<Touch *> ts,Event *E)
	{
		log("onTouchesBegan");
	};

	listener->onTouchesMoved = [](std::vector<Touch *> ts,Event *E)
	{
		log("touch is moved,touch's count is %d",ts.size());
	};

	listener->onTouchesEnded = [](std::vector<Touch *> ts,Event *E)
	{
		log("onTouchesEnded");
	};

	Director::geTinstance()->getEventDispatcher()->addEventListenerWithSceneGraphpriority(listener,this);

    return true;
}

大佬总结

以上是大佬教程为你收集整理的Cocos2d-3.x_触摸事件(单点触摸和多点触摸)全部内容,希望文章能够帮你解决Cocos2d-3.x_触摸事件(单点触摸和多点触摸)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。