【2014-02-19】cocos2dx学习笔记:水果忍者划痕效果 [历史归档]本文原发布于 cstriker1407.info 个人博客内容为历史存档仅供参考。发布时间2014-02-19 标题cocos2dx学习笔记水果忍者划痕效果分类编程 / C C / cocos2dx 标签cocos2dx·划痕cocos2dx学习笔记水果忍者划痕效果代码备份.h文件.m文件分析最近游戏中用到了划痕效果就在网上google了一下好多资源啊。作者找了一个感觉还不错。在这里备份下。参考【 http://blog.csdn.net/u012945598/article/details/17609281 】。代码备份.h文件#ifndef__RAZER_LAYER_H__#define__RAZER_LAYER_H__#includecocos2d.hclassRazerLayer:publiccocos2d::CCLayer{public:conststaticintTag139269181913;CREATE_FUNC(RazerLayer);virtualboolinit();voiddraw();voiddrawLine();virtualvoidccTouchesBegan(cocos2d::CCSet*pTouches,cocos2d::CCEvent*pEvent);virtualvoidccTouchesMoved(cocos2d::CCSet*pTouches,cocos2d::CCEvent*pEvent);virtualvoidccTouchesEnded(cocos2d::CCSet*pTouches,cocos2d::CCEvent*pEvent);std::listcocos2d::CCPointpointList;};#endif// __RAZER_LAYER_H__.m文件#includeRazerLayer.hUSING_NS_CC;boolRazerLayer::init(){if(!CCLayer::init()){returnfalse;}setTouchEnabled(true);returntrue;}voidRazerLayer::draw(){drawLine();}voidRazerLayer::drawLine(){inttickSubCount10;intpointListKeepCount500;for(inti0;itickSubCount;i){if(pointList.size()0){pointList.pop_front();}else{break;}}while(pointList.size()pointListKeepCount){pointList.pop_front();}floatmax_lineWidth5;floatmin_lineWidth1;intalpha_min10;intalpha_max200;intR100;//arc4random()%255;intG100;//arc4random()%255;intB100;//arc4random()%255;intpointListCountpointList.size();std::listCCPoint::iterator itpointList.begin();floatpointIndex0;for(;it!pointList.end();it){intdistanceToMiddlefabs(pointIndex-pointListCount/2);floatpercent1.0-(float)distanceToMiddle/(float)(pointListCount/2.0);floatlintWidthmin_lineWidthmax_lineWidth*percent;intalphaalpha_minalpha_max*percent;ccc4(R,G,B,alpha);ccPointSize(lintWidth);ccDrawPoint(*it);pointIndex;}}voidRazerLayer::ccTouchesBegan(CCSet*pTouches,CCEvent*pEvent){CCSetIterator itpTouches-begin();CCTouch*touch(CCTouch*)*it;CCPoint beginPointtouch-getLocationInView();beginPointCCDirector::sharedDirector()-convertToGL(beginPoint);// beginPoint 检测pointList.push_back(beginPoint);}voidRazerLayer::ccTouchesMoved(CCSet*pTouches,CCEvent*pEvent){CCSetIterator itpTouches-begin();CCTouch*touch(CCTouch*)*it;CCPoint nextPointtouch-getLocationInView();nextPointCCDirector::sharedDirector()-convertToGL(nextPoint);// nextPoint 检测CCPoint preMovePointtouch-getPreviousLocationInView();preMovePointCCDirector::sharedDirector()-convertToGL(preMovePoint);floatdistanceccpDistance(nextPoint,preMovePoint);if(distance1){intd(int)distance;for(inti0;id;i){floatdistanceXnextPoint.x-preMovePoint.x;floatdistanceYnextPoint.y-preMovePoint.y;floatpercenti/distance;CCPoint newPoint;newPoint.xpreMovePoint.x(distanceX*percent);newPoint.ypreMovePoint.y(distanceY*percent);pointList.push_back(newPoint);}}}voidRazerLayer::ccTouchesEnded(CCSet*pTouches,CCEvent*pEvent){pointList.clear();}分析代码原理比较清晰在touchmove的回调方法中获取两个手势点之间的所有的像素点让后将存储起来然后在每帧的绘制中根据每个点的位置设定不同的线宽然后将这个点绘制出来。就形成了划痕效果。如果要在指定的范围内进行划痕处理只要处理代码中的指定点即可。

相关新闻

最新新闻

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现

SerenityOS 命令行选项解析指南:getopt 与 getopt_long 用法、返回值与底层实现 【免费下载链接】serenity The Serenity Operating System 🐞 项目地址: https://gitcode.com/GitHub_Trending/se/serenity 导读 本文以 getopt(3) 手册 为核心&a…

2026/10/3 16:42:15
轻量服务器还是ECS?大促云服务器选购与避坑实战指南

轻量服务器还是ECS?大促云服务器选购与避坑实战指南

每年大促节点,群里永远有人在问同一个问题:“38元的轻量服务器到底怎么抢?为什么我每次点进去都是已售罄?68元直购和99元的ECS我到底选哪个?”作为一个常年帮团队和自己采购云服务器的老用户,我太清楚这种纠…

2026/10/3 16:42:30
为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南

为 AI 代理的 Review 动作编写 Cedar 审批门控策略:review-agent-governance 策略编写实战指南 【免费下载链接】agents Multi-harness agentic plugin marketplace for Claude Code, Codex, Cursor, OpenCode, GitHub Copilot, and Google Antigravity 项目地址:…

2026/10/3 16:42:22
PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署

PaddleOCR 手写数学公式识别算法 CAN 实战指南:Counting-Aware Network 训练、评估与推理部署 【免费下载链接】PaddleOCR Turn any PDF or image document into structured data for your AI. A powerful, lightweight OCR toolkit that bridges the gap between i…

2026/10/3 7:41:27
Spring源码解析:构造器注入的类型转换与候选匹配机制

Spring源码解析:构造器注入的类型转换与候选匹配机制

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/10/3 16:42:24
openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由

openai-agents-python 多模型接入指南:深入解析 AnyLLMModel 适配层与 any-llm 路由 【免费下载链接】openai-agents-python A lightweight, powerful framework for multi-agent workflows 项目地址: https://gitcode.com/GitHub_Trending/op/openai-agents-pyth…

2026/10/3 16:42:28

日新闻

周新闻

月新闻