开始写正式一点的分享了!想说的是游戏开发离不开点击、触碰、挤压等等的人为动作来增强游戏的可玩性!(还请大牛们多多指点!) 现在就动手,demo的目的是点击一个精灵,然后让它做你指定的动作,譬如旋转
开始写正式一点的分享了!想说的是游戏开发离不开点击、触碰、挤压等等的人为动作来增强游戏的可玩性!(还请大牛们多多指点!)
现在就动手,demo的目的是点击一个精灵,然后让它做你指定的动作,譬如旋转、跳跃、消失等等!我就弄个旋转的吧!你们自己据一方三!
1.打开xcode,新建项目,命名随意!哇卡卡卡!我就交TouchDemo!
之后你就看到文件目录就是这样的:
但是我之前一开始想说再introLayer.h/m 的“-(void) onEnter”里面写实现代码!but,shit!代码木有错,精灵也正常显示了!重点就是不做动作!后来我去到helloWorldLayer 里面写实现代码就OK!有时间再研究为神马哈哈!接着猪蹄!
2.打开HelloWorldLayer.h文件,声明一个精灵,和一个背景:
CCSprite *MySprite; CCSprite *background;
然后在init方法里添加代码如下:
if( (self=[super init]) ) { // create and initialize a Label CGSize winSize=[CCDirector sharedDirector].winSize; //告诉cocos2d使用RBG565的像素格式 [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_RGB565]; background = [CCSprite spriteWithFile:@"sexLady.png"]; background.anchorPoint = ccp(0,0); [self addChild:background]; [CCTexture2D setDefaultAlphaPixelFormat:kCCTexture2DPixelFormat_Default]; MySprite = [CCSprite spriteWithFile:@"sprite.png"]; float offsetFraction = ((float)(1+1))/(4+1); MySprite.position = ccp(winSize.width*offsetFraction, winSize.height/2); [self addChild:MySprite]; //这个是启动ccTouch delegate事件 [[[CCDirector sharedDirector] touchDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; }
然后呢写一个你要实现动作的方法:
- (void)SpriteForTouch:(CGPoint)touchLocation { if (CGRectContainsPoint(MySprite.boundingBox, touchLocation)) { CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:2 angle:360]; CCRepeatForever* repeat = [CCRepeatForever actionWithAction:rotateBy]; [MySprite runAction:repeat]; } }
最后调用ccTouchBegan方法:
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLocation = [self convertTouchToNodeSpace:touch]; [self SpriteForTouch:touchLocation]; return TRUE;}
so this is the end of demo and run it and have fun!!!!see u!!!