cocos2d-x树结构执行动作

本文介绍如何在Cocos2d-x中使用子节点,并为它们添加旋转和缩放动画。通过设置父节点的锚点并应用无限重复的动作序列,可以实现子节点跟随父节点一起运动的效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

void Test2::onEnter()
{
    TestCocosNodeDemo::onEnter();

    CCSize s = CCDirector::sharedDirector()->getWinSize();
    
    CCSprite *sp1 = CCSprite::create(s_pPathSister1);
    CCSprite *sp2 = CCSprite::create(s_pPathSister2);
    CCSprite *sp3 = CCSprite::create(s_pPathSister1);
    CCSprite *sp4 = CCSprite::create(s_pPathSister2);
    
    sp1->setPosition(ccp(100, s.height /2 ));
    sp2->setPosition(ccp(380, s.height /2 ));
    addChild(sp1);
    addChild(sp2);
    
    sp3->setScale(0.25f);
    sp4->setScale(0.25f);
    //把sp1和sp2当做父节点
    sp1->addChild(sp3);
    sp2->addChild(sp4);
    
    CCActionInterval* a1 = CCRotateBy::create(2, 360);
    CCActionInterval* a2 = CCScaleBy::create(2, 2);
    
    CCAction* action1 = CCRepeatForever::create(
                                                    (CCActionInterval*)(CCSequence::create(a1, a2, a2->reverse(), NULL))
                                                );
    CCAction* action2 = CCRepeatForever::create(
                                                    (CCActionInterval*)(CCSequence::create(
                                                                                        (CCActionInterval*)(a1->copy()->autorelease()), 
                                                                                        (CCActionInterval*)(a2->copy()->autorelease()), 
                                                                                        a2->reverse(), 
                                                                                        NULL) )
                                                );
    
    sp2->setAnchorPoint(ccp(0,0));
    //直接对父节点执行动作,子节点也会跟着执行
    sp1->runAction(action1);
    sp2->runAction(action2);
}

 

转载于:https://www.cnblogs.com/newlist/p/3215179.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值