`
zhangziyangup
  • 浏览: 1186104 次
文章分类
社区版块
存档分类
最新评论

4个很酷的类

 
阅读更多

原文:http://www.cnblogs.com/iosfans/archive/2011/12/18/2292734.html

接下来要出场的几个类的确很酷,你会发现很多游戏都在使用他们。

本章用到了以下几个类:

CCProgressTimer

CCParallaxNode

CCRibbon

CCMotionStreak

先看效果吧

CCParallaxNode(视差节点)
从名字上看得出来,此类继承自CCNode。cocos2D官方是这样说的:

A node that simulates a parallax scroller

The children will be moved faster / slower than the parent according the the parallax ratio.

此类能容纳一些孩子们,这些孩子们一般情况而言都是CCSprite。这些孩子们会按照事先设定好的比例来进行不同速度的移动。最终造成一种视觉上的景深感。
看代码吧:

复制代码
 1 -(id)init
 2 {
 3     if (self = [super init]) {
 4         
 5         CCParallaxNode  *para = [CCParallaxNode node];
 6         CGSize sizeOfWin = [[CCDirector sharedDirector] winSize];
 7         
 8         //用4个精灵来显示4个层次
 9         CCSprite *parallax1 = [CCSprite spriteWithFile:@"parallax1.png"];
10         CCSprite *parallax2 = [CCSprite spriteWithFile:@"parallax2.png"];
11         CCSprite *parallax3 = [CCSprite spriteWithFile:@"parallax3.png"];
12         CCSprite *parallax4 = [CCSprite spriteWithFile:@"parallax4.png"];
13         
14         //设置孩子们的定位点
15         parallax1.anchorPoint = CGPointMake(0, 1);
16         parallax2.anchorPoint = CGPointMake(0, 0.5);
17         parallax3.anchorPoint = CGPointMake(0, 0.5);
18         parallax4.anchorPoint = CGPointMake(0, 0);
19         CGPoint topOffset = CGPointMake(0, sizeOfWin.height);
20         CGPoint midOffset = CGPointMake(0, sizeOfWin.height * 0.5);
21         CGPoint zeroOffset = CGPointZero;
22         
23         //注意,这里并不是真正的坐标点,而是用来控制速度的。
24         CGPoint piont1 = CGPointMake(0.5f, 0);
25         CGPoint piont2 = CGPointMake(1, 0);
26         CGPoint piont3 = CGPointMake(1.5, 0);
27         CGPoint piont4 = CGPointMake(2, 0);
28         
29         //添加4个孩子,注意z轴、速度差别和基于定位点的Offset
30         [para addChild:parallax1 z:1 parallaxRatio:piont1 positionOffset:topOffset];
31         [para addChild:parallax2 z:2 parallaxRatio:piont2 positionOffset:midOffset];
32         [para addChild:parallax3 z:3 parallaxRatio:piont3 positionOffset:midOffset];
33         [para addChild:parallax4 z:4 parallaxRatio:piont4 positionOffset:zeroOffset];
34         
35         [self addChild:para];
36         
37         //动起来
38         CCMoveTo *moveTo = [CCMoveTo actionWithDuration:5 position:CGPointMake(-330, 0)];
39         CCMoveTo *moveBack = [CCMoveTo actionWithDuration:5 position:CGPointMake(0, 0)];
40         CCSequence *se = [CCSequence actions:moveTo,moveBack, nil];
41         CCRepeatForever *repeat = [CCRepeatForever actionWithAction:se];
42         [para runAction:repeat];
43     }
44     return  self;
45 }
复制代码

关键地方都写上注释了。不难,但是很有用的一个类。
注意,因为移动速度不一、各个层的的定位点和Offset不一样,在moveTo的时候边界的计算要综合考虑,避免移出边界。

CCProgressTimer(进度条)

  时间进度类可用于很多地方,比如加载进度条,或者用于展示处于失效状态的按钮恢复到激活状态所需要的时间。比如WOW中技能的CD冷却效果,想象一下如何实现这个效果,一个显示技能图标的精灵,一个半透明的起蒙版作用的遮罩精灵。

恩,我尝试着模仿了一下游戏中的技能冷却效果,还不错。这里特别提一下像素的【刀剑2】一个很值得一玩的游戏。

先看cocos2D官方的一句话定义:

CCProgresstimer is a subclass ofCCNode. It renders the inner sprite according to the percentage. The progress can be Radial, Horizontal or vertical.

其实有了这句话,不用多解释了,足够清晰!想象一下你接触过的游戏,很多效果用CCprogressTimer都可以实现。
我的例子中因为考虑到代码的可扩展性,定义了一个继承自CCNode的类。此类中其中一个CCSprite成员显示技能,一个CCProgressTimer用来起蒙版作用,另外也将技能CD冷却时间作为属性加入了类中。下面是SkillSprite的代码:
复制代码
  1 //
  2 //  SkillSprite.m
  3 //  CH07
  4 //
  5 //  Created by phc on 11-12-17.
  6 //  Copyright (c) 2011年 hxsoft. All rights reserved.
  7 //
  8 
  9 #import "SkillSprite.h"
 10 
 11 @implementation SkillSprite
 12 -(void)dealloc
 13 {
 14     //这个Scheduler必须手动释放
 15     [[CCScheduler sharedScheduler] unscheduleAllSelectorsForTarget:self];
 16     [super dealloc];
 17 }
 18 //向外部提供的静态初始化方法
 19 +(id)skillWithParent:(CCNode *)parentNode spriteFile:(NSString *)fileName
 20 {
 21     return [[[self alloc] initWithParent:parentNode spriteFile:fileName] autorelease];
 22 }
 23 
 24 -(int)cdTime
 25 {
 26     return cdTime_;
 27 }
 28 -(void)setCdTime:(int)cdTime
 29 {
 30     cdTime_ = cdTime;
 31 }
 32 -(id)initWithParent:(CCNode *)parentNode spriteFile:(NSString *)fileName
 33 {
 34     if (self = [super init]) {
 35         CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
 36         [frameCache addSpriteFramesWithFile:@"skill.plist"];
 37         
 38         //根据传递进来的fileName参数显示相应的图片给skillSprite
 39         skillSprite = [CCSprite spriteWithSpriteFrame:[frameCache spriteFrameByName:fileName]];
 40         
 41         //构造蒙版层,mask.png本身带有透明效果。
 42         maskSprite = [CCProgressTimer progressWithFile:@"mask.png"];
 43        
 44         //技能图标在下,蒙版在上
 45         [self addChild:skillSprite z:0];
 46         [self addChild:maskSprite z:1];
 47         
 48         
 49         
 50     }
 51     return  self;
 52 }
 53 
 54 //在onEnter中注册目标触摸事件委托,注意不要漏掉[super onEnter]
 55 -(void)onEnter
 56 {
 57     [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
 58     [super onEnter];
 59 }
 60 //在onExit中注销目标触摸事件委托
 61 -(void)onExit
 62 {
 63     [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
 64     [super onExit];
 65 }
 66 
 67 //为了检测是否命中,需要获取自身的rect,这里需要好好琢磨。
 68 -(CGRect)getRect
 69 {
 70     CGRect tmp = [maskSprite boundingBox];
 71     return CGRectMake(-tmp.size.width * 0.5, -tmp.size.height * 0.5, tmp.size.width, tmp.size.height);
 72 }
 73 
 74 -(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
 75 {
 76     //获得触摸点坐标
 77     CGPoint touchPos = [self convertTouchToNodeSpaceAR:touch];
 78 
 79     if (CGRectContainsPoint([self getRect], touchPos)) {
 80         if (CDLock) {
 81             return YES;
 82         }
 83         //如果命中了技能
 84         maskSprite.percentage = 100;
 85         //让蒙版转起来
 86         [[CCScheduler sharedScheduler] scheduleUpdateForTarget:self priority:0 paused:NO];
 87         timePicker = 0;
 88         CDLock = YES;
 89         return  YES;
 90     }
 91     return NO;
 92     
 93 }
 94 
 95 -(void)update:(ccTime)dt
 96 {
 97     //把时间片累加起来,来对应显示进度。
 98     timePicker += dt*10;
 99     float percent =100 - timePicker / cdTime_*100;
100     
101     
102     if (percent <= 0) {
103         percent = 0;
104         maskSprite.percentage = 0;
105         [[CCScheduler sharedScheduler] unscheduleAllSelectorsForTarget:self];
106         CDLock = NO;
107     }else
108     {
109         maskSprite.percentage = percent;
110     }
111 }
112 @end
复制代码
1. 注册了目标触摸,实现了触摸的began方法。
2. 在初始化方法中将fileName作为参数传了进来,当然,也可以把CDTime作为参数加进去。
3. 根据CDTime,在update方法中percentage的增加速度。
CCRibbon(链条)和CCMotionStreak(拖尾链条)
  官方的,总是最权威的:
  A CCRibbon is a dynamically generated list of polygons drawn as a single or series of triangle strips. The primary use of CCRibbon is as the drawing class of Motion Streak, but it is quite useful on it's own. When manually drawing a ribbon, you can call addPointAt and pass in the parameters for the next location in the ribbon. The system will automatically generate new polygons, texture them accourding to your texture width, etc, etc.
  从其中可以看出来CCRibbon和CCMotionStreak似乎密切相关(其实在CCMotionStreak中CCRibbon是作为一个属性出现的)。addPoiontAt是最重要的一个方法,它的作用是在该坐标点上添加一个新的链条。
复制代码
 1 //创建链条对象
 2 -(void)createRibbon
 3 {
 4     CCRibbon *rib = [CCRibbon ribbonWithWidth:32 image:@"sl.png" length:32 color:ccc4(255, 255, 255, 255) fade:125];
 5     [self addChild:rib z:3 tag:TAGRIBBON];
 6 }
 7 
 8 //移除链条对象,本例中在touchEnd的时候使用
 9 -(void)removeRibbon
10 {
11     [self removeChildByTag:TAGRIBBON cleanup:YES];
12 }
13 
14 
15 //创建拖尾链条,我写错了单词了,:-)
16 -(void)createMotionSpeak
17 {
18     CCMotionStreak *mot = [CCMotionStreak streakWithFade:0.4f minSeg:100 image:@"sl.png" width:16 length:16 color:ccc4(255, 0, 255, 255)];
19     [self addChild:mot z:3 tag:TAGMOTIONSTREAK];
20 }
21 //移除拖尾链条
22 -(void)removeMotionStreak
23 {
24     [self removeChildByTag:TAGMOTIONSTREAK cleanup:YES];
25 }
26 
27 //本例同时展示了链条和拖尾链条,没有办法,奇数次使用CCRibbon,偶数次使用CCMotionStreak
28 -(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
29 {
30     UITouch *touch = [touches anyObject];
31     CGPoint touchPosition = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
32     if (isMotionStreak) {
33         [self createMotionSpeak];
34         CCMotionStreak *mot = [self getChildByTag:TAGMOTIONSTREAK];
35         [mot.ribbon addPointAt:touchPosition width:16];
36     } else {
37         [self createRibbon];
38         CCRibbon *rib = [self getChildByTag:TAGRIBBON];
39         [rib addPointAt:touchPosition width:8];
40     }
41     
42 }
43 
44 //在触摸点移动的时候,addPointAt。
45 -(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
46 {
47     UITouch *touch = [touches anyObject];
48     CGPoint touchPosition = [[CCDirector sharedDirector] convertToGL:[touch locationInView:touch.view]];
49     if (isMotionStreak) {
50         CCMotionStreak *mot = [self getChildByTag:TAGMOTIONSTREAK];
51         [mot.ribbon addPointAt:touchPosition width:16];
52     } else {
53         CCRibbon *rib = [self getChildByTag:TAGRIBBON];
54         [rib addPointAt:touchPosition width:8];
55     }
56     
57 
58     
59 }
60 
61 //触摸结束的时候,清除链条
62 -(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
63 {
64     if (isMotionStreak) {
65         [self removeMotionStreak];
66     } else {
67          [self removeRibbon];
68     }
69    
70     isMotionStreak = !isMotionStreak;
71 }
复制代码
cc end。
分享到:
评论

相关推荐

    【吼吼睡cocos2d学习笔记】第七章 - 4个很酷的类 代码

    在本节中,我们将深入探讨Cocos2d游戏开发框架中的四个非常有用的类:CCParallaxNode、CCRibbon、CCProgressTime以及CCMotionStreak。这些类为开发者提供了丰富的视觉效果,增强了游戏的互动性和吸引力。 首先,让...

    JSP四酷全书

    《JSP四酷全书》是一本专注于JSP网站开发的综合教程,特别涵盖了构建BBS论坛的全过程。这本书深入浅出地介绍了如何利用JSP技术来设计和实现功能丰富的在线论坛系统。JSP(JavaServer Pages)是Java平台上的一个核心...

    VB 一个超酷的时钟 VB 一个超酷的时钟

    在这个场景中,"VB 一个超酷的时钟"很可能是指一个使用VB编写的、具有独特设计或功能的数字时钟程序。这个时钟可能采用了现代、动态或者用户友好的界面设计,使其在视觉效果上显得“超酷”。 在VB中开发时钟程序,...

    一个炫酷的抽奖系统源码

    《构建一个炫酷的抽奖系统:技术实现与应用》 抽奖系统在各种活动中扮演着重要的角色,无论是企业年会还是日常教学,都能...而“lucky”文件很可能是这个系统的源代码,对于开发者而言,这是一个学习和参考的好资源。

    网站开发四酷全书(下)

    《网站开发四酷全书(下)》主要聚焦于ASP.NET技术在网站开发中的应用,尤其是针对BLOG和SHOP这两个具体项目。ASP.NET是微软公司推出的一种强大的Web应用程序框架,它构建在.NET Framework之上,提供了丰富的功能和...

    很酷的窗口界面[Writepad.rar]-精品源代码

    【标题】"很酷的窗口界面[Writepad.rar]"是一个以Writepad命名的窗口界面设计的源代码项目,其中包含了创建独特且引人注目的用户界面的编程元素。这个项目可能是一个文本编辑器或者类似的软件应用,其界面设计旨在...

    酷Q插件合集

    这个“酷Q插件合集”可能包含以下几类插件: 1. 自动回复插件:这类插件可以设定关键词匹配,当收到特定消息时,酷Q会自动回复预设的内容,比如问候语、天气预报或者常见问题的答案。 2. 消息过滤插件:用于过滤掉...

    一个很酷的信息弹出对话框+VB源代码.rar_VB 对话框_VB源代码_redm3l_一个很酷的登录对话框.r

    "一个很酷的信息弹出对话框+VB源代码"是一个示例项目,旨在展示如何创建具有独特设计和功能的对话框。这个项目包含了多个对话框的实例,不仅限于基本的对话框,可能还包括自定义对话框以满足特定的用户界面需求。 ...

    多功能超酷界面程序类

    "多功能超酷界面程序类"是一个专门设计用于创建高质量、有质感界面的编程类,它允许开发者轻松地构建出吸引人且易用的软件界面。这类程序通常包含了丰富的控件库、自定义布局选项以及动画效果,以提升软件的整体视觉...

    VC实现超酷工具栏封装类

    本文将深入探讨如何使用Visual C++(简称VC)来实现一个超酷的工具栏封装类,这对于提升应用程序的用户体验至关重要。工具栏不仅能够提供快捷访问常用功能的按钮,还能通过自定义图标和样式,使软件更加吸引用户。 ...

    很酷的tooltip信息显示控件源码

    "很酷的tooltip信息显示控件源码" 这个标题表明我们讨论的是一个关于tooltip(提示信息)的控件,而且这个控件在设计或功能上具有独特的酷炫特点。Tooltip通常是在鼠标悬停在某个元素上时出现的一种小窗口,用于显示...

    一个很酷的网站源文件

    【标题】:“一个很酷的网站源文件”指的是www.samorost2.net的网页源代码,这...通过分析和学习这个很酷的网站源文件,我们可以了解到当前最佳的Web开发实践,提升自己的编程技能,同时也可能激发新的设计和开发灵感。

    安卓动画效果相关-很简单却很酷的粒子破碎效果.zip

    本资源包"安卓动画效果相关-很简单却很酷的粒子破碎效果.zip"似乎提供了一个有趣的粒子破碎特效,这在游戏、应用启动画面或交互设计中非常常见。下面我们将深入探讨这个特效的实现原理及相关知识点。 首先,粒子...

    很酷的菜单

    总之,"很酷的菜单"是一个利用VC++技术开发的高级菜单组件,它可能集成了许多创新性的设计和功能,旨在提升Windows应用程序的界面质量和用户交互体验。对于开发者来说,研究和理解这个组件的实现原理和使用方法,将...

    很酷的FLASH导航条

    "很酷的FLASH导航条"这个主题,意味着我们将探讨如何制作和实现具有独特设计和功能的Flash导航条。 【描述】:“很酷的FLASH导航条” "很酷的FLASH导航条"不仅体现在外观上的独特和时尚,还可能包含一些创新的交互...

    jsp四酷全书bbs源代码

    《jsp四酷全书》是一本深入探讨JavaServer Pages(JSP)技术的书籍,而提供的BBS模块源代码则是该书中的一个实践项目,旨在帮助读者更好地理解和应用JSP技术来开发论坛系统。这个BBS源代码包含了构建一个基本在线...

    酷盘开放平台DEMO

    3. **Fa.Kanbox.API.dll**: 这是酷盘开放平台提供的SDK(Software Development Kit)的一部分,包含了与酷盘API交互所需的类和方法。开发者可以通过引用这个库,直接调用预定义的函数来执行如文件上传、下载、删除等...

    asp网站开发四“酷”全书

    【ASP网站开发四“酷”全书】是一本专注于ASP(Active Server Pages)技术的综合性教程,涵盖了构建网络应用中的四个重要组成部分:BBS(论坛)、SHOP(电子商务)、BLOG(博客)和PUB(公共服务)。这四个部分代表...

    酷Q机器人插件

    在这个案例中,"cdyjzw.cqp.dll"很可能是某个酷Q插件的核心组件,包含了实现特定功能的函数和类。开发者可以通过引用这个DLL,调用其中的接口来扩展酷Q机器人的功能。例如,这个插件可能实现了特定的聊天机器人逻辑...

    超酷的后台界面,很漂亮 HTML 模板

    "free-admin-templates-002"、"free-admin-templates-003"这类文件可能是完整的后台管理模板集合,包含多个页面和组件。它们通常包括CSS样式表(用于控制外观)、JavaScript脚本(处理交互逻辑)以及图像和其他资源...

Global site tag (gtag.js) - Google Analytics