首先拉一個blue增加至GamePlayScene場景之中,並且設定Doc root var: _bluePlayer作為程式碼中的變數。
程式實作流程:
增加場景的點擊事件
- (void)didLoadFromCCB {
self.userInteractionEnabled = YES;
// =======================
// 程式省略...
// =======================
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
}
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
}
增加一個_bluePlayer變數,連結SpriteBuilder的藍色小人。
@interface GamePlayScene () {
// =======================
// 程式省略...
// =======================
CCSprite *_bluePlayer;
}
@end
實作畫面點擊時的_bluePlayer移動,當遊戲時間為0時就無法移動。
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (maxTimer > 0.0f) {
CCActionMoveBy *moveAction = [CCActionMoveBy actionWithDuration:0.5
position:ccp(10, 0)];
[_bluePlayer runAction:moveAction];
}
}
GamePlayScene.m 程式碼如下
#import "GamePlayScene.h"
@interface GamePlayScene () {
CGFloat maxTimer;
CCLabelTTF *_timer;
CCSprite *_bluePlayer;
}
@end
@implementation GamePlayScene
- (void)didLoadFromCCB {
self.userInteractionEnabled = YES;
maxTimer = 90.0f;
}
- (void)update:(CCTime)delta {
maxTimer -= delta;
if (maxTimer > 0.0f) {
_timer.string = [NSString stringWithFormat:@"%.0f", maxTimer];
} else {
_timer.string = @"0";
}
}
- (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
if (maxTimer > 0.0f) {
CCActionMoveBy *moveAction = [CCActionMoveBy actionWithDuration:0.5 position:ccp(10, 0)];
[_bluePlayer runAction:moveAction];
}
}
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
}
- (void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
}
@end
結果如下: