收藏 分享(赏)

Cocco2D-iPhone-实用教程.txt

上传人:eukav 文档编号:4014132 上传时间:2018-12-05 格式:TXT 页数:17 大小:41.33KB
下载 相关 举报
Cocco2D-iPhone-实用教程.txt_第1页
第1页 / 共17页
Cocco2D-iPhone-实用教程.txt_第2页
第2页 / 共17页
Cocco2D-iPhone-实用教程.txt_第3页
第3页 / 共17页
Cocco2D-iPhone-实用教程.txt_第4页
第4页 / 共17页
Cocco2D-iPhone-实用教程.txt_第5页
第5页 / 共17页
点击查看更多>>
资源描述

1、 3/4?shnwdsedp1?pdf?WAP?2“1/4?1/2?TXT?“?1/4t1/23/4“2?-?- 1 2D C Cocos2D-iPhone ?-?- 2 cocos2d-iphone ?-?- 4 Hellow World?-?- 6 Hellow Word ?-?- 9 ?-?- 11 ?-?- 13 ?-?- 14 ?-?- 17http:/ / 172DC Cocos2D-iPhone2 / 17http:/ / 17cocos2d-iphone./install_template.shhttp:/ / 17/Developer/Platforms/iPhoneOS.p

2、latform/Developer/Library/Xcode/P roject Templates/Application /http:/ / 17Hellow Worldhttp:/ / 17http:/ / 17http:/ / 17Hellow Word1C mainhttp:/ / 17Other Sources int main(int argc, char *argv) / it is safe to leave these lines as they are. NSAutoreleasePool *pool = NSAutoreleasePool new; UIApplicat

3、ionMain(argc, argv, nil, “T01AppDelegate“); pool release; return 0;2C T01AppDelegatez z z z z applicationDidFinishLaunchinghttp:/ / 173Cocos2d-iPhoneC HellowWorldScenehttp:/ / 17zzhttp:/ / 17zhttp:/ / 17Xcode - Build and runhttp:/ / 17http:/ / 17http:/ / 17http:/ / 17?-?- 1 CCScene ?-?- 2 CCLayer ?-

4、?- 3 CCSprite ?-?- 5 (CCDirector) ?-?- 5 Cocos2D-iPhone ?-?- 6 CCDirector( ) ?-?- 6 CCScene( ) ?-?- 8 CCLayer ?-?- 9 CCSprite ?-?- 11 ?-?- 13 ?-?- 17 ?-?- 17 ?-?- 17 ?-?- 20 SysMenu ?-?- 20 GameLayer ?-?- 22 SettingsLayer ?-?- 26 GameCntrolMenu ?-?- 29http:/ / 29CCScenez z zz z z ? ? ? z http:/ 2 /

5、29z zzz zCCLayerhttp:/ / 29http:/ / 29CCSpritez z z z(CCDirector)singletonhttp:/ / 29Cocos2D-iPhoneCCDirector()http:/ / 29/ before creating any layer, set the landscape mode Director sharedDirector setDeviceOrientation: CCDeviceOrientationLandscapeRight; / attach the OpenGL view to a window Director

6、 sharedDirector attachInView:window; / show FPS Director sharedDirector setDisplayFPS:YES;/ frames per second Director sharedDirector setAnimationInterval:1.0/60;/ Default texture format for PNG/BMP/TIFF/JPEG/GIF images / It can be RGBA8888, RGBA4444, RGB5_A1, RGB565 / You can change anytime. Textur

7、e2D setDefaultAlphaPixelFormat:kTexture2DPixelFormat_RGBA8888;zz http:/ / 291. 2.Scene *runningScene_ Scene *nextScene; NSMutableArray *scenesStack_;2. (void) pushScene:(Scene*) scene; (void) popScene;(void) replaceScene: (Scene*) scene;CCScene()http:/ / 29CCLayerzzhttp:/ / 29NSInvocation *invocatio

8、nhttp:/ / 29CocosNodezCCSpritehttp:/ / 29CCz http:/ 12 / 29zz zhttp:/ / 29http:/ / 29http:/ / 29http:/ / 29- (void) newGame:(id) sender Scene *sc = Scene node; sc addChild:GameLayer node; Director sharedDirector replaceScene: sc; Scene GameLayer ( GameLayer sc) Director ReplaceSceneSlideInRTransitio

9、nReplaceSceneDirector sharedDirector replaceScene: SlideInRTransition transitionWithDuration:1.2f scene:sc;http:/ / 29zzhttp:/ / 29http:/ / 29SysMenuhttp:/ / 29- (id) init self = super init; if (self) / CCSprite *sp = Sprite spriteWithFile:“bg.png“; sp.anchorPoint = CGPointZero; / Z self addChild:sp

10、 z:0 tag:1; / CCMenuItemFont setFontName:“Marker Felt“; CCMenuItemFont setFontSize:25; / 5 CCMenuItemFont setFontSize:25; CCMenuItem *newGame = CCMenuItemFont itemFromString:“ “ target:self selector:selector(newGame:); CCMenuItem *loadGame = CCMenuItemFont itemFromString:“ “ target:self selector:nil

11、; CCMenuItem *GameSttings = CCMenuItemFont itemFromString:“ “ target:self selector:selector(OnSettings:); CCMenuItem *helpGame = CCMenuItemFont itemFromString:“ “ target:self selector:nil; CCMenuItem *quitGame = CCMenuItemFont itemFromString:“ “ target:self selector:selector(onQuit:); / 5 CCMenu *mn

12、 = CCMenu menuWithItems:newGame, loadGame, GameSttings, helpGame, quitGame, nil; / 5 mn alignItemsVertically;/ self addChild:mn z:1 tag:2; return self; itemFromString http:/ 21 / 29Menu- (void) newGame:(id) sender CCScene *sc = CCScene node; / sc addChild:GameLayer node; sc addChild: GameCntrolMenu

13、node; / CCDirector sharedDirector replaceScene: CCSlideInRTransition transitionWithDuration:1.2f scene:sc; - (void) OnSettings:(id) sender CCScene *sc = CCScene node; sc addChild:SettingsLayer node; / CCDirector sharedDirector replaceScene: CCShrinkGrowTransition transitionWithDuration:1.2f scene:sc

14、; SceneGameLayerhttp:/ / 29z - (id) init self = super init; if (self) / CCSprite *sp = CCSpritespriteWithFile:“Space.png“; sp.anchorPoint = CGPointZero; / sp.opacity = 100;self addChild:sp z:0 tag:1; / BMP CCBitmapFontAtlas *lbScore = CCBitmapFontAtlas bitmapFontAtlasWithString:“Time: 0“ fntFile:“fo

15、nt09.fnt“; lbScore.anchorPoint = ccp(1.0, 1.0); lbScore.scale = 0.6; self addChild:lbScorez:1 tag:3; lbScore.position = ccp(310, 450); / CCSpriteSheet *mgr = CCSpriteSheet spriteSheetWithFile:“flight.png“ capacity:5; self addChild:mgrz:0 tag:4; CCSprite *sprite = CCSprite spriteWithTexture:mgr.textu

16、re rect:CGRectMake(0,0,31,30) ; mgr addChild:sprite z:1 tag:5; sprite.scale = 1.1; sprite.anchorPoint = ccp(0, 1); sprite.position = ccp(10, 460); / CCBitmapFontAtlas *lbLife = CCBitmapFontAtlas bitmapFontAtlasWithString:“3“ fntFile:“font09.fnt“; lbLife.anchorPoint = ccp(0.0, 1.0); lbLife.scale = 0.

17、6; self addChild:lbLife z:1 tag:6; lbLife.position = ccp(50, 450); / self schedule:selector(step:) interval:1;/ Action flight = CCSprite spriteWithTexture:mgr.texture rect:CGRectMake(0,0,31,30); http:/ 23 / 29flight.position = ccp(160, 30); flight.scale = 1.6; mgr addChild:flight z:1tag:99; / CCAnim

18、ation *animation = CCAnimation animationWithName:“flight“ delay:0.2f; for(int i=0;isize; i+) ccHashSetBin *bin; for(bin = targets-tablei; bin; ) currentTarget = (tHashElement*) bin-elt; currentTargetSalvaged = NO; if( ! currentTarget-paused ) / The actions ccArray may change while inside this loop.

19、for( currentTarget-actionIndex = 0; currentTarget-actionIndex actions-num; currentTarget-actionIndex+) currentTarget-currentAction = currentTarget-actions-arrcurrentTarget-actionIndex; currentTarget-currentActionSalvaged = NO; currentTarget-currentAction step: dt; if( currentTarget-currentActionSalv

20、aged ) / The currentAction told the node to remove it. To prevent the action from / accidentally deallocating itself before finishing its step, we retained / it. Now that step is done, its safe to release it. currentTarget-currentAction release; else if( currentTarget-currentAction isDone ) currentT

21、arget-currentAction stop; Action *a = currentTarget-currentAction; / Make currentAction nil to prevent removeAction from salvaging it. currentTarget-currentAction = nil; self removeAction:a; currentTarget-currentAction = nil; / bin, at thismoment, is still valid / so it is safe to ask this here (iss

22、ue #490) bin = bin-next; / only delete currentTarget if no actions were scheduled during the cycle (issue #481) if( currentTargetSalvaged http:/ / 20? ? z-(void) step:(ccTime) dt other step: dt; if( other isDone ) other start; z-(void) step:(ccTime) dt other step: dt * speed; z Action EaseInimplemen

23、tation EaseIn -(void) update: (ccTime) t other update: powf(t,rate); endstephttp:/ / 20iPhone OSTouche ?-?- 2 ?-?- 2 ?-?- 2 ?-?- 3 Cocos2D ?-?- 5 ?-?- 6 ?-?- 9 ?-?- 12 ?-?-15 ?-?- 15 ?-?- 16 ?-?- 18(NSSet *)toucheshttp:/ / 18(NSSet *)touchesiPhone OSTouchez z z z(void)touchesBegan:(NSSet *)touches w

24、ithEvent:(UIEvent *)event (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event http:/ / 18NSMutableSet *mutableTouches = touches mutableCopy;NSSet *allTouches =

25、 event allTouches;allTouches count UITouch *touch = allTouches allObjects objectAtIndex:0;UITouch *touch2 = allTouches allObjects objectAtIndex:1;touch tapCount- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event /Get allthe touches. NSSet *allTouches = event allTouches; /Number of touch

26、es on the screen switch (allTouches count) case 1: /Get the first touch. UITouch *touch = allTouches allObjects objectAtIndex:0; switch(touch tapCount) case 1:/Single tap / break; case 2:/Double tap. / break; break; http:/ / 18- (CGFloat)distanceBetweenTwoPoints:(CGPoint)fromPoint toPoint:(CGPoint)t

27、oPoint float x = toPoint.x - fromPoint.x; float y = toPoint.y - fromPoint.y; return sqrt(x * x + y * y); - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event NSSet *allTouches = event allTouches; switch (allTouches count) case 1: /Single touch break; case 2: /Double Touch /Track the init

28、ial distance between two fingers. UITouch *touch1 = allTouches allObjects objectAtIndex:0; UITouch *touch2 = allTouches allObjects objectAtIndex:1; initialDistance = self distanceBetweenTwoPoints:touch1 locationInView:self view toPoint:touch2 locationInView:self view; break; default: break; - (void)

29、touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event NSSet *allTouches = event allTouches; switch (allTouches count) case 1: break; case 2:http:/ / 18 UITouch *touch1 = allTouches allObjects objectAtIndex:0; UITouch *touch2 = allTouches allObjects objectAtIndex:1; /Calculate the distance between

30、 the two fingers. CGFloat finalDistance = self distanceBetweenTwoPoints:touch1 locationInView:self view toPoint:touch2 locationInView:self view; /Check if zoom in or zoom out. if(initialDistance finalDistance) NSLog(“Zoom Out“); / else NSLog(“Zoom In“); break; /Cocos2Dhttp:/ / 18Director sharedDirec

31、tor attachInWindow:window;-(BOOL)attachInWindow:(UIWindow *)window if(self initOpenGLViewWithView:window withFrame:window bounds) return YES; returnNO; -(BOOL)initOpenGLViewWithView:(UIView *)view withFrame:(CGRect)rect ?-/ check if the view is not initialized if(!openGLView_) ?- / EAGLViewopenGLVie

32、w_ = EAGLView alloc initWithFrame:rect pixelFormat:pFormat depthFormat:depthFormat preserveBackbuffer:NO; ?- elsehttp:/ / 18 / set the (new) frame of the glview openGLView_ setFrame:rect; / sharedDispatcher; ?- / ?- UIViewsharedDispatcheropenGLView_ setTouchDelegate: TouchDispatcherOpneGL ESview add

33、Subview:openGLView_;interface Director : NSObject EAGLView *openGLView_;/ internal timer NSTimer *animationTimer; ?- protocol EAGLTouchDelegate - (void)touchesBegan:(NSSet *)toucheswithEvent:(UIEvent *)event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)touchesMoved:(NSS

34、et *)touches withEvent:(UIEvent *)event; - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; endhttp:/ / 18interface TouchDispatcher : NSObject NSMutableArray *touchHandlers; BOOL dispatchEvents; interface EAGLView : UIView private ?- id touchDelegate;- (void)touchesBegan:(NSSet *)

35、touches withEvent:(UIEvent *)event if(touchDelegate) touchDelegate touchesBegan:touches withEvent:event; - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event if(touchDelegate) touchDelegate touchesMoved:touches withEvent:event; - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)e

36、vent if(touchDelegate) touchDelegate touchesEnded:touches withEvent:event; - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event if(touchDelegate) touchDelegate touchesCancelled:touches withEvent:event;http:/ / 18 iprotocol StandardTouchDelegate optional - (void)ccTouchesBegan:(NSSet

37、*)touches withEvent:(UIEvent *)event; - (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event; - (void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event; endiprotocol TargetedTouchDelegate /* Return YES to cl

38、aim the touch. since v0.8 */ - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event; optional / touch updates:http:/ / 18- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event; - (void)ccTouchCancelled:(UITouch *)t

39、ouch withEvent:(UIEvent *)event; endNSSetinterface Layer : CocosNode BOOL isTouchEnabled; BOOL isAccelerometerEnabled; i-(void) setIsTouchEnabled:(BOOL)enabled if( isTouchEnabled != enabled ) isTouchEnabled = enabled; if( isRunning ) if( enabled ) self registerWithTouchDispatcher; / else TouchDispat

40、cher sharedDispatcher removeDelegate:self; i http:/ 10 / 18-(void) onEnter / register parent nodes first / since events are propagated in reverse order if (isTouchEnabled) self registerWithTouchDispatcher; / / then iterate over all the children super onEnter; if( isAccelerometerEnabled ) UIAccelerom

41、eter sharedAccelerometer setDelegate:self; -(void) registerWithTouchDispatcher TouchDispatcher sharedDispatcher addStandardDelegate:self priority:0; -(void) addStandardDelegate:(id) delegate priority:(int)priority TouchHandler *handler = StandardTouchHandler handlerWithDelegate:delegate priority:pri

42、ority; self addHandler:handler; -(void) addTargetedDelegate:(id) delegate priority:(int)priority swallowsTouches:(BOOL)swallowsTouches TouchHandler *handler = TargetedTouchHandler handlerWithDelegate:delegate priority:priority swallowsTouches:swallowsTouches;self addHandler:handler; http:/ / 18iihtt

43、p:/ / 18i i CCTouchDispatcherihttp:/ / 18CCTouchDispatcherinterface Layer : CocosNode TouchDispatcher priority:0;sharedDispatcheraddStandardDelegate:selfhttp:/ / 18http:/ / 18http:/ / 18interface KillingLight : AtlasSprite + (id)KillingLightWithRect:(CGRect)rect spriteManager:(AtlasSpriteManager*)manager; end- (void)o

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 实用文档 > 简明教程

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报