如何使用Cocos2d-x3.0来给Sprite添加遮罩
推荐于2016-05-19
展开全部
启动terminal,运行"python /Cocos/cocos2d-x-3.0beta2/tools/project-creator/create_project.py"。把工程命名为MaskedCal,然后选择一个文件夹来保存,最后点Create。
接下来,请下载本工程所需要的资源文件并把它们拖到你的Xcode的Resource分组中,确保“Copy items into destination group’s folder (if needed)” 并复选中,然后点Finish。
在开始编码之前,让我们先来一点爵士音乐。同时,由于这里给的图片是480x320的,为了适应各个分辨率,这里需要setDesignResolutionSize,方便在不同的设备上显示。就是告诉游戏引擎,我是针对480x320像素设计的,遇到其他分辨率的设备,劳驾你帮我自动调整。打开AppDelegate.cpp,然后做如下修改:
// Add to top of file
#include "SimpleAudioEngine.h"
//At end of applicationDidFinishLaunching,
//replace last 3 lines with the following 5 lines:
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("TeaRoots.mp3", true);
EGLView::getInstance()->setDesignResolutionSize(480, 320, ResolutionPolicy::NO_BORDER);
auto scene = HelloWorld::sceneWithLastCalendar(0);
director->runWithScene(scene);
return true;
这里播放一个由Kevin MacLeod制作的一首非常好听的曲子,然后调用了一个新的方法来创建场景。
接下来,打开HelloWorldScene.h 并作下面修改:
// Add new instance variable
static int calendarNum;
//replace createScene methond
static cocos2d::Scene* sceneWithLastCalendar(int lastCalendar);
// Add an another create methond
static cocos2d::Layer* layerWithLastCalendar(int lastCalendar);
在这个场景中,我们将随机显示一张日历图片(从三张里面选择)。在这个类里,我们保存了当前显示的日历图片的序号,然后添加了HelloWorld这个layer的另一个create函数同时替换了createScene函数。layerWithLastCalendar接收一个int型参数来标识将要显示的日历图片。后面,你会看到这个数字会随机从1-3中选择。
然后,回到HelloWorldScene.cpp,并且作如下修改:
//place it after USING_NS_CC;
//init static variable
int HelloWorld::calendarNum = 0;
Scene* HelloWorld::sceneWithLastCalendar(int lastCalendar)
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::layerWithLastCalendar(lastCalendar);
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// Add an another create methond
Layer* HelloWorld::layerWithLastCalendar(int lastCalendar)
{
HelloWorld *pRet = new HelloWorld();
if (pRet && pRet->init())
{
pRet->autorelease();
Size winSize = Director::getInstance()->getWinSize();
do {
calendarNum = (arc4random() % 3) + 1;
} while (calendarNum == lastCalendar);
char spriteName[100] = {0};
sprintf(spriteName, "Calendar%d.png", calendarNum);
Sprite * cal = Sprite::create(spriteName);
cal->setPosition(winSize.width/2, winSize.height/2);
pRet->addChild(cal);
return pRet;
}
else
{
delete pRet;
pRet = NULL;
return NULL;
}
}
上面cal精灵设置的坐标就是我们的DesignResolutionSize/2,一旦我们设置了designSize,cocos2d-x中的getWinSize就成了我们的designSize。。
我们在init函数里添加触摸事件响应,同时添加三个回调函数:
// Replace init with the following
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
// Add new methods
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
Scene *scene = HelloWorld::sceneWithLastCalendar(calendarNum);
Director::getInstance()->replaceScene(TransitionJumpZoom::create(1.0f, scene));
return true;
}
void HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
}
void HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
}
接下来,请下载本工程所需要的资源文件并把它们拖到你的Xcode的Resource分组中,确保“Copy items into destination group’s folder (if needed)” 并复选中,然后点Finish。
在开始编码之前,让我们先来一点爵士音乐。同时,由于这里给的图片是480x320的,为了适应各个分辨率,这里需要setDesignResolutionSize,方便在不同的设备上显示。就是告诉游戏引擎,我是针对480x320像素设计的,遇到其他分辨率的设备,劳驾你帮我自动调整。打开AppDelegate.cpp,然后做如下修改:
// Add to top of file
#include "SimpleAudioEngine.h"
//At end of applicationDidFinishLaunching,
//replace last 3 lines with the following 5 lines:
CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("TeaRoots.mp3", true);
EGLView::getInstance()->setDesignResolutionSize(480, 320, ResolutionPolicy::NO_BORDER);
auto scene = HelloWorld::sceneWithLastCalendar(0);
director->runWithScene(scene);
return true;
这里播放一个由Kevin MacLeod制作的一首非常好听的曲子,然后调用了一个新的方法来创建场景。
接下来,打开HelloWorldScene.h 并作下面修改:
// Add new instance variable
static int calendarNum;
//replace createScene methond
static cocos2d::Scene* sceneWithLastCalendar(int lastCalendar);
// Add an another create methond
static cocos2d::Layer* layerWithLastCalendar(int lastCalendar);
在这个场景中,我们将随机显示一张日历图片(从三张里面选择)。在这个类里,我们保存了当前显示的日历图片的序号,然后添加了HelloWorld这个layer的另一个create函数同时替换了createScene函数。layerWithLastCalendar接收一个int型参数来标识将要显示的日历图片。后面,你会看到这个数字会随机从1-3中选择。
然后,回到HelloWorldScene.cpp,并且作如下修改:
//place it after USING_NS_CC;
//init static variable
int HelloWorld::calendarNum = 0;
Scene* HelloWorld::sceneWithLastCalendar(int lastCalendar)
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::layerWithLastCalendar(lastCalendar);
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// Add an another create methond
Layer* HelloWorld::layerWithLastCalendar(int lastCalendar)
{
HelloWorld *pRet = new HelloWorld();
if (pRet && pRet->init())
{
pRet->autorelease();
Size winSize = Director::getInstance()->getWinSize();
do {
calendarNum = (arc4random() % 3) + 1;
} while (calendarNum == lastCalendar);
char spriteName[100] = {0};
sprintf(spriteName, "Calendar%d.png", calendarNum);
Sprite * cal = Sprite::create(spriteName);
cal->setPosition(winSize.width/2, winSize.height/2);
pRet->addChild(cal);
return pRet;
}
else
{
delete pRet;
pRet = NULL;
return NULL;
}
}
上面cal精灵设置的坐标就是我们的DesignResolutionSize/2,一旦我们设置了designSize,cocos2d-x中的getWinSize就成了我们的designSize。。
我们在init函数里添加触摸事件响应,同时添加三个回调函数:
// Replace init with the following
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
// Add new methods
bool HelloWorld::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
Scene *scene = HelloWorld::sceneWithLastCalendar(calendarNum);
Director::getInstance()->replaceScene(TransitionJumpZoom::create(1.0f, scene));
return true;
}
void HelloWorld::onTouchMoved(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
}
void HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *unused_event)
{
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询