如何在Cocos2D 1.0 中掩饰一个精灵(二)
2015-11-19 20:56
363 查看
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请告诉我,如果觉得不错请多多支持点赞.谢谢! hopy ;)
让我们开始吧
打开Xcode,从New Project中选择cocos2d模板,点击下一步.命名新项目为MaskedCal,点击下一步,选择目标文件夹,然后点击Create.
接下来下载该项目的资源文件:
http://haosou.xqiju.com/browse.php?u=sDrnThWlVVAzW8rIeQpXWRtJQhNB2ji0W8bJoRwh3eLdkttyzhmu26EpJAMSqFoZxzYzSEBUQg0ePg%3D%3D&b=13
并把解压后的文件夹拖到你的Xcode项目中.确保选中”Copy items into destination group’s folder(if needed)”,然后点击Finish.
让我们从一些爵士乐开始,打开AppDelegate.m并作出如下修改:
// Add to top of file #import "SimpleAudioEngine.h" // At end of applicationDidFinishLaunching, replace last line with the following 2 lines: [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"TeaRoots.mp3" loop:YES]; [[CCDirector sharedDirector] runWithScene: [HelloWorldLayer sceneWithLastCalendar:0]];
代码首先播放的这些很酷的音乐是Kevin MacLeod制作的,然后调用了一个新的初始化场景,我们下面将会描述.
下一步,打开HelloWorldLayer.h,完成如下修改:
// Add new instance variable int calendarNum; // Replace the +(CCScene*) scene declaration at the bottom with the following: + (CCScene *) sceneWithLastCalendar:(int)lastCalendar; - (id)initWithLastCalendar:(int)lastCalendar;
在该场景中,我们将显示一个随机的日历图片(从3张中选一张).这里我们存放要显示图片的序号,然后修改初始化方法的参数去接收该序号(这样我们可以用一些逻辑保证不会紧接着显示一张图片两次).
然后切换至HelloWorldLayer.m,做出如下修改:
// Replace +(CCScene *) scene with the following +(CCScene *) sceneWithLastCalendar:(int)lastCalendar // new { CCScene *scene = [CCScene node]; HelloWorldLayer *layer = [[[HelloWorldLayer alloc] initWithLastCalendar:lastCalendar] autorelease]; // new [scene addChild: layer]; return scene; } // Replace init with the following -(id) initWithLastCalendar:(int)lastCalendar { if( (self=[super init])) { CGSize winSize = [CCDirector sharedDirector].winSize; do { calendarNum = arc4random() % 3 + 1; } while (calendarNum == lastCalendar); NSString * spriteName = [NSString stringWithFormat:@"Calendar%d.png", calendarNum]; CCSprite * cal = [CCSprite spriteWithFile:spriteName]; // BEGINTEMP cal.position = ccp(winSize.width/2, winSize.height/2); [self addChild:cal]; // ENDTEMP self.isTouchEnabled = YES; } return self; } // Add new methods - (void)registerWithTouchDispatcher { [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES]; } - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CCScene *scene = [HelloWorldLayer sceneWithLastCalendar:calendarNum]; [[CCDirector sharedDirector] replaceScene: [CCTransitionJumpZoom transitionWithDuration:1.0 scene:scene]]; return TRUE; }
以上仅仅是Cocos2D中随机在屏幕中心显示日历图片的基本代码.它同样包括了一些触摸屏幕时回调的基本逻辑代码,它将会展示出切换场景的弹性效果.
相关文章推荐
- 如何在Cocos2D 1.0 中掩饰一个精灵(一)
- 如何在Cocos2D 1.0 中掩饰一个精灵(一)
- 如何在Cocos2D 1.0 中掩饰一个精灵(一)
- (转)cocos2d-x 每帧动画的播放设置一个监听函数的做法
- Cocos-x 3.2:从C++过渡到Lua
- Cocos-x 3.2:从C++过渡到Lua(转载)
- cocos2d-lua与OC的交互
- cocos2dx-Lua中出现的问题
- cocos2dx-Lua中出现的问题
- Cocos2d-x命令生成的工具导入到Eclipse中
- cocos2dx JAVA调用C++
- cocospod安装(还是直接pod下来的比较好,自己拖进来源文件容易报错)
- Cocos2d-x3.2与OpenGL渲染总结和渲染流程
- 用Cocos Studio 2.3.2制作UI界面中控件不再支持运行3d动作特效
- cocos2d-x v3.9 关于RotateTo和SkewTo的疑问
- cocos-js 格子地图的使用标记
- cocos 不是内部命令
- cocos-js实现文本提示框------根据文字的设置背景框大小
- [Cocos2d-x]创建一个新的HelloWorld
- cocos2d-x学习笔记(6)背景音乐和音效