COCOS 2d实现通过单点触摸移动场景中的对象。

HelloWorldScene.h代码如下

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
	cocos2d::Sprite *myCard[5];
	int selectedId;
	static cocos2d::Scene* createScene();
	virtual bool init();
	CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp代码如下:

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"

USING_NS_CC;

using namespace cocostudio::timeline;

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();
	// 加入背景
	auto* background = Sprite::create("background.png");
	background->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	this->addChild(background);
	// 加入卡牌精灵
	for (int i = 0; i < 5; i++)
	{
		char imageName[15] = { 0 };
		sprintf(imageName, "mycard0%d.png", i);
		myCard[i] = Sprite::create(imageName);
		myCard[i]->setScale(0.26f);
		myCard[i]->setPosition(visibleSize.width*(i + 1) / 6, visibleSize.height / 2);
		this->addChild(myCard[i]);
	}
	// 建立事件监听器
	auto myListener = EventListenerTouchOneByOne::create();
	myListener->setSwallowTouches(true);
	myListener->onTouchBegan = [](Touch* touch, Event* event)
	{
		// 获取事件所绑定的 target
		auto target = static_cast<Sprite*>(event->getCurrentTarget());
		// 获取当前点击点所在相对按钮的位置坐标
		Point locationInNode = target->convertToNodeSpace(touch->getLocation());
		Size s = target->getContentSize();
		Rect rect = Rect(0, 0, s.width, s.height);
		// 点击范围判断检测
		if (rect.containsPoint(locationInNode))
		{
			// 显示当前卡牌精灵的坐标位置
			log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
			target->setOpacity(180);
			return true;
		}
		return false;
	};
	myListener->onTouchMoved = [](Touch* touch, Event* event)
	{
		auto target = static_cast<Sprite*>(event->getCurrentTarget());
		// 显示当前卡牌精灵的坐标位置
		log("sprite move... x = %f, y = %f", touch->getLocation().x, touch->getLocation().y);
		// 移动当前卡牌精灵的坐标位置
		target->setPosition(target->getPosition() + touch->getDelta());
	};
	myListener->onTouchEnded = [](Touch* touch, Event* event)
	{
		auto target = static_cast<Sprite*>(event->getCurrentTarget());
		target->setOpacity(255);
	};
	// 为事件监听器绑定事件
	_eventDispatcher->addEventListenerWithSceneGraphPriority(myListener, myCard[0]);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(myListener->clone(), myCard[1]);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(myListener->clone(), myCard[2]);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(myListener->clone(), myCard[3]);
	_eventDispatcher->addEventListenerWithSceneGraphPriority(myListener->clone(), myCard[4]);

	return true;
}

运行截图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这辈子秃头是不可能的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值