栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

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

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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(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(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(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;
}

运行截图:

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/879882.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号