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 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");//精灵帧加载到精灵帧缓存中
    Vectorvec;//创建动画帧容器
    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);


 

你可能感兴趣的:(COCOS2D)