iOS_31_cocos2d_消息调度
来源:互联网 发布:mac卸载自带软件卸载 编辑:程序博客网 时间:2024/06/08 03:00
最终效果如图:
cocos2d V3 只要实现了- (void)update:(CCTime)delta方法,
就会自动调用它,无需手动调用
foreach 或者说for in遍历的时侯,不能增删成员
封装的 子弹类,继承自CCSprite
//// Bullet.h// 31_cocos2D入门//// Created by beyond on 14-9-7.// Copyright (c) 2014年 com.beyond. All rights reserved.// 子弹 成员属性:速度 每秒移动多少#import "CCSprite.h"@interface Bullet : CCSprite// 速度(每秒挪动这个速度)@property (nonatomic, assign) CGPoint velocity;@end
//// Bullet.m// 31_cocos2D入门//// Created by beyond on 14-9-7.// Copyright (c) 2014年 com.beyond. All rights reserved.//#import "Bullet.h"@implementation Bullet// do nothing in cocos2d v3- (id)initWithTexture:(CCTexture *)texture rect:(CGRect)rect{<span style="white-space:pre"></span> if (self = [super initWithTexture:texture rect:rect]) { // 默认会调用update: // [self schedule:@selector(update:) interval:0]; // 取消消息调度 // [self unschedule:<#(SEL)#>]; } return self;}// 路程 = 速度 * 时间// delta 两次刷帧的时间间距- (void)update:(CCTime)delta{ // 每次增加5的x值 // self.position = ccpAdd(self.position, ccp(5, 0)); // s = 速度 * 时间 CGPoint deltaS = ccpMult(_velocity, delta); // 更改位置 self.position = ccpAdd(self.position, deltaS);}@end
主场景
//// HelloWorldScene.h// 31_cocos2D入门//// Created by beyond on 14-9-5.// Copyright com.beyond 2014年. All rights reserved.//// Importing cocos2d.h and cocos2d-ui.h, will import anything you need to start using Cocos2D v3#import "cocos2d.h"#import "cocos2d-ui.h"/** * 主场景 */@interface HelloWorldScene : CCScene+ (HelloWorldScene *)scene;- (id)init;@end
//// HelloWorldScene.m// 31_cocos2D入门//// Created by beyond on 14-9-5.// Copyright com.beyond 2014年. All rights reserved.//#import "HelloWorldScene.h"#import "IntroScene.h"#import "CCAnimation.h"// 子弹#import "Bullet.h"@implementation HelloWorldScene{ CCSprite *_sprite;}#pragma mark - 生命周期+ (HelloWorldScene *)scene{ return [[self alloc] init];}// 在HelloWorldScene中- (id)init{ if (!(self = [super init]) ) return(nil); // 1、场景Node 允许交互 self.userInteractionEnabled = YES; // 2、创建背景颜色为深灰色 CCNodeColor *background = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0.2f green:0.2f blue:0.2f alpha:1.0f]]; [self addChild:background]; // 3、添加一个精灵,并居中 [self addSpriteInCenter]; // 4、右上方,创建一个返回按钮,点击后,返回至上一个场景 [self addBtnOnTopRight]; // 5、重要,开启消息调用 // [self schedule:@selector(update:) interval:0]; // 返回创建好的场景对象return self;}#pragma mark - Enter & Exit//- (void)onEnter{ // 必须总是先调用父类的onEnter方法 [super onEnter]; // In pre-v3, touch enable and scheduleUpdate was called here // In v3, touch is enabled by setting userInteractionEnabled for the individual nodes // Per frame update is automatically enabled, if update is overridden }- (void)onExit{ // 必须总是 最后才调用父类的onExit方法 [super onExit];}#pragma mark - 触摸事件// 用户触摸屏幕,精灵跟随手指移动-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event { CGPoint touchLoc = [touch locationInNode:self]; // 输出触摸点的坐标 CCLOG(@"Move sprite to @ %@",NSStringFromCGPoint(touchLoc)); // 移动精灵到触摸点处 To表示绝对 By表示相对 //CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc]; // 调用精灵的runAction方法执行动作 //[_sprite runAction:actionMove];}// 手抬起处就放一颗bullet,并且让子弹 射出去,并且进行碰撞检测- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{ // 得到触摸点 CGPoint touchLoc = [touch locationInNode:self]; Bullet *bullet = [Bullet spriteWithImageNamed:@"bullet.png"]; bullet.name = @"bullet"; // 设置 子弹出现的位置 bullet.position = touchLoc; // 设置 子弹的速度 bullet.velocity = ccp(500, CCRANDOM_MINUS1_1() * 300); [self addChild:bullet];}// 在场景的消息调度里面,进行碰撞检测- (void)update:(CCTime)delta{ [self boundaryCheck];}- (void)boundaryCheck{ for (int i = 0; i<self.children.count;i++) { CCSprite *child = [self.children objectAtIndex:i]; // 子弹 if ([child.name isEqualToString:@"bullet"]) { // 1.往右边飞 // CGPoint pos = child.position; // pos.x += 5; // child.position = pos; // 2.跟人的碰撞检测 // CCSprite *person = (CCSprite *)[self getChildByName:@"nana" recursively:YES]; if ( CGRectIntersectsRect(child.boundingBox, _sprite.boundingBox) ) { // 移除子弹 [child removeFromParentAndCleanup:YES]; CCLOG(@"碰到了人"); _sprite.opacity = _sprite.opacity*0.8; if (_sprite.opacity < 0.05) { _sprite.position = ccp(-10,-10); } } else if ( !CGRectContainsRect(self.boundingBox, child.boundingBox) ) { // 移除子弹 [child removeFromParentAndCleanup:YES]; CCLOG(@"离开了屏幕"); } } } }#pragma mark - 按钮点击事件- (void)onBackClicked:(id)sender{ // 使用转场动画,切换场景至 IntroScene [[CCDirector sharedDirector] replaceScene:[IntroScene scene] withTransition:[CCTransition transitionPushWithDirection:CCTransitionDirectionRight duration:1.0f]];}#pragma mark - 私有方法// 右上方,创建一个返回按钮,点击后,返回至上一个场景- (void)addBtnOnTopRight{ // 5、右上方,创建一个返回按钮,点击后,返回至上一个场景 CCButton *backButton = [CCButton buttonWithTitle:@"[ Back ]" fontName:@"Verdana-Bold" fontSize:18.0f]; backButton.positionType = CCPositionTypeNormalized; // 屏幕的右上方 注意这里是笛卡尔坐标系,原点在左下方 backButton.position = ccp(0.85f, 0.95f); // 监听点击事件 [backButton setTarget:self selector:@selector(onBackClicked:)]; [self addChild:backButton];}// 添加一个精灵,并设置位置居中- (void)addSpriteInCenter{ // 3、添加一个精灵,并设置位置居中 ColorCircle _sprite = [CCSprite spriteWithImageNamed:@"nana_logo.png"]; _sprite.name = @"nana"; //NSString *fullPath = [[NSBundle mainBundle] pathForResource:@"nana_logo.png" ofType:nil]; //_sprite = [CCSprite spriteWithTexture:[CCTexture textureWithFile:fullPath ] rect:CGRectMake(0, 0, 112, 112) ]; //_sprite.position = ccp(self.contentSize.width/2,self.contentSize.height/2); _sprite.position = ccp(self.contentSize.width*0.85,self.contentSize.height/2); [self addChild:_sprite];}// 为精灵创建一个旋转动作- (void)addRotateAction{ // 4、为精灵创建一个旋转动作,并且调用精灵的runAction方法,重复执行动作 CCActionRotateBy* actionSpin = [CCActionRotateBy actionWithDuration:1.5f angle:360]; [_sprite runAction:[CCActionRepeatForever actionWithAction:actionSpin]];}// 测试sprite属性- (void)testCCSprite{ _sprite.opacity = 125; //_sprite.color = [UIColor redColor];//ccc3(255, 0, 0); _sprite.color = [CCColor colorWithRed:1.0f green:0.2f blue:0.2f alpha:1.0f]; _sprite.flipX = YES; _sprite.flipY = YES; CCAction *action = [CCActionCallFunc actionWithTarget:self selector:@selector(go:)]; [_sprite runAction:action];}// 播放帧动画【赵云】- (void)playFrameAnimatiton{ // 创建精灵,并居中【千万要记得addChild】 _sprite = [CCSprite spriteWithImageNamed:@"1.png"]; _sprite.position = ccp(self.contentSize.width/2,self.contentSize.height/2); // 用来存放所有的帧 NSMutableArray *frames = [NSMutableArray array]; // 加载所有的图片 for (int i = 1; i<= 10; i++) { // 1、拼接图片名 NSString *name = [NSString stringWithFormat:@"%i.png", i]; // 2、根据图片名(或全路径)生成纹理,一个图片对应一个纹理对象 CCTexture *texture = [CCTexture textureWithFile:name]; CGRect retct = CGRectMake(0, 0, texture.contentSize.width, texture.contentSize.height); // 3、根据纹理创建一个精灵帧 CCSpriteFrame *frame = [[CCSpriteFrame alloc]initWithTexture:texture rectInPixels:retct rotated:NO offset:ccp(0, 0) originalSize:retct.size]; // 4、添加精灵帧 到 精灵帧数组中 [frames addObject:frame]; } // 根据【精灵帧数组】创建CCAnimation,参数:每隔0.1秒播放下一张图片 CCAnimation *animation = [CCAnimation animationWithSpriteFrames:frames delay:0.1]; // 根据CCAnimation对象 创建 动作 CCActionAnimate *animate = [CCActionAnimate actionWithAnimation:animation]; CCActionRepeatForever *forever = [CCActionRepeatForever actionWithAction:animate]; [_sprite runAction:forever]; // 重要~~~必须成为场景的子Node [self addChild:_sprite];}@end
1 0
- iOS_31_cocos2d_消息调度
- 消息调度
- iOS_31_cocos2d_纹理打包(TexturePacker)
- iOS_31_cocos2d_粒子系统ParticleSystem
- iOS_31_cocos2d_微信飞机
- Windows消息调度机制
- iOS_31_cocos2d_图层CCLayer_加速计
- 作业调度Quartz与消息中间件MSMQ
- Android 消息机制 进程调度问题
- 消息调度中心的学习资料汇总
- 自动消息调度器的实现
- 使用优先队列PriorityBlockingQueue进行消息调度
- rabbitMQ 消息确认与公平调度消费者
- mfc 通过消息映射调度消息 虚函数 WindowProc
- SNMP协议详解—消息调度与处理
- Windows消息调度机制和线程同步控制
- rabbitMQ学习笔记(三) 消息确认与公平调度消费者
- Windows消息调度机制和线程同步控制
- 时间管理
- [windows C/C++]面试准备(二)多线程面试题1
- hbase shell 命令
- 高精度计算bign类
- 模拟CSDN请求,做一点事
- iOS_31_cocos2d_消息调度
- Java问题排查常用linux命令
- 黑马程序员-反射调用其他类main方法
- Java内存溢出
- hdu 4990 Reading comprehension(BestCoder Round #8 1002)
- ZOJ 3811 Untrusted Patrol(dfs+并查集)
- Java自带的性能监测工具用法简介——jstack、jconsole、jinfo、jmap、jdb、jsta、jvisualvm .
- 敏捷是把开发搞死吗?
- 关于mysql