//GameScene.cpp
#include "GameScene.h"
USING_NS_CC;
cocos2d::Scene* GameScene::createScene()
{
auto scene = Scene::create(); //创建一个场景
auto layer = GameScene::create(); //创建一个图层
scene->addChild(layer);
return scene;
}
//初始化当前的图层
bool GameScene::init()
{
if (!Layer::init()) //初始化父类
return false ;
//获取屏幕大小
Size visibleSize = Director::getInstance()->getVisibleSize();
//auto size = Director::getInstance()->getWinSize();
auto sprite1 = Sprite::create( "Icon.png" );
sprite1->setPosition(Vec2(visibleSize.width/2, visibleSize.height*0.8));
this ->addChild(sprite1);
auto sprite2 = Sprite::create( "Icon.png" );
sprite2->setPosition(Vec2(visibleSize.width*0.4, visibleSize.height*0.4));
this ->addChild(sprite2);
auto sprite3 = Sprite::create( "Icon.png" );
sprite3->setPosition(Vec2(visibleSize.width/2, visibleSize.height*0.2));
this ->addChild(sprite3);
//移动
auto moveTo = MoveTo::create(1, Vec2(visibleSize.width*0.8, visibleSize.height*0.8));
auto moveBy = MoveBy::create(1, Point(-visibleSize.width*0.3, 0)); //此处不能用Vec2()形式,否则会报错
//缩放
//auto scaleTo = ScaleTo::create(1, 3); //放大3倍
auto scaleBy = ScaleBy::create(1, 3);
auto rescale = scaleBy->reverse(); //动作逆向执行,不支持ScaleTo动作
sprite1->runAction(RepeatForever::create(Sequence::create(scaleBy, rescale, moveTo, moveBy, NULL))); //Sequence 动作依次执行
//sprite1->runAction(Spawn::create(scaleBy, rescale, moveTo, moveBy, NULL)); //Spawn 动作同时执行
//创建一个容量为5的数组,用来存放曲线样点
auto array = PointArray::create(5);
array->addControlPoint(Vec2(visibleSize.width*0.4, visibleSize.height*0.4));
array->addControlPoint(Vec2(visibleSize.width*0.6, visibleSize.height*0.4));
array->addControlPoint(Vec2(visibleSize.width*0.6, visibleSize.height*0.6));
array->addControlPoint(Vec2(visibleSize.width*0.4, visibleSize.height*0.6));
array->addControlPoint(Vec2(visibleSize.width*0.4, visibleSize.height*0.4));
//样条曲线运动,第三个参数:小于1向外弯曲,大于1向内弯曲
auto action = CardinalSplineTo::create(4, array, 0.1f);
//sprite2->runAction(Repeat::create(action, 5)); //动作重复执行5次
sprite2->runAction(RepeatForever::create(action)); //动作永久执行
//旋转
auto rotateTo = RotateTo::create(2, 30); //顺时针旋转30°
//倾斜
auto skewTo = SkewTo::create(2, 15, 30); //沿X轴倾斜15°,沿Y轴倾斜30°
//闪烁
auto blink = Blink::create(2, 4); //闪烁4次
//透明度变化
auto fadeTo = FadeTo::create(2, 200); //透明度变为100
// auto fadeIn = FadeIn::create(2); //从透明到不透明
// auto fadeOut = FadeOut::create(2); //从不透明到透明
//颜色变化
auto tintTo = TintTo::create(2, 0, 200, 0);
//跳动
auto jumpTo = JumpTo::create(2, Vec2(visibleSize.width*0.8, visibleSize.height*0.2), 30, 5);
sprite3->runAction(Sequence::create(jumpTo, fadeTo, tintTo, blink, rotateTo, skewTo, NULL));
return true ;
}
|