Simple but Charming_0

本文介绍了一个使用Cocos2d-x创建动态噪点层的技术实现方案。该方案通过定时更新噪点图的纹理坐标来实现水平方向无缝滚动的视觉效果。文章详细展示了如何设置噪点图的纹理参数、位置以及混合函数,并通过定时器调用方法实现动态效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

动态噪点(要求水平方向无缝的噪点图:w=1024,h=640)

DynamicNoiseLayer.h

//
//  DynamicNoiseLayer.h
//  HungryBear
//
//  Created by Bruce Yang on 12-8-2.
//  Copyright (c) 2012年 EricGameStudio. All rights reserved.
//

#import "cocos2d.h"

@interface DynamicNoiseLayer : CCLayer {
    CCTexture2D* m_texGradientBg;
    CCTexture2D* m_texNoise;
    CCSprite* m_spNoise;
}

+(CCScene*) scene;

-(id) init;

@end

DynamicNoiseLayer.mm

//
//  DynamicNoiseLayer.mm
//  HungryBear
//
//  Created by Bruce Yang on 12-8-2.
//  Copyright (c) 2012年 EricGameStudio. All rights reserved.
//

#import "DynamicNoiseLayer.h"

#ifndef PORTRAIT_CENTER
    #define PORTRAIT_CENTER ccp(160, 240)
#endif

#ifndef LANDSCAPE_CENTER
    #define LANDSCAPE_CENTER ccp(240, 160)
#endif

#define SPEED_FACTOR   8

@implementation DynamicNoiseLayer

+(CCScene*) scene {
    CCScene* t_scene = [CCScene node];
	
	DynamicNoiseLayer* t_layer = [DynamicNoiseLayer node];
	[t_scene addChild:t_layer];
	
	return t_scene;
}

-(id) init {
	if((self = [super init])) {
        CCTextureCache* tmp_texCache = [CCTextureCache sharedTextureCache];
        
        // 1.背景图片~
        m_texGradientBg = [tmp_texCache addImage:@"gbg.png"];
        
        CCSprite* tmp_spBg = [[CCSprite alloc] initWithTexture:m_texGradientBg];
        [tmp_spBg setPosition: LANDSCAPE_CENTER];
        [self addChild:tmp_spBg];
        [tmp_spBg release];
        
        
        // 2.噪点图片~
        m_texNoise = [tmp_texCache addImage:@"noiseTexture.png"];
        ccTexParams tmp_texParams = (ccTexParams){GL_LINEAR, GL_LINEAR, GL_REPEAT, GL_REPEAT};
        [m_texNoise setTexParameters:&tmp_texParams];
        
        m_spNoise = [[CCSprite alloc] initWithTexture:m_texNoise];
        [m_spNoise setPosition: LANDSCAPE_CENTER];
        
        ccBlendFunc tmp_blendFunc = (ccBlendFunc){GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA};
        [m_spNoise setBlendFunc:tmp_blendFunc];
        
        [self addChild:m_spNoise];
        [m_spNoise release];
        
        
        // 3.定时器~
        [self schedule:@selector(tick:)];
	}
	return self;
}

-(void) tick:(ccTime)dt {
    static float offset = 0;
    offset += dt * SPEED_FACTOR;
    CGSize tmp_texRect = m_spNoise.textureRect.size;
    [m_spNoise setTextureRect:CGRectMake(offset, 0, tmp_texRect.width, tmp_texRect.height)];
}

-(void) dealloc {
    CCTextureCache* texCache = [CCTextureCache sharedTextureCache];
    [texCache removeTexture:m_texGradientBg];
    [texCache removeTexture:m_texNoise];
    [super dealloc];
}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值