主要原理是当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
{
}
///
以上纯属个人想法,若有更好的方案敬请赐教!