iOS cocos2d实现slider(滑块)控件效果源码
这半年一直在用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
//// CCSlider.m// DiceGameBox2D//// Created by jasonsalex on 13-1-29.//////开发人员:Jason's.Alex//QQ:531401335#import "CCSlider.h"@implementation CCSlider@synthesize maxValue;@synthesize minValue;@synthesize curValue;+(id)sliderWithCCSlider:(id)button background:(id)bg target:(id)target callback:(SEL)cb{ return [[[self alloc] sliderWithInit:button background:bg target:target callback:cb]autorelease];}-(id)sliderWithInit:(id)button background:(id)bg target:(id)tar callback:(SEL)cb{ if(self=[super init]) { target=tar; callBack=cb; curValue=0; maxValue=0; minValue=0; CCSprite* bgSprite=[CCSprite spriteWithFile:bg]; slider=[CCSprite spriteWithFile:button]; [slider setPosition:ccp(0,bgSprite.boundingBox.size.height*0.5f)]; [self setDisplayFrame:[bgSprite displayedFrame]]; [self addChild:slider z:10]; curMove=[slider position]; } return self;}-(void)onEnter{ [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:-128 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]; touchLocal=[self convertToNodeSpace:touchLocal]; //转换为当前节点的坐标 return CGRectContainsPoint([slider boundingBox], touchLocal);}-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event{ CGPoint touchMove=[touch locationInView:[touch view]]; touchMove=[[CCDirector sharedDirector] convertToGL:touchMove]; touchMove=[self convertToNodeSpace:touchMove]; CGPoint subMove=ccpSub(touchMove, curMove); CGPoint moveTo=ccp(slider.position.x+subMove.x,curMove.y); moveTo=[self convertToWorldSpace:moveTo]; if(CGRectContainsPoint([self boundingBox], moveTo)) //判断是否超出了范围 { [slider setPosition:[self convertToNodeSpace:moveTo]]; CGSize size=[self boundingBox].size; float def=maxValue-minValue; curValue=slider.position.x*(def/size.width); //将当前滑块坐标转换为数值 [target performSelector:callBack withObject:self]; } curMove=[slider position];}-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event{ }-(void)setCurValue:(float)value //修改当前数值{ if(value<=maxValue&&value>=minValue) { CGSize size=[self boundingBox].size; float def=maxValue-minValue; float x=(value/def)*size.width; //将数值转换为坐标点 [slider setPosition:ccp(x, slider.position.y)]; curValue=value; } }@end