Cocos2d学习 第5天

1.Log一直打印cocos2d: removeAction: Target not found 
解决方法:找的这个函数CCActionManager::removeAction(CCAction *pAction)
        注释掉
2.精灵执行完动作立刻销毁的方法
回调时函数Sprite::removeFromParent->对应的target是自身精灵,不应该填this.

正确调用:crush->runAction(Sequence::create(action1,CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent, crush)), NULL));
3.重复执行动作  参数:(动作,次数);
    Repeat::create(action,3);
    RepeatForever:create(action);
    auto move1 = MoveBy::create(0.1,Vec2(10, 0));
    auto seqq = Sequence::create(move1,move1->reverse(), NULL);
    auto r1 = RotateTo::create(0.01, 10);
    auto r2 = RotateTo::create(0.01, -10);
    auto seqqq = Sequence::create(r1,r2, NULL);
    auto repeat = Repeat::create(seqqq, 10);
4.RepeatForever 无限次重复动作
  auto sp = Sprite::create("LunTai2.png");
    sp->setPosition(Vec2(665, 375));
    this->addChild(sp);
    auto spLun = RotateBy::create(0.1, 720);
    auto repeatForever = RepeatForever::create(spLun);
5.延时动作(空动作) DelayTime 一般与序列动作一起作用 参数(时间)
  auto delay = DelayTime::create(3.0);
6.改变动作执行的对象 TargetedAction 参数(改变后动作的执行者,动作)
  auto seqWithTar = Sequence::create(MoveBy::create(1.0, Vec2(400, 0)),delay,TargetedAction::create(sseq, RotateBy::create(2.0, 1080)), NULL);
7.过程动作(进度条) progressTo 和 ProgressFromTo
  //类型为圆形
    auto pro1 = ProgressFromTo::create(4.0, 0, 100);
    auto timr1 = ProgressTimer::create(Sprite::create("LunTai.png"));
    timr1->setType(cocos2d::ProgressTimer::Type::RADIAL);
    timr1->setPosition(Vec2(1200, 375));
    addChild(timr1);
    auto rep = RepeatForever::create(pro1);
    timr1->runAction(rep);

  //类型为条形
    auto  pro2 = ProgressFromTo::create(3.0, 0, 100);
    auto timer2 = ProgressTimer::create(Sprite::create("LunTai2.png"));
    timer2->setType(cocos2d::ProgressTimer::Type::BAR);
    timer2->setMidpoint(Vec2(0, 0));
    timer2->setBarChangeRate(Vec2(1, 0));
    timer2->setPosition(Vec2(667, 125));
    addChild(timer2);
    timer2->runAction(pro2);
8.瞬时动作(CallFunc)参数(std::Function)
   auto seq = Sequence::create(MoveBy::create(2.0, Vec2(800, 0)),RotateBy::create(2.0, 1080),CallFunc::create(CC_CALLBACK_0(ActionTest::remove, this)), NULL);

   //CallFunN
   auto callN = CallFuncN::create(CC_CALLBACK_1(ActionTest::callN_func, this));
    auto seqq = Sequence::create(MoveBy::create(2.0, Vec2(800, 0)),RotateBy::create(2.0, 1080),callN, NULL);

9.Hide(会让动作执行者隐藏) Show(会让动作执行者显示)
  auto seqqq = Sequence::create(Hide::create(),DelayTime::create(1.0),Show::create(),DelayTime::create(1.0), NULL);
    auto repeat = RepeatForever::create(seqqq);
    sprite->runAction(repeat);
10.水平翻转 和 垂直翻转
   sprite->runAction(FlipX::create(true));//水平
    sprite->runAction(FlipY::create(true));//垂直
    setScaleX(-1);//也可以实现翻转
11.改变速度动作 Speed
   auto moveBy = MoveBy::create(2.0, Vec2(800, 0));
    auto speed = Speed::create(moveBy, 5.0);
    sprite->runAction(speed);

12.Follow 参数(被跟随的节点,跟随范围)
    auto bg1 = Sprite::create("HelloWorld.png");
    bg1->setPosition(667, 375);
    addChild(bg1,-1);
    
    auto bg2 = Sprite::create("HelloWorld.png");
    bg2->setPosition(Vec2(667+1334, 375));
    addChild(bg2,-1);
    
   auto follow = Follow::create(sprite,Rect(0, 0, 0, 0));
   auto move_follow = MoveBy::create(3.0, Vec2(1334, 0));
   sprite->runAction(move_follow);
   this->runAction(follow);

13.TextureCache 和 Texture2D使用
   //获取(纹理缓存)TextureCache实例 创建速度快  也为单例
    TextureCache * tex = Director::getInstance()->getTextureCache();
    tex->addImage("Hero.png");//将图片纹理加载到纹理缓存中
    Texture2D * texture = tex->getTextureForKey("Hero.png");//从纹理缓存里获取纹理
    Sprite * spp = Sprite::createWithTexture(texture);//通过纹理来创建精灵
    spp->setPosition(Vec2(1200, 700));
    addChild(spp);
    TextureCache::getInstance()->addImage("Hero.png");
14.//SpriteBatchNode 批量渲染节点   在2.0中批量渲染可以节省资源,使帧率较高。在3.0中不推荐使用,优化效果不明显。

15.Cocos2d 中常用的动画有两种:  (1)序列帧动画  2.骨骼动画
(1)//1.获取精灵帧缓存
    SpriteFrameCache * cache = SpriteFrameCache::getInstance();
    //2.将精灵帧加载到精灵缓存中
    cache->addSpriteFramesWithFile("Robot.plist", "Robot.png");
    //3.创建一个保存精灵帧的容器
    Vector<SpriteFrame*> vec;
    //4.创建一个存放图片名字(精灵帧名字)的数组
    char str[20];
    //5.运用循环将图片名字存入数组


   
    for (int i =1; i<6; i++) {
        sprintf(str, "0%d.png",i);
        //6.通过精灵帧的名字从缓存中获取对应的精灵帧对象
        SpriteFrame * frame = cache->getSpriteFrameByName(str);
        //7.将精灵帧加载到容器中
        vec.pushBack(frame);
    }
    //8.通过容器创建动画
    Animation * animation = Animation::createWithSpriteFrames(vec);
    //9.设置帧间隔
    animation->setDelayPerUnit(0.1);
    //10.通过动画创建动作
    Animate * animate = Animate::create(animation);
    //11.设置动作循环
    RepeatForever * rep = RepeatForever::create(animate);
    //12.通过精灵帧创建精灵
    Sprite * sprite = Sprite::createWithSpriteFrameName("
01.png");
    sprite->setPosition(Vec2(667+200, 375-100));
    addChild(sprite);
    sprite->runAction(rep);

(2)
    auto cache =  SpriteFrameCache::getInstance();//获取精灵帧缓存
    cache->addSpriteFramesWithFile("People.plist", "People.png");//精灵帧加载到精灵帧缓存中
    Vector<AnimationFrame*>vec;//创建动画帧容器
    char str[20];//用于存放图片名
    for (int i = 0; i<14; i++)
    {
                if (i<10) {
                    sprintf(str, "000%d.png",i);
                }
                else
                {
                    sprintf(str, "00%d.png",i);
                }
        SpriteFrame * frame = cache->getSpriteFrameByName(str);
        AnimationFrame * aFrame = AnimationFrame::create(frame, 0.2, ValueMap());
        vec.pushBack(aFrame);
    }
    Animation * animation = Animation::create(vec, 0.2);
    Animate * animate = Animate::create(animation);
    
    Sprite * ani_sp = Sprite::createWithSpriteFrameName("0000.png");//通过精灵帧创建精灵(精灵帧可以通过文件名从缓存中读取出来)
        ani_sp->setPosition(Vec2(200,200));
        addChild(ani_sp);
     RepeatForever * rep = RepeatForever::create(animate);
        ani_sp->runAction(rep);


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d-JS开发之旅 从HTML 5到原生手机游戏》 《Cocos2d-JS开发之旅——从HTML 5到原生手机游戏》从简单到复杂逐渐深入介绍Cocos2d-JS,包括HTML5和手机原生游戏两个方面的内容。这些内容融汇了作者多年的工作经验和Cocos2d-JS 的亲身使用教训,有助于读者快速掌握游戏开发的方法和避开不必要的麻烦。 《Cocos2d-JS开发之旅——从HTML 5到原生手机游戏》以两个游戏为线索,每一章的学习都为最终实现游戏而准备。除了基础的Cocos2d-JS使用方法,本书还会探讨如何调试发布原生手机游戏和另外一些高级话题。 《Cocos2d-JS开发之旅——从HTML 5到原生手机游戏》适合所有对2D 游戏开发感兴趣的人群,尤其是计算机专业学生、Flash/JS 开发者,因为Cocos2d-JS简单易懂,读者会发现自己已有的基础能快速应用或转移到Cocos2d-JS 游戏的开发中。 目录 第一部分 准 备 篇 第1 章 Cocos2d-JS 介绍 / 2 第2 章 跑起我们的HelloWorld / 10 第一部分总结 / 27 第二部分 做一个简单的小游戏 第3 章 Cocos2d-JS 的平面世界 / 30 第4 章 让世界来点动静 / 51 第5 章 让玩家操纵这个世界 / 72 第6 章 控制小怪按时出现——定时器 / 84 第7 章 游戏界面 / 96 第8 章 不能光说不练——小小碰碰糖 / 122 第9 章 把成果分享到网上 / 143 第二部分总结 / 158 第三部分 再做一个高大上游戏 第10 章 走向高大上的必经之路——简单的性能优化 / 160 第11 章 让主角不再死板 / 173 第12 章 动态的背景 / 188 第13 章 界面的文字有点丑 / 204 第14 章 超炫的效果——粒子系统 / 211 第15 章 尝试做一个更大的游戏——Hungry Hero(上篇)/ 226 第16 章 尝试做一个更大的游戏——Hungry Hero(下篇)/ 235 第三部分总结 / 291 第四部分 把两个游戏做成原生手机游戏 第17 章 咱们也来做APP / 294 第18 章 真是这么美好吗?更多问题来了 / 304 第四部分总结 / 320 第五部分 高 级 篇 第19 章 订阅者模式——事件机制 / 322 第20 章 屏幕尺寸适配哪家强 / 331 第21 章 让死去的主角灰掉——渲染控制 / 342 第22 章 动态热更新 / 363 第23 章 想说的还有很多 / 374 第五部分总结 / 376

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值