#include "HelloWorldScene.h"
USING_NS_CC;
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()
{
if (!Layer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
//添加背景
CCSprite* bk = CCSprite::create("bk.png");
bk->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(bk);
//cardFlag为偶数时,表示牌翻开,否则盖上
cardFlag = 0;
/* 创建第一个精灵 */
sprite1 = Sprite::create("card.jpg");
sprite1->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sprite1);
/* 创建第二个精灵 */
sprite2 = Sprite::create("bkCard.png");
sprite2->setPosition(Point(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sprite2);
/* 第二个精灵默认x拉伸至0.0,于是第二个精灵在一开始是看不见的*/
sprite2->setScaleX(0.0f);
//触摸事件
EventListenerTouchOneByOne* event = EventListenerTouchOneByOne::create();
event->onTouchBegan = [](Touch* touch, Event* event){
auto target = static_cast<Sprite*>(event->getCurrentTarget());
Point point = Director::getInstance()->convertToGL(touch->getLocationInView());
if (target->getBoundingBox().containsPoint(point))
{
return true;
}
else
{
return false;
}
};
event->onTouchEnded = [=](Touch* touch, Event* event){
/* cardFlag为偶数则盖住卡牌,基数则打开卡牌 */
if (0 == cardFlag % 2)
{
closeCard();
}
else
{
openCard();
}
cardFlag++;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(event, sprite1);
_eventDispatcher->addEventListenerWithSceneGraphPriority(event->clone(), sprite2);
return true;
}
void HelloWorld::closeCard()
{
/* x方向拉伸至0的动作 */
ScaleTo* scaleToHide = ScaleTo::create(0.6f, 0.0f, 1.0f);
/* 创建卡牌放大的回调函数 */
auto funcScaleToShow = [=](){
ScaleTo* scaleToShow = ScaleTo::create(0.6f, 1.0f, 1.0f);
sprite2->runAction(scaleToShow);
};
/* 将回调函数封装为动作 */
CallFunc* callFuncSpr2 = CallFunc::create(funcScaleToShow);
/* 依次执行动作 */
Sequence* sequence = Sequence::create(scaleToHide, callFuncSpr2, NULL);
sprite1->runAction(sequence);
}
void HelloWorld::openCard()
{
/* x方向拉伸至0的动作 */
ScaleTo* scaleToHide = ScaleTo::create(0.6f, 0.0f, 1.0f);
auto funcScaleToShow = [=](){
ScaleTo* scaleToShow = ScaleTo::create(0.6f, 1.0f, 1.0f);
sprite1->runAction(scaleToShow);
};
CallFunc* callFuncSpr1 = CallFunc::create(funcScaleToShow);
Sequence* sequence = Sequence::create(scaleToHide, callFuncSpr1, NULL);
sprite2->runAction(sequence);
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Cocos2D游戏之旅(三):卡牌翻转效果的实现(只有当鼠标点中卡牌才翻转).rar (36个子文件)
Resources
bkCard.png 166KB
bk.png 489KB
card.jpg 40KB
sprite.png 19KB
proj.win32
Chapter5_1_BlinkAction.vcxproj.filters 1KB
build-cfg.json 45B
res
game.ico 47KB
Chapter5_1_BlinkAction.sln 3KB
game.rc 2KB
Debug.win32
vc120.pdb 15MB
Chapter5.DE329DCC.tlog
CL.read.1.tlog 169KB
rc.write.1.tlog 370B
rc.read.1.tlog 3KB
cl.write.1.tlog 2KB
cl.command.1.tlog 11KB
link.write.1.tlog 2KB
Chapter5_1_BlinkAction.lastbuildstate 186B
link.command.1.tlog 4KB
rc.command.1.tlog 632B
link.read.1.tlog 8KB
AppDelegate.obj 461KB
main.obj 433KB
Chapter5_1_BlinkAction.log 17KB
game.res 47KB
vc120.idb 1003KB
HelloWorldScene.obj 761KB
Chapter5_1_BlinkAction.vcxprojResolveAssemblyReference.cache 917B
resource.h 490B
main.cpp 451B
main.h 267B
Chapter5_1_BlinkAction.vcxproj.user 635B
Chapter5_1_BlinkAction.vcxproj 10KB
Classes
AppDelegate.cpp 1KB
HelloWorldScene.h 672B
AppDelegate.h 947B
HelloWorldScene.cpp 3KB
共 36 条
- 1
资源评论
- o_lei2017-06-28例子很不错
晓石头
- 粉丝: 34
- 资源: 38
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功