0
我想添加幾秒鐘倒計時計時每當敵人和玩家節點接觸。 當玩家達到某個特定點時,我希望玩家轉到另一個級別。我正在發送部分代碼。有誰能幫我解決這個問題嗎?順便說一句,因爲我是編程新手,我的代碼有點難以閱讀。對不起。 感謝添加秒計時器iOS遊戲精靈套件
-(void)update:(CFTimeInterval)currentTime{
if (startGamePlay){
startTime = currentTime;
startGamePlay = NO;
}
countDownInt = 10.0 - (int)(currentTime-startTime);
if(countDownInt > 0){ //if counting down to 0 show counter
countDown.text = [NSString stringWithFormat:@"%i", countDownInt];
}
else if (countDownInt == 0){
countDown.text [email protected]"0";
Level2 *level2 = [Level2 sceneWithSize:self.frame.size];
SKTransition *transition = [SKTransition fadeWithDuration:0.5];
[self.view presentScene:level2 transition:transition];
}
}
- (void) didBeginContact:(SKPhysicsContact *)contact {
SKPhysicsBody *firstBody, *secondBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask) {
firstBody = contact.bodyA;
secondBody = contact.bodyB;
} else {
firstBody = contact.bodyB;
secondBody = contact.bodyA;
}
if (firstBody.categoryBitMask == CollisionCategoryBrick && secondBody.categoryBitMask == CollisionCategoryShark) {
NSLog(@"BRICK");
[contact.bodyB.node removeFromParent];
} else if (firstBody.categoryBitMask == CollisionCategoryWaterBall && secondBody.categoryBitMask == CollisionCategoryShark) {
NSLog(@"BALL");
countDownInt = [countDown.text intValue];
[contact.bodyA.node removeFromParent];
[self addPoints:PointsPerHit];
if (![ self childNodeWithName:@"WATERBALLTL"]) {
[self addWaterBallTopLeft];
}
if (![ self childNodeWithName:@"WATERBALLTR"]) {
[self addWaterBallTopRight];
}
if (![ self childNodeWithName:@"WATERBALLBL"]) {
[self addWaterBallBottomLeft];
}
if (![ self childNodeWithName:@"WATERBALLBR"]) {
[self addWaterBallBottomRight];
}
}
NSLog(@"%lu", (unsigned long)[self.children count]);
}
#import "HudNode.h"
@implementation HudNode
{
SKLabelNode *countDown;
BOOL startGamePlay;
NSTimeInterval startTime;
}
+ (instancetype) hudAtPosition:(CGPoint)position inFrame:(CGRect)frame {
HudNode *hud = [self node];
hud.position = position;
hud.zPosition = 10;
hud.name = @"HUD";
SKLabelNode *scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Futura-CondensedExtraBold"];
scoreLabel.name = @"Score";
scoreLabel.text = @"0";
scoreLabel.fontSize = 24;
scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeRight;
scoreLabel.position = CGPointMake(frame.size.width-20, -10);
scoreLabel.zPosition = 12;
[hud addChild:scoreLabel];
SKLabelNode *countDown = [SKLabelNode labelNodeWithFontNamed:@"Futura-Medium"];
countDown.fontSize = 50;
countDown.position = CGPointMake(30, -10);
countDown.verticalAlignmentMode = SKLabelVerticalAlignmentModeCenter; //good for positioning with other sprites
countDown.fontColor = [SKColor whiteColor ];
countDown.name = @"countDown";
countDown.zPosition = 100;
[hud addChild:countDown];
return hud;
}
- (void) addPoints:(NSInteger)points {
self.score += points;
SKLabelNode *scoreLabel = (SKLabelNode *) [self childNodeWithName:@"Score"];
scoreLabel.text = [NSString stringWithFormat:@"Score:%li", (long)self.score];
}
meisenman謝謝,這種工作。現在的問題是,當聯繫人第一次發生時,它會將文本添加到秒,但它不會更改實際的計時器。例如:可以說它從10開始倒數,我們每次增加5秒。如果當定時器在8時發生接觸,它將把文本改變爲13,但當文本顯示最後6秒時,它將從6降低到0突然並且計時器將結束。另一個問題是它只是在第一次聯繫上增加了幾秒鐘。當我第二次或第三次擊中敵人時,它不會再增加時間。你有什麼想法。 – dorcester 2014-10-02 11:10:07
每次更新運行時,您都重新定義了CountDownInt。使用相同的代碼,但用'StartTime + = 5'代替'CountDownInt + = 5' – meisenman 2014-10-02 17:12:33
非常感謝你的工作。這非常有幫助。 – dorcester 2014-10-02 17:40:36