gpt4 book ai didi

objective-c - 我怎样才能使分数等于在 spritekit 中玩游戏的时间(以秒为单位)

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:11 27 4
gpt4 key购买 nike

好的,所以我尝试了 orbivoid 教程,但我希望耗时(以秒为单位)是玩家的分数,而不是重生的敌人数量。关于 NSTimer、NSTimeInterval 和 CFTimeInterval,我尝试了很多东西,但不用说,我失败了。有人可以帮我解决这个问题——要添加什么代码、提示或其他什么?顺便说一句,这是 orbivoid 教程中的 GameScene.m。先感谢您!

 @implementation GameScene
{
BOOL _dead;
SKNode *_player;
NSMutableArray *_enemies;
SKLabelNode *_scoreLabel;
}


-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */

self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];


self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);
self.physicsWorld.contactDelegate = self;


_enemies = [NSMutableArray new];

_player = [SKNode node];
SKShapeNode *circle = [SKShapeNode node];

circle.path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(-10, -10, 20, 20)].CGPath;`
circle.fillColor = [UIColor blueColor];
circle.glowWidth = 5;

SKEmitterNode *trail = [SKEmitterNode orb_emitterNamed:@"Trail"];
trail.targetNode = self;
trail.position = CGPointMake(CGRectGetMidX(circle.frame), CGRectGetMidY(circle.frame));
_player.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
_player.physicsBody.mass = 100000;
_player.physicsBody.categoryBitMask = CollisionPlayer;
_player.physicsBody.contactTestBitMask = CollisionEnemy;




[_player addChild:trail];

_player.position = CGPointMake(size.width/2, size.height/2);

[self addChild:_player];


}
return self;
}

- (void)didMoveToView:(SKView *)view
{
[self performSelector:@selector(spawnEnemy) withObject:nil afterDelay:1.0];
}

-(void)spawnEnemy
{
[self runAction:[SKAction playSoundFileNamed:@"Spawn.wav" waitForCompletion:NO]];
SKNode *enemy = [SKNode node];

SKEmitterNode *trail = [SKEmitterNode orb_emitterNamed:@"Trail"];
trail.targetNode = self;
trail.particleScale /= 2;
trail.position = CGPointMake(10, 10);
trail.particleColorSequence = [[SKKeyframeSequence alloc] initWithKeyframeValues:@[


[SKColor redColor],
[SKColor colorWithHue:0.1 saturation:.5 brightness:1 alpha:1],
[SKColor redColor],
] times:@[@0, @0.02, @0.2]];




[enemy addChild:trail];
enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:6];
enemy.physicsBody.categoryBitMask = CollisionEnemy;
enemy.physicsBody.allowsRotation = NO;

enemy.position = CGPointMake(50, 50);

[_enemies addObject:enemy];
[self addChild:enemy];

if(!_scoreLabel) {
_scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Courier-Bold"];

_scoreLabel.fontSize = 200;
_scoreLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
_scoreLabel.fontColor = [SKColor colorWithHue:0 saturation:0 brightness:1 alpha:0.5];
[self addChild:_scoreLabel];
}
_scoreLabel.text = [NSString stringWithFormat:@"%02d", _enemies.count];



// Next spawn
[self runAction:[SKAction sequence:@[
[SKAction waitForDuration:5],
[SKAction performSelector:@selector(spawnEnemy) onTarget:self],
]]];
}


-(void)dieFrom: (SKNode*)killingEnemy
{
_dead = YES;

SKEmitterNode *explosion = [SKEmitterNode orb_emitterNamed:@"Explosion"];
explosion.position = _player.position;
[self addChild:explosion];

[explosion runAction:[SKAction sequence:@[
[SKAction playSoundFileNamed:@"Explosion.wav" waitForCompletion:NO],
[SKAction waitForDuration:0.4],
[SKAction runBlock:^{
// TODO: Revove these more nicely
[killingEnemy removeFromParent];
[_player removeFromParent];
}],
[SKAction waitForDuration:0.4],
[SKAction runBlock:^{
explosion.particleBirthRate = 0;
}],
[SKAction waitForDuration: 1.2],

[SKAction runBlock:^{
ORBMenuScene *menu = [[ORBMenuScene alloc] initWithSize:self.size];
[self.view presentScene:menu transition:[SKTransition doorsCloseHorizontalWithDuration:0.4]];
}],
]]];

}


-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self touchesMoved:touches withEvent:event];

}

-(void)touchesMoved: (NSSet *) touches withEvent:(UIEvent *)event
{
[_player runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self] duration:.01]];
}

-(void)update:(CFTimeInterval)currentTime
{
CGPoint playerPos = _player.position;

for(SKNode *enemyNode in _enemies)
{
CGPoint enemyPos = enemyNode.position;

/* Uniform speed: */
CGVector diff = TCVectorMinus(playerPos, enemyPos);
CGVector normalized = TCVectorUnit(diff);
CGVector force = TCVectorMultiply(normalized, 4);

[enemyNode.physicsBody applyForce:force];
}

_player.physicsBody.velocity = CGVectorMake (0, 0);


}
-(void)didBeginContact:(SKPhysicsContact *)contact
{
if(_dead)
return;

[self dieFrom:contact.bodyB.node];
contact.bodyB.node.physicsBody = nil;
}

@end

最佳答案

简单版本:创建一个分数整数(需要 _block,因为您将在 block 中使用它)

__block int score123=0;

运行一个增加整数的 Action ,然后等待 1.0 秒再做一次。这个序列永远重复。为操作指定键“scoreIncrease”,以便稍后停止。

SKAction *scoreIncrease = [SKAction repeatActionForever:[SKAction sequence:@[[SKAction runBlock:^{ score123++; }],[SKAction waitForDuration:1.0f]]]];
[self runAction:scoreIncrease withKey:@"scoreIncrease"];

当你想停止计数时,使用这个:

[self removeActionForKey:@"scoreIncrease"];

关于objective-c - 我怎样才能使分数等于在 spritekit 中玩游戏的时间(以秒为单位),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21647505/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com