一、动作
动作是Node的属性,具有传递性,也就是说父节点发生变化,子节点也会发生变化(node和node的子类都可以使用runaction,一个动作只能移动一个目标)
动作类的继承关系Action-》FiniteTimeAction(有限时间动作)-》ActionInterval(延时动作)
-》ActionInstant(瞬时动作)
-》Follow(跟随)
-》Speed(移动速度)
一、 延时动作(动作的执行需要一段时间)
Sprite* s=Sprite::create("CloseNormal.png");
s->setPosition(Vec2(240, 160));
this->addChild(s);
(1)MoveTo和MoveBy(移动)
MoveTo* moveto=MoveTo::create(4, Vec2(100, 100));//第一个参数移动时间,第二个参数移动到的位置
s->runAction(moveto);
MoveBy* moveby=MoveBy::create(4, Vec2(300,300));//第一个参数移动时间,第二个参数移动到的位置
s->runAction(moveby);
(2)RotateTo和RotateBy(旋转)
RotateTo* r=RotateTo::create(3.0, 180);//时间 角度 正方向顺时针 当角度大于180时会减去360 >180-360=360
//需要旋转多圈
s->runAction(r);
RotateBy* ro=RotateBy::create(3.0, 720);
s->runAction(ro);
(3)ScaleTo和ScaleBy(缩)
ScaleTo* sc=ScaleTo::create(2.0, 2);//时间 缩放比例
s->runAction(sc);//在原图基础上放大
ScaleBy* sc1=ScaleBy::create(2.0, 2);
s->runAction(sc1);//在放大后的基础上放大
(4)JumpTo和JumpBy(跳跃)
JumpTo* j=JumpTo::create(2.0, Vec2(300,100), 50, 80);//时间 位置 高度 次数
s->runAction(j);//最终都会停止在这个位置上
JumpBy* ju=JumpBy::create(2.0, Vec2(300,100), 50, 80);
s->runAction(ju);//最终停止位置=初始位置+Vec2(100,100);
(5)Blink(闪烁)
Blink* b=Blink::create(2.0, 3);//时间 次数
s->runAction(b);
(6)reverse
MoveBy* m=MoveBy::create(4, Vec2(100, 100));
s->runAction(m->reverse());
(7)缓冲动作类//缓冲动作
Sprite* sprite=Sprite::create("CloseNormal.png");
sprite->setPosition(Vec2(100,100));
this->addChild(sprite);
//EaseIn <1 开始速度快->慢
// =1与普通的一样
// >1慢->快
MoveTo* moveto=MoveTo::create(2.0, Vec2(500, 300));
EaseIn* easein=EaseIn::create(moveto, 0.5);
sprite->runAction(easein);
//EaseOut>1 开始速度快->慢
// =1与普通的一样
// <1慢->快
MoveTo* moveto=MoveTo::create(2.0, Vec2(500, 300));
EaseOut* easeout=EaseOut::create(moveto, 0.5);
sprite->runAction(easeout);
MoveTo* moveto=MoveTo::create(2.0, Vec2(500, 300));
EaseInOut* easeinout=EaseInOut::create(moveto, 0.5);
sprite->runAction(easeinout);
(8)组合动作类
动作延时DelayTime
序列动作Sequence
MoveBy* m=MoveBy::create(4, Vec2(100, 100));
Blink* b=Blink::create(2.0, 3);
Sequence* se=Sequence::create(m,b, NULL);
s->runAction(se);
同步动作Spawn
MoveBy* m=MoveBy::create(4, Vec2(100, 100));
Blink* b=Blink::create(2.0, 3);
Spawn * se=Spawn::create(m,b, NULL);
s->runAction(se);
重复动作Repeat
MoveBy* m=MoveBy::create(4, Vec2(100, 100));
Repeat* r=Repeat::create(m, 5);
s->runAction(r);
MoveBy* m=MoveBy::create(4, Vec2(100, 100));
RepeatForever* r=RepeatForever::create(m);
s->runAction(r);
改变动作对象
RotateBy* r=RotateBy::create(5, 90);
TargetedAction* t=TargetedAction::create(s1, r);
Sequence* ss=Sequence::create(r,t ,NULL);
s->runAction(ss);
进度条专属动作
//技能冷却效果(下面放亮的)
ProgressTimer* timer = ProgressTimer::create(Sprite::create("HelloWorld.png"));
timer->setPosition(Vec2(720, 160));
timer->setMidpoint(Vec2(0, 0.5));//圆心的位置
timer->setType(ProgressTimer::Type::BAR);
//timer->setType(ProgressTimer::Type::RADIAL);//圆形
timer->setBarChangeRate(Vec2(1, 0));//1 0在初始的时候全部显示
addChild(timer);
ProgressTo* to = ProgressTo::create(5, 100);//延时动作,动作执行时间 0~100表示显示的百分比
timer->runAction(to);
二、瞬时动作(动作的执行不需要时间)
//瞬时动作
//[=](){}=花括号里面可以使用不能修改& 可以使用可以修改this可以使用对象里面的成员
CallFunc* fun=CallFunc::create([](){log("Helloworld");});
//sprite2->runAction(func);
CallFuncN* funcN=CallFuncN::create([](Node* psender){
psender->removeFromParent();
});
Sequence* seq3=Sequence::create(moveto2,funcN ,NULL);
sprite2->runAction(seq3);
CallFuncN* funcN1=CallFuncN::create(CC_CALLBACK_1(HelloWorld::myfunc,this, 10, sprite));
sprite2->runAction(funcN1);
//_1 看需要的参数原型的个数 std:bind C++11
三、速度动作
//加速
Sprite* sprite=Sprite::create("CloseNormal.png");
sprite->setPosition(100, 100);
this->addChild(sprite);
MoveTo* moveto=MoveTo::create(5, Vec2(300, 100));
Speed* speed=Speed::create(moveto, 2);//第一个参数是动作,第二个参数改变动作速度。大于1加速,会改变执行时间
sprite->runAction(speed);
四、跟随动作
Sprite* s=Sprite::create("CloseNormal.png");
s->setPosition(Vec2(240, 160));
this->addChild(s);
Sprite* s1=Sprite::create("HelloWorld.png");
s1->setPosition(Vec2(100, 100));
this->addChild(s1,-1);
//一个层跟着精灵
Follow* follow=Follow::create(s);
this->runAction(follow);
MoveBy* mo=MoveBy::create(5, Vec2(100, 100));
s->runAction(mo);
//follow 层跟随精灵
Sprite* bg=Sprite::create("HelloWorld.png");
bg->setPosition(Vec2(240, 160));
bg->setScale(2);
this->addChild(bg,-1);
Follow* f=Follow::create(sprite);
// Follow* f1=Follow::create(sprite,Rect(240, 0, 480, 320));//第二个参数是设定跟随区域
this->runAction(f);//this 也就是层,跟随sprite
//this->runAction(f1);
13、动画
//图片占内存的计算方式 长*宽*4 r(8位,一个字节)
//不是2的次方时 可以将小图合成大图 不要超过2048 可以节省内存
//对图片进行压缩,去掉透明度,压缩颜色的表现形式
一、纹理(Texture2D)和纹理缓存(TextureCache)
TextureCache* tc=Director::getInstance()->getTextureCache();//得到纹理缓存
Texture2D* tx=tc->addImage("HelloWorld.png”);//将纹理存储到缓存中
addChild(Sprite::createWithTexture(tx));
//异步加载资源
TextureCache::getInstance()->addImageAsync("CloseNormal.png", CC_CALLBACK_1(HelloWorld::addImageFunc,this));
TextureCache::getInstance()->addImageAsync("CloseNormal.png", CC_CALLBACK_1(HelloWorld::addImageFunc,this));
二、精灵帧和精灵帧缓存
三、创建帧动画
(1)将多张散图打成一张大图 (2)使用精灵帧缓存将大图中的小图读取到缓存中
(3)讲多张精灵帧创建为动画帧 (4)将多张动画帧做成动画
(5)将动画转成动作 (6)让精灵执行这个动作
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("jiangshi.plist", "jiangshi.png");
Sprite* s=Sprite::createWithSpriteFrameName("1.png");
s->setPosition(Vec2(240, 160));
this->addChild(s);
Vector<SpriteFrame*>vec;//容器
for (int i=1; i<11; i++) {
__String* str=__String::createWithFormat("%i.png",i);
SpriteFrame* frame=SpriteFrameCache::getInstance()->getSpriteFrameByName(str->getCString());
vec.pushBack(frame);//添加
}
Animation* animation=Animation::createWithSpriteFrames(vec,0.1);//帧间隔
animation->setRestoreOriginalFrame(true);//让他停在最开始的动作上
Animate* animate=Animate::create(animation);
s->runAction(RepeatForever::create(animate));