跟踪某个类别的 SKNode
Posted
技术标签:
【中文标题】跟踪某个类别的 SKNode【英文标题】:Keeping track of SKNodes for a category 【发布时间】:2017-11-15 08:21:36 【问题描述】:我有一个函数可以将敌人添加到我的场景中,并按时间间隔调用
如何跟踪场景中的敌人数量以限制我在每个关卡中拥有的敌人数量?
**在我的更新函数中**
CFTimeInterval timeSinceLastEnemy = currentTime - self.lastEnemyUpdateTime;
self.lastEnemyUpdateTime = currentTime;
if (timeSinceLastEnemy > 1) // more than a second since last update
timeSinceLastEnemy = 1.0 / 60.0;
self.lastEnemyUpdateTime = currentTime;
[self spwanEnemyWithTime:timeSinceLastEnemy];
**定时器和添加敌人的方法**
- (void)spwanEnemyWithTime:(CFTimeInterval)timeSinceLast
self.lastEnemySpawn += timeSinceLast;
if (self.lastEnemySpawn > 0.6)
self.lastEnemySpawn = 0;
[self spawnEnemy];
-(void) spawnEnemy
SKSpriteNode *enemy = [SKSpriteNode spriteNodeWithImageNamed: enemySprite];
int minX = 5;
int maxX = self.frame.size.width;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
// Create the enemy slightly off-screen along the upper edge,
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(enemy.size.height - 10 , enemy.size.width)];
enemy.physicsBody.dynamic = YES;
enemy.physicsBody.categoryBitMask = enemyCategory;
enemy.physicsBody.contactTestBitMask = bulletCategory;
enemy.physicsBody.collisionBitMask = 0;
// and along a random position along the X axis as calculated above
enemy.position = CGPointMake(actualX, self.frame.size.height + enemy.size.height);
[self addChild:enemy];
enemy.xScale = 0.2;
enemy.yScale = 0.2;
enemy.zPosition = 4;
// Create the actions
SKAction * actionMove = [SKAction moveToY:(0 - enemy.size.height) duration:4];
SKAction * actionMoveDone = [SKAction removeFromParent];
[enemy runAction:[SKAction sequence:@[actionMove, actionMoveDone]]];
【问题讨论】:
你试过什么?向我们展示添加敌人的代码。向我们展示您的计时器的代码。您需要提供足够的信息让人们帮助您,否则您将被标记为“过于宽泛”。 欢迎来到SO,请看***.blog/2010/07/07/… @RonMyschuk 我已经编辑了我的帖子 【参考方案1】:我承认我无法理解你在用双计时器做什么,所以我会假设这部分工作如你所愿。对于敌人的计数,这难道不能像在你的 spawnEnemy 上增加一个计数器并在敌人从场景中移除时减少计数器一样简单吗?
你必须仔细检查我的 Obj-c 我有一段时间没用过了。
int enemyCount = 0;
-(void) spawnEnemy
enemyCount += 1
...
// Create the actions
SKAction *actionMove = [SKAction moveToY:(0 - enemy.size.height) duration:4];
SKAction *actionMoveDone = [SKAction removeFromParent];
SKAction *decrementCount = [SKAction runBlock: ^(void)
enemyCount -= 1;
];
[enemy runAction:[SKAction sequence:@[actionMove, actionMoveDone, decrementCount]]];
仅供参考,您的方法“spwanEnemyWithTime”中有错字
【讨论】:
以上是关于跟踪某个类别的 SKNode的主要内容,如果未能解决你的问题,请参考以下文章