【Cocos2d TestCpp实例模仿一】-- ActionsTest
转载请注明出处:http://blog.csdn.net/oyangyufu/article/details/25252539
CCActionInterval(持续性动作) 位置性变化动作以To结束的, 表示精灵变化位置以绝对坐标为參考,左下角(0, 0);以By结尾,表示精灵精灵变化位置以相对坐标为參考,精灵当前所在位置为(0, 0)
移动动作(位置变化)
一、TestCpp实例ActionsTest 模块中ActionManual动作
素材原图:
动作效果图:
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); 设置猛兽白虎的x,y轴缩放、透明度 CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(1.0f); sp1->setScaleX(1.0f); sp1->setScaleY(-0.5f); sp1->setPosition(ccp(200,visibleSize.height/2)); sp1->setOpacity(150);//透明度 this->addChild(sp1); 设置天使MM的旋转、自身为红色 CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setRotation(300);//旋转0~360 sp2->setColor(ccc3(255,0,0)); sp2->setPosition(ccp(visibleSize.width/2, visibleSize.height/2)); this->addChild(sp2); 设置猛兽灰狼的颜色为蓝色 CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setColor( ccBLUE);//(0,0,255) sp3->setPosition(ccp(visibleSize.width-200, visibleSize.height/2)); this->addChild(sp3); return true; }
二、TestCpp实例ActionsTest 模块中ActionRotate旋转动作
效果图:
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDi rector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); //******************************ActionRotate旋转********************************* CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); //2s内将顺时针旋转45' CCActionInterval *to = CCRotateTo::create(2.0f, 45); CCActionInterval *to0 = CCRotateTo::create(2.0f, 0); //2s内将sp1逆时针旋转45' CCActionInterval *to1 = CCRotateTo::create(2.0f, -45); sp2->runAction(CCSequence::create(to, to1, NULL)); //旋转与回旋360' CCActionInterval *by = CCRotateBy::create(2.0f, 360); sp3->runAction(CCSequence::create(by, by->reverse(), NULL)); //先旋转360',再运行逆时针旋转45'(将top1动作在内存中拷贝一份运行,由于to1动画在sp2也在调用,原则两个精灵运行同一个动画,仅仅有最后面一个精灵调用) sp1->runAction(CCSequence::create(by, to1->copy()->autorelease(), NULL)); return true; }
三、TestCpp实例ActionsTest 模块中ActionScale缩放动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDi rector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); /******************************ActionScale缩放*********************************/ CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); //2s内放大2倍 CCActionInterval *to = CCScaleTo::create(2.0f, 2.0f); //sp1->runAction(to); //2s内在原来基础上xy方向分别放大1倍和3倍 CCActionInterval *by = CCScaleBy::create(2.0f, 1.0f, 3.0f); sp2->runAction(by); //2s内先xy方向放大3倍和1倍,再缩回到原来大小 CCActionInterval *bx = CCScaleBy::create(2.0f, 3.0f, 1.0f); sp3->runAction(CCSequence::create(bx, bx->reverse(), NULL)); return true; }
四、TestCpp实例ActionsTest 模块中ActionMove移动动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDi rector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); /******************************ActionMove移动*********************************/ //创建3怪兽,设置位置 CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); //2s内移动屏幕中心(绝对位置) CCActionInterval *to = CCMoveTo::create(2.0f, ccp(visibleSize.width/2, visibleSize.height/2)); sp2->runAction(to); //2s内移动后再回到原点 CCActionInterval *by = CCMoveBy::create(2.0f, ccp(200, 200));//2s内相对它原来坐标,xy轴再移动200 CCActionInterval *byback = by->reverse();//反向播放动画 sp3->runAction(CCSequence::create(by, byback, NULL));//按序列运行动作,先相对移动,再反向回到原地 sp1->runAction(CCMoveTo::create(1.0f, ccp(100, 100))); return true; }
五、TestCpp实例ActionsTest 模块中ActionJump缩放动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); /******************************ActionJump跳跃*********************************/ //參数1:持续时间;參数2:跳跃到的位置;參数3:跳跃高度;參数4:跳跃次数 CCActionInterval *to = CCJumpTo::create(2.0f, ccp(300, 0), 300, 2); sp1->runAction(to); //跳跃回到原点 CCActionInterval *by = CCJumpBy::create(2.0f, ccp(300, 0), 100, 2); CCActionInterval *backby = by->reverse(); sp2->runAction(CCSequence::create(by, backby, NULL)); //原地无限循环跳跃 CCActionInterval* actionUp = CCJumpBy::create(2, ccp(0,0), 80, 4); sp3->runAction(CCRepeatForever::create(actionUp)); return true; }
六、TestCpp实例ActionsTest 模块中ActionTint色值渐变动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); /******************************ActionTint色值渐变*********************************/ CCActionInterval *ac = CCTintTo::create(4, 255,255, 0);//4s渐变黄色 CCActionInterval *ac1 = CCTintBy::create(4, -255, 0, -255);//在眼下值上加上该色值,4s渐变绿 sp3->runAction(ac); sp2->runAction(CCRepeatForever::create(CCSequence::create(ac1, ac1->reverse(), NULL))); return true; }
七、TestCpp实例ActionsTest 模块中ActionBlink闪烁动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); /******************************ActionBlink闪烁*********************************/ CCActionInterval *ac = CCBlink::create(2, 4);//2s内闪烁4次 CCActionInterval *ac1 = CCBlink::create(5, 4);//5s内闪烁4次 sp2->runAction(ac); sp3->runAction(ac1); return true; }
八、TestCpp实例ActionsTest 模块中ActionFade谈入谈出动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); /******************************ActionFade谈入谈出*********************************/ sp1->setOpacity(0);//设置透明 CCActionInterval *in = CCFadeIn::create(2.0f);//谈入 sp1->runAction(CCSequence::create(in, in->reverse(), NULL)); CCActionInterval *out = CCFadeOut::create(2.0f);//谈出 sp2->runAction(CCSequence::create(out, in->reverse(), NULL)); sp3->setOpacity(100); return true; }
九、TestCpp实例ActionsTest 模块中ActionBezier贝赛尔曲线动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); /******************************ActionBezier贝赛尔曲线*********************************/ //sprite 1 ccBezierConfig bezi;//配置贝赛尔曲线动作 bezi.controlPoint_1 = ccp(0, visibleSize.height/2);//控制点1位置 bezi.controlPoint_2 = ccp(300, -visibleSize.height/2);//控制点2位置 bezi.endPosition = ccp(300, 100); CCActionInterval * to = CCBezierBy::create(3, bezi);//參数1:持续时间;參数2:配置对象 CCAction *ac = CCRepeatForever::create(CCSequence::create(to, to->reverse(), NULL)); sp1->runAction(ac); //sprite 2 ccBezierConfig bezi1; bezi1.controlPoint_1 = ccp(100, visibleSize.height/2); bezi1.controlPoint_2 = ccp(200, -visibleSize.height/2); bezi1.endPosition = ccp(500, 500); CCActionInterval *to1 = CCBezierTo::create(3, bezi1); sp2->runAction(to1); //sprite 3 sp3->setPosition(ccp(400,160)); CCActionInterval *to2 = CCBezierTo::create(3, bezi1); sp3->runAction(to2); return true; }
十、TestCpp实例ActionsTest 模块中ActionJump跳跃动作
程序代码:
bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png", "CloseSelected.png", this, menu_selector(HelloWorld::menuCloseCallback)); pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , origin.y + pCloseItem->getContentSize().height/2)); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); pMenu->setPosition(CCPointZero); this->addChild(pMenu, 1); CCSprite *sp1 = CCSprite::create("cpp1.png"); sp1->setScale(0.5f); sp1->setPosition(ccp(100, 100)); this->addChild(sp1); CCSprite *sp2 = CCSprite::create("cpp2.png"); sp2->setScale(0.5f); sp2->setPosition(ccp(visibleSize.width/2-100, visibleSize.height/2)); this->addChild(sp2); CCSprite *sp3 = CCSprite::create("cpp3.png"); sp3->setScale(0.5f); sp3->setPosition(ccp(visibleSize.width/2+100, visibleSize.height/2)); this->addChild(sp3); /******************************ActionJump跳跃*********************************/ //參数1:持续时间;參数2:跳跃到的位置;參数3:跳跃高度;參数4:跳跃次数 CCActionInterval *to = CCJumpTo::create(2.0f, ccp(300, 0), 300, 2); sp1->runAction(to); //跳跃回到原点 CCActionInterval *by = CCJumpBy::create(2.0f, ccp(300, 0), 100, 2); CCActionInterval *backby = by->reverse(); sp2->runAction(CCSequence::create(by, backby, NULL)); //原地无限循环跳跃 CCActionInterval* actionUp = CCJumpBy::create(2, ccp(0,0), 80, 4); sp3->runAction(CCRepeatForever::create(actionUp)); return true; }