Cocos2d-x:学习笔记(2017.05.01更新)
发布时间:2020-12-14 17:20:54 所属栏目:百科 来源:网络整理
导读:1.参考链接汇总 官方接口文档 关于Cocos2d-x中addchild和removeChild方法的参数的解析 Cocos2d-x3.2 Menu菜单的创建 cocos 中熟练运用场景的切换 cocos2dx一个场景添加多个层 Cocos2d-x Layer锚点设置 cocos2d-x convertToWorldSpace 和 convertToNodeSpace
1.参考链接汇总官方接口文档 2.创建Spriteauto 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.创建MenuItemauto 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.创建LayerstoneLayer = 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与originSize 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);
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |