加入收藏 | 设为首页 | 会员中心 | 我要投稿 江门站长网 (https://www.0750zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 教程 > 正文

cocos2d中实现触摸按钮换图效果计划

发布时间:2021-11-22 14:01:04 所属栏目:教程 来源:互联网
导读:主要原理是当TouchBegan时根据按钮下的坐标把对应按钮换成按下的效果图,当TouchMoved时根据移动previousLocationInView坐标取消对应按钮的按下效果图,即把按钮还原成未按下的图,当TouchEnded时根据抬手的坐标取消对应按钮的按下效果图,也即把按钮还原成

主要原理是当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  
{  
      
}  
  
///////////////////////////////////////////////////////////  

(编辑:江门站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读