您的位置:首页 > 移动开发 > Cocos引擎

iOS cocos2d实现自定义button(按钮特效)控件效果源码

2014-12-20 10:57 513 查看
这半年一直在用object-c开发一个ios游戏。使用cocos2d和box2d 。开发游戏变的简单多了。这游戏开发了半年多了。直到最近这个游戏停止了,因为资金问题,老大没法在发更多的工资了。哎,真的非常遗憾,我一个人完成游戏的编辑器开发,脚本开发,游戏代码开发,很不容易,因为我学object-c,coco2d才看了2个星期的书就直接开发了,以前是搞c++的吗。感觉4个人开发游戏真的很累,游戏为了脱颖出更加真实的效果还使用了物理引擎,在老大的同意的情况下,我共享cocos2d自己写的一些大家比较常用的,因为cocos2d有些控件不怎么好用或者没有,反正我觉得是这样的。如slider(滑块),button(按钮),RollNumber(数字滚动),Progress(进度条)....控件一一在我的博客里面公布,可以直接使用.源码打包下载

开发人员:Jason's.Alex QQ:531401335

csdn博客:http://blog.csdn.net/RuShrooM

[cpp]
view plaincopy

//
// SpriteMenu.h
// DiceGameBox2D
//
// Created by 电脑 富力 on 12-12-25.
// Copyright (c) 2012年 科技. All rights reserved.
//菜单精灵

//开发人员:Jason's.Alex
//QQ:531401335

#import "cocos2d.h"
#import "ResourceLoad.h"

@interface SpriteMenu : CCSprite<CCTargetedTouchDelegate>
{
CCSprite *normalImage;//没有选择的图片
CCSprite *selImage; //选着的图片

id target; //调用对象
SEL endedFunAddr; //触摸结束调用的函数
SEL beganFunAddr;//触摸开始调用的函数

BOOL isTouchEffect;//触摸特效

float zoom;//缩放
}

@property(nonatomic,readwrite,assign) CCSprite *normalImage;//没有选择的图片
@property(nonatomic,readwrite,assign) CCSprite *selImage; //选着的图片
@property(readwrite)BOOL isTouchEffect;

+(id)menuWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag beganFunc:(SEL)beganFunc endedFunc:(SEL)endedFunc;

-(id)initWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag beganFunc:(SEL)beganFunc endedFunc:(SEL)endedFunc;

-(void)showNormalImage;
-(void)ShowSelectImage;

-(void)showEffect:(NSString*)name;//显示特效

@end

[cpp]
view plaincopy

//
// SpriteMenu.m
// DiceGameBox2D
//
// Created by 电脑 富力 on 12-12-25.
// Copyright (c) 2012年 科技. All rights reserved.
//
//开发人员:Jason's.Alex
//QQ:531401335

#import "SpriteMenu.h"

@implementation SpriteMenu

@synthesize normalImage;
@synthesize selImage;
@synthesize isTouchEffect;

+(id)menuWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag beganFunc:(SEL)beganFunc endedFunc:(SEL)endedFunc;
{
return [[[self alloc] initWithSpriteMenu:imagePath SelImage:selPath target:tag beganFunc:beganFunc endedFunc:endedFunc]autorelease];
}

-(id)initWithSpriteMenu:(id)imagePath SelImage:(id)selPath target:(id)tag beganFunc:(SEL)beganFunc endedFunc:(SEL)endedFunc;
{
if(self=[super init])
{

normalImage=[[CCSprite alloc]initWithFile:imagePath];
selImage=[[CCSprite alloc]initWithFile:selPath];

target=tag;
endedFunAddr=endedFunc;
beganFunAddr=beganFunc;

[self setDisplayFrame:[normalImage displayedFrame]];
isTouchEffect=FALSE;//默认启用触摸特效的
zoom=[self scale];
}

return self;
}

-(void)dealloc
{
NSLog(@"~SpriteMenu");
[normalImage release];
[selImage release];

[super dealloc];
}

-(void)onEnter
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:10 swallowsTouches:YES];

[super onEnter];
}

-(void)onExit
{
[[CCTouchDispatcher sharedDispatcher]removeDelegate:self];

[super onExit];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLocal=[touch locationInView:[touch view]];
touchLocal=[[CCDirector sharedDirector]convertToGL:touchLocal];

BOOL isTouch=CGRectContainsPoint([self boundingBox], touchLocal);
if(isTouch)
{
[self ShowSelectImage];

if(isTouchEffect) //是否使用了触摸特效
{
id scaleTo=[CCScaleTo actionWithDuration:0.25f scale:zoom-0.2f];
id callfunc=nil;

if(beganFunAddr!=nil)
callfunc=[CCCallFunc actionWithTarget:target selector:beganFunAddr];

id sequece=[CCSequence actions:scaleTo, callfunc,nil];
[self runAction:sequece];

}else
{
if(beganFunAddr!=nil)
[target performSelector:beganFunAddr];
}

}

return isTouch;
}

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

}

-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
[self showNormalImage];

if(isTouchEffect)
{
id scaleTo=[CCScaleTo actionWithDuration:0.25f scale:zoom+0.2f];
id scaleTo1=[CCScaleTo actionWithDuration:0.25f scale:zoom];

id callfunc=nil;

if(endedFunAddr!=nil)
callfunc=[CCCallFunc actionWithTarget:target selector:endedFunAddr];

CCSequence* sequece=[CCSequence actions:scaleTo,scaleTo1,callfunc,nil];

[self runAction:sequece];
}else
{
if(endedFunAddr!=nil)
[target performSelector:endedFunAddr];
}

}

-(void)showNormalImage
{
[self setDisplayFrame:[normalImage displayedFrame]];
}

-(void)ShowSelectImage
{
[self setDisplayFrame:[selImage displayedFrame]];
}

-(void)showEffect:(NSString*)name//显示特效
{
if([name isEqualToString:@"stretch"])//拉伸特效
{
[self setScaleX:0];
id scaleXTo=[CCScaleTo actionWithDuration:2.0f scale:zoom];//拉伸
id fadeIn=[CCFadeIn actionWithDuration:2.0f];

id spawn=[CCSpawn actions:scaleXTo,fadeIn,nil];

[self runAction:spawn];
}
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐