• Cocos2d-x 水果忍者划痕效果


    网上找的一个关于水果忍者划痕的,效果还算凑合。其原理就是基于OpenGL绘制直线,因为版本号过老,此处笔者改动了一些方法,粘贴后可直接使用

    适用于Cocos2d-x 2.2.1

    .h文件里须要添�的代码:

     void draw();

         void drawLine();

         virtual void ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent);

         virtual void ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent);

         virtual void ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent);

          std::list<CCPoint> pointList;


    .cpp文件里

    void HelloWorld::draw()

    {

        drawLine();

    }

    void HelloWorld::drawLine()

    {

        int tickSubCount = 10;

        int pointListKeepCount = 500;

        

        for (int i=0; i<tickSubCount ; i++)

        {

            if (pointList.size() >0)

            {

                pointList.pop_front();

            }

            else

            {

                break;

            }

        }

        while (pointList.size() > pointListKeepCount)

        {

            pointList.pop_front();

        }

        

        float max_lineWidth = 5;

        float min_lineWidth = 1;

        int   alpha_min = 10;

        int   alpha_max =  200;

        

        int  R = arc4random()%255;

        int  G = arc4random()%255;

        int  B = arc4random()%255;

        

        int pointListCount = pointList.size();

        std::list <CCPoint>::iterator it =pointList.begin();

        

     

        

        float pointIndex = 0;

        for(;it!=pointList.end();it++)

        {

            int distanceToMiddle = fabs(pointIndex-pointListCount/2);

            float percent = 1.0-(float)distanceToMiddle/(float)(pointListCount/2.0);

            float lintWidth = min_lineWidth + max_lineWidth*percent;

            int alpha = alpha_min +alpha_max*percent;

            

            ccc4(R,G,B,alpha );

            ccPointSize(lintWidth);

            ccDrawPoint( *it );

            

            pointIndex++;

        }

    }

    void HelloWorld::ccTouchesBegan(CCSet *pTouches,CCEvent *pEvent)

    {

        CCSetIterator it = pTouches->begin();

        CCTouch* touch = (CCTouch*)*it;

        CCPoint beginPoint = touch->getLocationInView();

        beginPoint = CCDirector::sharedDirector()->convertToGL(beginPoint);

        

        pointList.push_back(beginPoint);

    }


    void HelloWorld::ccTouchesMoved(CCSet *pTouches,CCEvent *pEvent)

    {

        CCSetIterator it = pTouches->begin();

        CCTouch* touch = (CCTouch*)*it;

        

        CCPoint nextPoint = touch->getLocationInView( );

        nextPoint = CCDirector::sharedDirector()->convertToGL(nextPoint);

        

        CCPoint preMovePoint = touch->getPreviousLocationInView();

        preMovePoint = CCDirector::sharedDirector()->convertToGL(preMovePoint);

        

        float distance = ccpDistance(nextPoint, preMovePoint);

        if (distance > 1)

        {

            int d = (int)distance;

            for (int i =0; i < d; i++ )

            {

                float distanceX = nextPoint.x - preMovePoint.x;

                float distanceY = nextPoint.y - preMovePoint.y;

                

                float percent = i / distance;

                CCPoint newPoint;

                newPoint.x = preMovePoint.x + (distanceX * percent);

                newPoint.y = preMovePoint.y + (distanceY * percent); 

                

                pointList.push_back(newPoint);

            }

        }

    }

    void HelloWorld::ccTouchesEnded(CCSet *pTouches,CCEvent *pEvent)

    {

        pointList.clear();

    }

  • 相关阅读:
    如何在Root的手机上开启ViewServer,使得HierachyViewer能够连接
    java调用monkeyrunner(亲测绝对可行)
    如何在DOS窗口复制和粘贴命令
    查看当前APP打开的是哪个Activity
    MonkeyRunner之MonkeyRecorder录制回放脚本(亲测可正常运行)
    selenium python2.7安装配置
    常用adb操作命令详解
    Spring整合strus2简单应用总结
    spring整合strus2的Hellowworld
    责任链模式-对象行为型
  • 原文地址:https://www.cnblogs.com/yxwkf/p/3819484.html
Copyright © 2020-2023  润新知