如何实现在屏幕上有漂亮的刀光效果并有星星环绕

  我做项目时,也需要用到划屏这个效果,就在网上搜到了这一部分的讲解,根据原博主的解释和我自己的理解写得下面这篇文章         

    原博客地址  http://www.xcoder.cn/html/mobile/iOS/2013/0310/1863_7.html   

1.下载 CCBlade,解压后将其添加到工程中,解压后的文件是 CCBlade.m 和 CCBlade.h

 2.不过在Cocos2D 2.X中需要改变CCBlade.m,将其重命名为CCBlade.mm

     改变draw函数为下面的代码

- (void) draw{
    if ((reset && [path count] > 0) || (self.autoDim && _willPop)) {
        [self pop:1];
        if ([path count] < 3) {
            [self clear];
        }
    }
    
    if ([path count] < 3) {
        return;
    }
    
    _willPop = YES;
	
    NSAssert(_texture, @"NO TEXTURE SET");
    CC_NODE_DRAW_SETUP(); 
    
    ccGLBlendFunc( CC_BLEND_SRC, CC_BLEND_DST );
    
    ccGLBindTexture2D( [_texture name] );   
 
    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, sizeof(vertices[0]), vertices);
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, sizeof(coordinates[0]), coordinates);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 2*[path count]-2);
}
     在 - (id) initWithMaximumPoint:(int) limit函数中添加

self.shaderProgram = [[CCShaderCache sharedShaderCache] programForKey:kCCShader_PositionTexture];

   改变setWidth函数为下面的

- (void) setWidth:(float)width_{
    width = width_ * CC_CONTENT_SCALE_FACTOR();
}

    改变push函数为下面的

- (void) push:(CGPoint) v{
    _willPop = NO;
    
	if (reset) {
		return;
	}
    if (CC_CONTENT_SCALE_FACTOR() != 1.0f) {
        v = ccpMult(v, CC_CONTENT_SCALE_FACTOR());
    }

3、在CCBlade中定义一个由点组成的path数组,并穿过这些点绘制一个纹理直线

      打开CCBlade.h并在@interface中加入以下内容

@property(nonatomic,retain)NSMutableArray *path;

切换到CCBlade.mm并在@implementation中加入以下内容:

@synthesize path;

4、切换到HelloWorldLayer.h并作如下修改:

       在头文件中添加

#import "CCBlade.h"

          在 @interfac中定义

CCArray *_blades;
CCBlade *_blade;
float _deltaRemainder;
@property(nonatomic,retain)CCArray *blades;

5、切换到HelloWorldLayer.mm并做如下修改

在@implementation中添加

@synthesize blades = _blades;

在delloc中

[_blades release];
_blades = nil;

在init中

        _deltaRemainder = 0.0;
        _blades = [[CCArray alloc] initWithCapacity:3];
        CCTexture2D *texture = [[CCTextureCache sharedTextureCache]addImage:@"streak.png"];
        for (int i = 0; i < 3; i++)
        {
        //创建了3个在游戏中公用的CCBlade对象,对每一个blade,设置最大的点个数为50来防止轨迹太长,并设置
        //blade的纹理为Resources文件夹中的streak
            CCBlade *blade = [CCBlade bladeWithMaximumPoint:100];
        //设置每个blade的autoDim变量为NO,CCblade使用术语“Dim”来说明此blade会自动从尾巴到头的渐变消失
        //CCBlade自动从path数组中移除这些点
            blade.autoDim = NO;
            blade.texture = texture;
            [self addChild:blade z:2];
            [_blades addObject:blade];
        }
        //添加粒子效果
        _bladeSparkle = [CCParticleSystemQuad particleWithFile:@"blade_sparkle.plist"];
        [_bladeSparkle stopSystem];
        [self addChild:_bladeSparkle z:3];
        _timeCurrent = 0;
        _timePrevious = 0;
        [self scheduleUpdate];
    }

在update中

-(void) update: (ccTime) dt
{
    if ([_blade.path count] > 3)
    {
        _deltaRemainder+=dt*60*1.2;
        int pop = (int)roundf(_deltaRemainder);
        _deltaRemainder-=pop;
        [_blade pop:pop];
    }
    // update the time used by the swoosh sound
    _timeCurrent += dt;
}

 
 

在touchBegan中

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    for (UITouch *touch in touches){
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector]convertToGL:location];
        
        _startPoint = location;
        _endPoint = location;
        
        CCBlade *blade;
        CCARRAY_FOREACH(_blades, blade)
        {
            if (blade.path.count == 0)
            {
                _blade = blade;
                [_blade push:location];
                break;
            }
        }
        //刀片上环绕着星星  粒子效果
        _bladeSparkle.position = location;
        [_bladeSparkle resetSystem];     
    }
}

在touchMove中

- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    for (UITouch *touch in touches)
    {
        CGPoint location = [touch locationInView:[touch view]];
        location = [[CCDirector sharedDirector] convertToGL:location];
        
        //跟随触摸设置最后的点的坐标
        _endPoint = location;

        [_blade push:location];   //出现刀片
        
        //星星粒子效果跟随着触摸点
        _bladeSparkle.position = location;
        
        //计算播放时间
        ccTime deltaTime = _timeCurrent - _timePrevious;
        _timePrevious = _timeCurrent;
        
        //触摸过的点的坐标为了下面的计算
        CGPoint oldPosition = _bladeSparkle.position;

        //如果划屏的速度超过了1000 就播放音乐
        if (ccpDistance(_bladeSparkle.position, oldPosition) / deltaTime > 1000)
        {
            if (!_swoosh.isPlaying)
            {
                //播放音乐
                [_swoosh play];
            }
        }
    }
}

在touchEnd中

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
	for( UITouch *touch in touches )
    {
		CGPoint location = [touch locationInView: [touch view]];

        // 褪去叶片
        [_blade dim:YES];
        
        //划到最后使星星消失
        [_bladeSparkle stopSystem];    
	}
}

上面的就是制作刀片以及跟随刀片出现的星星效果

如果你有不同意见欢迎交流


你可能感兴趣的:(如何实现在屏幕上有漂亮的刀光效果并有星星环绕)