Cocos2d-X 物体重复运动的实现

void Hook::runRotateAction()

{

    this->setAnchorPoint(CCPointMake(0.5, 1));

    CCActionInterval *action1 = CCRotateTo::create(2,-90.0);

    CCActionInterval *action2 = CCRotateTo::create(2, 90.0);

    float rotate = this->getRotation();

    CCSequence *action3;

    if(rotate>0)

    {

        action3 = CCSequence::create(action1,action2,NULL);

    }

    else

    {

        action3 = CCSequence::create(action2,action1,NULL);

    }

    CCRepeatForever *repeat = CCRepeatForever::create(action3);

    this->runAction(repeat);

}


void Hook::runBack()

{

    CCSize winSize = CCDirector::sharedDirector()->getWinSize();

    CCActionInterval *action4 = CCMoveTo::create(2, ccp(winSize.width/2-2, winSize.height/2-20));

    CCCallFunc *func = CCCallFunc::create(this, callfunc_selector(Hook::runRotateAction));

    CCSequence *seq = CCSequence::create(action4,func,NULL);

    this->runAction(seq);

}


void Hook::runToPoint(CCPoint point)

{

    this->stopAllActions();

    CCPoint start =  this->getPosition();

    float distance = sqrtf((start.x-point.x)*(start.x-point.x) +  (start.y-point.y)*(start.y-point.y));

    float time = distance*0.01;

    CCActionInterval *actionTo = CCMoveTo::create(time, point);

    CCCallFunc *fun = CCCallFunc::create(this, callfunc_selector(Hook::runBack));

    CCSequence *seq = CCSequence::create(actionTo,fun,NULL);

    this->runAction(seq);

}


void Hook::runTargetAction()

{

    float x;

    float y;

    if(this->getRotation()>0)

    {

        if(this->getRotation()<=45)

        {

            y=0.0;

            x= 240-160*tan(this->getRotation()*3.1415/180);

        }

        else

        {

            y=160-240/tan(this->getRotation()*3.1415/180);

            x= 0.0;

        }

    }

    else if(this->getRotation()<0)

    {

        if(this->getRotation()<=-45)

        {

            y=160+240/tan(this->getRotation()*3.1415/180);

            x= 480;

        }

        else

        {

            y=0;

            x= 240-160*tan(this->getRotation()*3.1415/180);

        }

    }

    else if(this->getRotation() == 0)

    {

        x= 240;

        y= 160;

    }

    this->runToPoint(ccp(x, y));

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Cocos2d-x实现流光效果可以使用shader来进行渲染。首先,我们需要创建一个自定义的shader,并将其应用于需要实现流光效果的节点上。 在使用Cocos2d-x的过程中,我们可以使用GLSL语言编写shader代码。在实现流光效果的shader中,我们可以通过改变像素的颜色和透明度来创建流动的效果。 首先,我们定义一个uniform变量time,用于控制流光的移动速度。然后,在片段着色器中,通过改变颜色和透明度的计算公式来实现流动的效果。我们可以使用sin函数或者其他数学函数来计算出每个像素点的颜色和透明度,然后将其应用到节点上。 在节点的渲染流程中,我们将这个自定义的shader应用到节点上,然后传入时间参数,即更新uniform变量time的值。随着时间的增加,我们就可以看到节点上的流光效果在不断地移动。 为了实现更加逼真的流光效果,我们可以尝试给流光添加一些额外的效果,比如模糊、叠加等。通过调整shader代码中的计算公式和传入的参数,我们可以根据自己的需求来调整流光效果的强度和样式。 总结起来,在Cocos2d-x实现流光效果需要创建一个自定义的shader,并将其应用于需要实现效果的节点上。通过改变颜色和透明度的计算公式、传入时间参数等,我们可以实现一个流光效果,使节点看起来具有流动的动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值