cocos2d中实现触摸按钮换图效果方案

主要原理是当TouchBegan时根据按钮下的坐标把对应按钮换成按下的效果图,当TouchMoved时根据移动previousLocationInView坐标取消对应按钮的按下效果图,即把按钮还原成未按下的图,当TouchEnded时根据抬手的坐标取消对应按钮的按下效果图,也即把按钮还原成未按下的图,直接上代码:

-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
        
        [self show];
        
        self.isTouchEnabled=YES;
	}
	return self;
}

-(void) show
{
    CGSize s = [[CCDirector sharedDirector] winSize];
	
	//触摸数组,用于存放要检测触摸的精灵
    movableSprites = [[NSMutableArray alloc] init];
	
	[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"MineButton.plist"];
	//根据一个plist文件名构建CCSpriteFrame对象并添加到内存池中,就是加载要用到的各种图片

    //背景图
	CCSprite *bg = [CCSprite spriteWithFile:@"bg.png"];
	bg.position=ccp(s.width/2,s.height/2);
	[self addChild:bg z:-1 tag:99];
    
    //levelBt等级按钮
	levelBt=[CCSprite spriteWithSpriteFrameName:@"btnLevel.png"];
	levelBt.position=ccp(170,levelBt.contentSize.height/2+50);
	[self addChild:levelBt z:1 tag:103];
	[movableSprites addObject:levelBt];
    
    //scoreBt按钮
	scoreBt=[CCSprite spriteWithSpriteFrameName:@"btnScore.png"];
	scoreBt.position=ccp(230,scoreBt.contentSize.height/2+50);
	[self addChild:scoreBt z:1 tag:104];
	[movableSprites addObject:scoreBt];
    
    //moreBt按钮
	moreBt=[CCSprite spriteWithSpriteFrameName:@"btnMore.png"];
	moreBt.position=ccp(290,moreBt.contentSize.height/2+50);
	[self addChild:moreBt z:1 tag:105];
	[movableSprites addObject:moreBt];
    
}



#pragma mark Touch Method
//更换触摸协议
-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-1 swallowsTouches:YES];
	//priority参数越小优先级越高
}

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event
{
    BOOL hasTouched = NO;
    CGPoint touchlocation = [touch locationInView: [touch view]];
    touchlocation =[[CCDirector sharedDirector] convertToGL:touchlocation];
    
    CCSprite * newSprite = nil;
	if (newSprite == nil) {
		for (CCSprite *sprite in movableSprites) {
			if (CGRectContainsPoint(sprite.boundingBox, touchlocation)) {            
				newSprite = sprite;
				break;
			}
		} 
		
	}
	int na = [newSprite tag];
	switch (na) {
        case 103:
			CCLOG(@"levelBt");
			//换按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnLeveled.png"];
            hasTouched = YES;
			break;
        case 104:
			CCLOG(@"scoreBt");
			//换按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnScoreed.png"];
            hasTouched = YES;
			break;
        case 105:
			CCLOG(@"moreBt");
			//换按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnMoreed.png"];
            hasTouched = YES;
			break;
		default:
            hasTouched = NO;
			break;
	}
    if (hasTouched==YES) {
        CCLOG(@"吞并触摸事件");
		//按中本层的按钮
        return YES;
    }
    else
    {
        CCLOG(@"传递触摸事件");
		//未按中本层的按钮
        return NO;
    }
    
}

-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{

    //获取旧的坐标,即按下时的坐标,移动了就表示取消对应按钮的动作,那么就要还原按钮原图
	CGPoint oldTouchLocation=[touch previousLocationInView:touch.view];
    oldTouchLocation=[[CCDirector sharedDirector] convertToGL:oldTouchLocation];
	
    CCSprite * newSprite = nil;
	if (newSprite == nil) {
		for (CCSprite *sprite in movableSprites) {
			if (CGRectContainsPoint(sprite.boundingBox, oldTouchLocation)) {            
				newSprite = sprite;
				break;
			}
		} 
		
	}
	int na = [newSprite tag];
	switch (na) {
        case 103:
			CCLOG(@"levelBt");
			//换未按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnLevel.png"];
			break;
        case 104:
			CCLOG(@"scoreBt");
			//换未按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnScore.png"];
			break;
        case 105:
			CCLOG(@"moreBt");
			//换未按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnMore.png"];
			break;
		default:
			break;
	}
    
}

-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event
{
    CGPoint touchlocation = [touch locationInView: [touch view]];
    touchlocation =[[CCDirector sharedDirector] convertToGL:touchlocation];
    
    CCSprite * newSprite = nil;
	if (newSprite == nil) {
		for (CCSprite *sprite in movableSprites) {
			if (CGRectContainsPoint(sprite.boundingBox, touchlocation)) {            
				newSprite = sprite;
				break;
			}
		} 
		
	}
	int na = [newSprite tag];
	switch (na) {
        case 103:
			CCLOG(@"levelBt");
			//换按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnLevel.png"];
			//执行按钮触发的函数
            [self showSelectLevelLayer];
			break;
        case 104:
			CCLOG(@"scoreBt");
			//换按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnScore.png"];
			//执行按钮触发的函数
            [self showScoreLayer];
			break;
        case 105:
			CCLOG(@"moreBt");
			//换按下的效果图
            [GameHelper changeImageBtn:newSprite imagePath:@"btnMore.png"];
			//执行按钮触发的函数
            [self showMoreLayer];
			break;
		default:
			break;
	}
    
}

///
-(void)showSelectLevelLayer
{

}

-(void)showScoreLayer
{

}

-(void)showMoreLayer
{
    
}

///

以上纯属个人想法,若有更好的方案敬请赐教!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值