1.参考链接汇总
官方接口文档
关于Cocos2d-x中addchild和removeChild方法的参数的解析
Cocos2d-x3.2 Menu菜单的创建
cocos 中熟练运用场景的切换
cocos2dx一个场景添加多个层
Cocos2d-x Layer锚点设置
cocos2d-x convertToWorldSpace 和 convertToNodeSpace
cocos2d-x 延迟执行一段代码1 顺序执行动作+延迟动作+CallFunc
Cocos2d-x v3.6制作射箭游戏(三)
菜鸟学习Cocos2d-x 3.x——浅谈动画
cocos2d-x“无法打开源文件”
cocos2d-x 坐标研究
深入理解 cocos2d-x 坐标系
Cocos2d-x解析XML文件,解决中文乱码
2.创建Sprite
auto bg = Sprite::create("level-background-0.jpg");
bg->setPosition(Vec2(visibleSize.width / 2 + origin.x,visibleSize.height / 2 + origin.y));
this->addChild(bg,0);
使用plist:
auto spritecache = SpriteFrameCache::getInstance();
spritecache->addSpriteFramesWithFile("level-sheet.plist");
mouse = Sprite::createWithSpriteFrameName("gem-mouse-0.png");
mouse->setPosition(Vec2(origin.x + visibleSize.width / 2,0));
3.创建MenuItem
auto closeItem = MenuItemImage::create(
"CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2));
auto menu = Menu::create(closeItem,NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu,1);
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
4.创建Layer
stoneLayer = Layer::create();
stoneLayer->setPosition(Vec2(0,0));
stoneLayer->setAnchorPoint(Vec2(0,0));
stoneLayer->ignoreAnchorPointForPosition(false);
stoneLayer->addChild(stone);
mouseLayer = Layer::create();
mouseLayer->setPosition(Vec2(0,origin.y + visibleSize.height / 2));
mouseLayer->setAnchorPoint(Vec2(0,0));
mouseLayer->ignoreAnchorPointForPosition(false);
mouseLayer->addChild(mouse);
this->addChild(stoneLayer,1);
this->addChild(mouseLayer,1);
5.显示中文字符
CCDictionary* message = CCDictionary::createWithContentsOfFile("Chinese.xml");
auto nameKey = message->valueForKey("Name");
const char* Name = nameKey->getCString();
auto IDKey = message->valueForKey("ID");
const char* ID = IDKey->getCString();
auto label = Label::createWithTTF(Name,"fonts/FZSTK.ttf",24);
auto label1 = Label::createWithTTF(ID,"fonts/Marker Felt.ttf",24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,origin.y + visibleSize.height - label->getContentSize().height));
label1->setPosition(Vec2(origin.x + visibleSize.width / 2,origin.y + visibleSize.height
- label->getContentSize().height-label1->getContentSize().height));
<?xml version="1.0" encoding="UTF-8"?>
<dict>
<key>Name</key>
<string>名字</string>
<key>ID</key>
<string>学号</string>
</dict>
6. 播放背景英语
#include <SimpleAudioEngine.h>
#define MUSIC_FILE "666.mp3"
以下是menuItem的触发函数,点击一下播放音乐,再点击一下停止音乐
void HelloWorld::display_music(Ref* pSender) {
CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadBackgroundMusic(MUSIC_FILE);
CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(0.5);
if (count % 2 == 0)
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic(MUSIC_FILE,true);
else
CocosDenshion::SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
count++;
}
7.定义动画(使用plist)
在AppDelegate.cpp中,
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("level-sheet.plist");
char _totalFrames = 7;
char _frameName[40];
Animation* diamondAnimation = Animation::create();
for (int i = 0; i < _totalFrames; i++) {
sprintf(_frameName,"pulled-diamond-%d.png",i);
diamondAnimation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(_frameName)); } diamondAnimation->setDelayPerUnit(0.1);
AnimationCache::getInstance()->addAnimation(diamondAnimation,"diamondAnimation");
在GameSence.cpp中,
Animate* diamondAnimate = Animate::create(AnimationCache::getInstance()->getAnimation("diamondAnimation")); diamond->runAction(RepeatForever::create(diamondAnimate));
8.触摸监听器
EventListenerTouchOneByOne* listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(GameSence::onTouchBegan,this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
bool GameSence::onTouchBegan(Touch *touch,Event *unused_event) {}
9.获得visibleSize与origin
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
10.设置锚点
mouseLayer->setAnchorPoint(Vec2(0,0));
mouseLayer->ignoreAnchorPointForPosition(false);
11.MoveTo使用
auto moveTo = MoveTo::create(2,mouseLayer->convertToNodeSpace(location));
mouse->runAction(moveTo);
12.世界坐标系与本地坐标系的转换
auto location = mouse->getPosition();
location = mouseLayer->convertToWorldSpace(location);
auto moveTo = MoveTo::create(1,stoneLayer->convertToNodeSpace(location));
stone->runAction(moveTo);
13.延迟函数的实现(异步操作)
auto _delayTime = DelayTime::create(0.5);
auto _func = CallFunc::create([this]() {
stoneLayer->removeChild(stone);
stone = Sprite::create("stone.png");
stone->setPosition(Vec2(560,480));
stoneLayer->addChild(stone);
});
auto _seq = Sequence::create(_delayTime,_func,NULL);
this->runAction(_seq);