网上找的一个关于水果忍者划痕的,效果还算凑合。其原理就是基于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();

}

Cocos2d-x 水果忍者划痕效果的更多相关文章

  1. Android上的水果忍者刀锋效果(JAVA实现)

    显示刀锋的View package com.wbhuang.myninjia; import java.util.ArrayList; import java.util.List; import an ...

  2. 前端优秀作品展示,JavaScript 版水果忍者

    <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 Rapha ...

  3. 作品展示,JavaScript 版水果忍者

    点这里 <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版的切水果游戏由百度 JS 小组开发,采用 vml + svg 绘图,使用了 R ...

  4. 基于html5 canvas和js实现的水果忍者网页版

    今天爱编程小编给大家分享一款基于html5 canvas和js实现的水果忍者网页版. <水果忍者>是一款非常受喜欢的手机游戏,刚看到新闻说<水果忍者>四周年新版要上线了.网页版 ...

  5. JavaScript实现的水果忍者游戏,支持鼠标操作

    智能手机刚刚普及时,水果忍者这款小游戏可谓风靡一时.几年过去了,现在,让我们用纯JavaScript来实现这个水果忍者游戏,就算是为了锤炼我们的JavaScript开发技能吧. 大家可以通过这个链接在 ...

  6. L3-012 水果忍者 (30 分)

    2010年风靡全球的“水果忍者”游戏,想必大家肯定都玩过吧?(没玩过也没关系啦~)在游戏当中,画面里会随机地弹射出一系列的水果与炸弹,玩家尽可能砍掉所有的水果而避免砍中炸弹,就可以完成游戏规定的任务. ...

  7. java游戏制作之水果忍者

    水果忍者的原理很简单,主要就是采用随机的方式是画面上面出现水果. package Fruitninja; import java.awt.Dimension; import java.awt.Grap ...

  8. 团体程序设计天梯赛(CCCC) L3012 水果忍者 上凸或下凹的证明

    团体程序设计天梯赛代码.体现代码技巧,比赛技巧.  https://github.com/congmingyige/cccc_code #include <cstdio> #include ...

  9. 团体程序设计天梯赛 L3-012. 水果忍者

    /*对于一条满足条件的直线,向下移,直到触碰一条线段的下端点,仍然经过其它线段,该直线仍然满足条件 即以一条线段的下(上)端点作为直线上的一点,求为了经过一条线段的最小.最大斜率值(mink,maxk ...

随机推荐

  1. int(long) 类型转换为char

    char类型占一个字节,8位 int类型四个字节32位 (long类型的转换跟int类型相同) #include <stdio.h> ]) { buffer[] = (char)tmp; ...

  2. 使用extjs做的一个简单grid

    <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding= ...

  3. centos6.5 使用 rpm 安装 mysql

    从mysql网站下载mysql rpm安装包(包括server.client) 1.安装server rpm -ivh MySQL-server-5.6.19-1.el6.x86_64.rpm 强制安 ...

  4. 进程一些命令pstree,ps,pstack,top

    1. pstree pstree以树结构显示进程$ pstree -p work | grep adsshd(22669)---bash(22670)---ad_preprocess(4551)-+- ...

  5. 20165301 2017-2018-2 《Java程序设计》第三周学习总结

    20165301 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 第四章:类与对象 类: 类的声明:class+类名 类体:成员变量的声明+方法(局部变量+语 ...

  6. iOS开发:用DES对字符串加解密

    参考http://www.cnblogs.com/janken/archive/2012/04/05/2432930.html,做了个小修改,实现PHP,JAVA,Objective-c加解密结果相同 ...

  7. 安卓使用WebView清除缓存

    原文:https://blog.csdn.net/liwei123liwei123/article/details/52624826 Android 清除WebView缓存 最近项目中需要用WebVi ...

  8. [翻译]HLS实践

    最近公司项目没事做,课余实践研究一下技术,算是积累,也可以用到项目里,从零开始记录 HLS:Http Live Streaming 官方文档 https://developer.apple.com/s ...

  9. gym101431B

    以此纪念我都快忘了的后缀自动机(捂脸) 不过这题有两种做法: 用后缀自动机,先把原串再接遍中间加入特殊连接字符再把原串反接两遍,对这个新构造出的串做后缀自动机.(或者直接建立广义后缀自动机) 下面只要 ...

  10. 深度学习应用系列(四)| 使用 TFLite Android构建自己的图像识别App

    深度学习要想落地实践,一个少不了的路径即是朝着智能终端.嵌入式设备等方向发展.但终端设备没有GPU服务器那样的强大性能,那如何使得终端设备应用上深度学习呢? 所幸谷歌已经推出了TFMobile,去年又 ...