iPhoneアプリ開発講座を受講する その7
[iPhone][cocos2d][Objective-C]
3連休の初日も終日、某社で開催される講座を受講して参りました。
今回もつらつらと。
背景とかボタンとかアクションとか
背景を白にしてみる
CCLayerColor *backgroundLayer=[CCLayerColor layerWithColor:ccc4(255, 255, 255, 255)]; [self addChild:backgroundLayer];
左矢印&右矢印ボタンをCCMenuで作ってみる
CCMenuItemImage *leftButton=[CCMenuItemImage itemFromNormalImage:@"texture/arrow_left.png" selectedImage:@"texture/arrow_left.png" target:self selector:@selector(doLeft)]; [leftButton setPosition:ccp(40, 40)]; CCMenuItemImage *rightButton=[CCMenuItemImage itemFromNormalImage:@"texture/arrow_right.png" selectedImage:@"texture/arrow_right.png" target:self selector:@selector(doRight)]; [rightButton setPosition:ccp(440, 40)]; CCMenu *menu=[CCMenu menuWithItems:leftButton,rightButton, nil]; [menu setAnchorPoint:ccp(0, 0)]; [menu setPosition:ccp(0, 0)]; [self addChild:menu];
既に画面上に適当なspriteを配置した前提で・・・
次にやってみたのは、ボタン押下時のアクションをつけてみた。
ボタン押下時のアクション
-(void)doLeft { while (!actionOK_) { return; } if (50<hogeSprite_.position.x-50) { CCCallFunc *func1=[CCCallFunc actionWithTarget:self selector:@selector(actionNG)]; CCJumpTo *jumpTo=[CCJumpTo actionWithDuration:0.3f position:ccp(hogeSprite_.position.x-50, 180) height:100 jumps:1]; CCCallFunc *func2=[CCCallFunc actionWithTarget:self selector:@selector(actionOK)]; CCSequence *s=[CCSequence actions:func1,jumpTo,func2,nil]; [hogeSprite_ runAction:s]; } } -(void)doRight { while (!actionOK_) { return; } CCMoveTo *moveTo; if (480>hogeSprite_.position.x+70) { CCCallFunc *func1=[CCCallFunc actionWithTarget:self selector:@selector(actionNG)]; moveTo=[CCMoveTo actionWithDuration:0.35f position:ccp(hogeSprite_.position.x+70, 180)]; id ease = [CCEaseInOut actionWithAction:moveTo rate:2]; CCCallFunc *func2=[CCCallFunc actionWithTarget:self selector:@selector(actionOK)]; CCSequence *s=[CCSequence actions:func1,ease,func2, nil]; [hogeSprite_ runAction:s]; } }
上記の中で気にした点は、ボタンを連続押下した場合に、
現在動作中のアクションに影響を与えてしまったこと。
そこで、アクション中は操作を受け付け無いようにしてみた。
それがこの部分であり、
while (!actionOK_) { return; }
この部分である。
CCCallFunc *func1=[CCCallFunc actionWithTarget:self selector:@selector(actionNG)]; 適当なアクションをさせて〜♪ CCCallFunc *func2=[CCCallFunc actionWithTarget:self selector:@selector(actionOK)]; CCSequence *s=[CCSequence actions:func1,適当なアクションfunc2, nil];
他のアクションを適当に。
回転しまーす
CCRotateTo *rotate=[CCRotateTo actionWithDuration:0.5f angle:180]; CCRotateBy *rotate=[CCRotateBy actionWithDuration:0.5f angle:360];
遅延させまーす
CCDelayTime *delay=[CCDelayTime actionWithDuration:2.0f];
複数のアクションを同時に動かしまーす
CCSpawn *s=[CCSpawn actions:moveTo,rotate, nil];
パラパラ漫画みたいなもんだ
** お家で打ち合わせ? CCSpriteBatchNode *hogeBatchNode=[CCSpriteBatchNode batchNodeWithFile:@"texture/hoge_walk.png"]; [self addChild:hogeBatchNode]; hogeSprite_=[CCSprite spriteWithBatchNode:hogeBatchNode rect:CGRectMake(0, 0, 126/2, 190/2)]; [hogeSprite_ setPosition:ccp(80, 180)]; [hogeBatchNode addChild:hogeSprite_]; 歩いているようにみえてきた。 **パラパラ NSMutableArray *walkFrames=[NSMutableArray array]; CCSpriteFrame *frame1=[CCSpriteFrame frameWithTexture:misodaBatchNode.texture rect:CGRectMake(0, 0, 126/2, 190/2)]; CCSpriteFrame *frame2=[CCSpriteFrame frameWithTexture:misodaBatchNode.texture rect:CGRectMake(128/2, 0, 126/2, 184/2)]; CCSpriteFrame *frame3=[CCSpriteFrame frameWithTexture:misodaBatchNode.texture rect:CGRectMake(256/2, 0, 126/2, 182/2)]; CCSpriteFrame *frame4=[CCSpriteFrame frameWithTexture:misodaBatchNode.texture rect:CGRectMake(384/2, 0, 126/2, 194/2)]; [walkFrames addObject:frame1]; [walkFrames addObject:frame2]; [walkFrames addObject:frame3]; [walkFrames addObject:frame4]; CCAnimation *animation=[CCAnimation animationWithFrames:walkFrames delay:0.05f]; CCAnimate *animate=[CCAnimate actionWithAnimation:animation]; CCRepeatForever *walk=[CCRepeatForever actionWithAction:animate]; [hogeSprite_ runAction:walk];