Keeping track of SKNodes for a category - ios

I have a function that adds enemies to my scene that get called by an timing interval
How can I keep track of the number of enemies on the Scene to limit the amount of enemies I have in each level?
**In my update function **
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];
**The timer and The method to add enemies **
- (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]]];
}

I confess I couldn't grasp what you are doing with the double timers, so I will assume that portion works as you want it to. for the enemy count couldn't this be as simple as incrementing a counter on your spawnEnemy and decrementing the counter when the enemy removes itself from the scene?
You'll have to double check my Obj-c I haven't used it in a while.
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]]];
}
FYI you have a typo in your method "spwanEnemyWithTime"

Related

SpriteKit moving Nodes faster with time

I want to spawn the Objects faster with time, for e.g. I got a Object added every 1.5 seconds. This Object is moving to X = -100 with Duration = 5. After 30 seconds it should move faster.
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
.....
SKAction * Spawn = [SKAction performSelector:#selector(Enemy) onTarget:self];
SKAction * Delay = [SKAction waitForDuration:1.5];
SKAction * SpawnThenDelay = [SKAction sequence:#[Spawn, Delay]];
SKAction * SpawnThenDelayForever = [SKAction repeatActionForever:SpawnThenDelay];
[self runAction:SpawnThenDelayForever];
.....
}
- (void)Enemy {
Enemy = [SKSpriteNode spriteNodeWithImageNamed:#"Enemy.png"];
Enemy.size = CGSizeMake(85, 85);
Enemy.zPosition = 2;
Enemy.name = #"Enemy";
Enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(50, 50)];
Enemy.physicsBody.dynamic = NO;
Enemy.physicsBody.allowsRotation = NO;
Enemy.physicsBody.usesPreciseCollisionDetection = YES;
Enemy.physicsBody.restitution = 0;
Enemy.physicsBody.categoryBitMask = EnemyCategory;
Enemy.physicsBody.collisionBitMask = StoneCategory;
Enemy.physicsBody.contactTestBitMask = StoneCategory;
Enemy.position = CGPointMake(self.frame.size.width * 1.25, self.frame.size.height / 2.2);
SKAction * actionMove = [SKAction moveToX:-100 duration:5];
SKAction * actionMoveDone = [SKAction removeFromParent];
[Enemy runAction:[SKAction repeatActionForever:[SKAction sequence:#[actionMove,actionMoveDone]]]];
[self addChild:Enemy];
}
every SKAction has a property speed and by defult every action runs with a speed of 1 so you can increase speed property acc to your game situation
You could store your enemy speed as global variable and add a NSTimer with a 30 seconds interval and every 30 seconds, the timer executes a function that adds a certain value to the speed of the enemy. But make sure there's a speed limit otherwise it will be too fast.
MyScene.h:
#import <SpriteKit/SpriteKit.h>
#interface MyScene : SKScene {
NSTimer *timer;
int SpeedVar;
int MaxSpeed;
int IncreaseSpeedValue;
}
#end
MyScene.m:
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
timer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:#selector(increaseSpeed) userInfo:nil repeats:YES];
SpeedVar = -100; //Normal speed of your enemy
MaxSpeed = -300; //Maximum speed of your enemy
IncreaseSpeedValue = 15; //Increase speed by this value every 30sec
}
return self;
}
-(void)increaseSpeed {
if (SpeedVar > MaxSpeed)
SpeedVar -= IncreaseSpeedValue;
else {
[timer invalidate];
timer = nil;
}
}

self.children count doesn't give correct number

I have this method that is supposed to loop through my nodes and check how many tubes I have in my parent, and if it is less than 6, I should add more. The first 6 work, but after they are removed by the remove action [self.children count] still reports that they're are 6 tubes still there. It logs "Tube Amount: 6/6" even though on screen the node count is at 0. I need an accurate way to see how many nodes I have. here is my code:
-(void) checkTubes {
int amt = 0;
SKSpriteNode *last;
for(SKSpriteNode *node in self.children)
if(node.size.height > 100) {
last = node;
amt++;
}
NSLog(#"Tube Amount: %i/%i", amt, [self.children count]);
if(amt < 6) {
for(SKSpriteNode *node in self.children)
if(node.size.height > 100)
last = node;
SKSpriteNode *tube1 = [SKSpriteNode spriteNodeWithImageNamed:#"tube"];
SKSpriteNode *tube2 = [SKSpriteNode spriteNodeWithImageNamed:#"tube2"];
int x1 = 155;
int y1 = -100;
int x2 = x1;
int y2 = y1+tube1.size.height + 68;
if(last != (id)[NSNull null]) {
x1 = last.position.x + x1;
x2 = x1;
}
tube1.position = CGPointMake(x1, y1);
tube2.position = CGPointMake(x2, y2);
[self addChild:tube1];
[self addChild:tube2];
SKAction *actionMove1 = [SKAction moveTo: CGPointMake(tube1.position.x - 1600, tube1.position.y) duration: 15.55f];
SKAction *actionMove2 = [SKAction moveTo: CGPointMake(tube2.position.x - 1600, tube2.position.y) duration: 15.55f];
SKAction *actionMoveDone = [SKAction removeFromParent];
[tube1 runAction:[SKAction sequence:#[actionMove1, actionMoveDone]]];
[tube2 runAction:[SKAction sequence:#[actionMove2, actionMoveDone]]];
}
}
You can not use the same action for multiple nodes without copying the action. This might fix it:
SKAction *actionMoveDone = [SKAction removeFromParent];
[tube1 runAction:[SKAction sequence:#[actionMove1, actionMoveDone]]];
// 2nd and any following use of the same action must be a 'copy'
[tube2 runAction:[SKAction sequence:#[actionMove2, [actionMoveDone copy]]]];

Changing Sprite Spawns

Here is my code that I m using below and i want it to spawn from the bottom of the x-axis in portrait mode right now in spawns from the top in portrait mode how do i edit the code to do this?
- (void)addBalloon {
// Create sprite
SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:#"balloon"];
// Determine where to spawn the monster along the X axis
int minX = monster.size.width / 2;
int maxX = self.frame.size.width - monster.size.width / 2;
int rangeX = maxX - minX;
int actualX = (arc4random_uniform(rangeX)) + minX;
// Create the monster slightly off-screen along the top,
// and along a random position along the X axis as calculated above
monster.position = CGPointMake(actualX, self.frame.size.height +
monster.size.height/2);
[self addChild:monster];
// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, -
monster.size.height/2) duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[monster runAction:[SKAction sequence:#[actionMove, actionMoveDone]]];
}
I'm not sure what you are asking here. You want to spawn a sprite on the bottom of the X-axis?
In case it is what I think you are asking, you only have to switch:
monster.position = CGPointMake(actualX, self.frame.size.height + monster.size.height/2);
&&
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, - monster.size.height/2) duration:actualDuration];
with:
monster.position = CGPointMake(actualX, - monster.size.height/2);
&&
SKAction * actionMove = [SKAction moveTo:CGPointMake(actualX, self.frame.size.height + monster.size.height/2) duration:actualDuration];

Moving background stops repeating on Spritekit

This is my code to make my background repeat, just one background and it should be repeating forever:
SKTexture* bgTexture = [SKTexture textureWithImageNamed:#"nightbackground"];
bgTexture.filteringMode = SKTextureFilteringNearest;
SKAction* movebgSprite = [SKAction moveByX:-bgTexture.size.width*2 y:0 duration:0.1 * bgTexture.size.width*2];
SKAction* resetbgSprite = [SKAction moveByX:bgTexture.size.width*2 y:0 duration:0];
SKAction* movebgSpritesForever = [SKAction repeatActionForever:[SKAction sequence:#[movebgSprite, resetbgSprite]]];
for( int i = 0; i < 2 + self.frame.size.width / ( bgTexture.size.width * 2 ); ++i ) {
SKSpriteNode* sprite = [SKSpriteNode spriteNodeWithTexture:bgTexture];
[sprite setScale:1.0];
sprite.zPosition = -20;
sprite.position = CGPointMake(self.size.width/2, self.size.height/2);
[sprite runAction:movebgSpritesForever];
[self addChild:sprite];
But it does not repeat forever, it stops after a while. What might be wrong with it?
This case will work in landscape orientation and have the view repeat scrolling infinitely to the left (-x direction). You create two instances of the background SKSPriteNode and place them next to each other. When the first one scrolls completely off the left side of the screen, it is placed to the right of the second background sprite node.
in didMoveToView:
for (int i = 0; i < 2; i++) {
SKSpriteNode *bg = [SKSpriteNode spriteNodeWithImageNamed:#"background"];
bg.anchorPoint = CGPointZero;
bg.position = CGPointMake(i * bg.size.width, 0);
bg.name = #"starBackground";
[self addChild:bg];
}
in the update: loop
[self enumerateChildNodesWithName:#"background" usingBlock:^(SKNode *node, BOOL *stop) {
SKSpriteNode *bg = (SKSpriteNode *)node;
bg.position = CGPointMake(bg.position.x - 1.5, bg.position.y);
if (bg.position.x <= -bg.size.width) {
bg.position = CGPointMake(bg.position.x + bg.size.width * 2, bg.position.y);
}
}];

Sprite wont spawn [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
This code spawns a monster, but no enemy.
I expect an enemy to be spawned, why doesn't it?
#import "MyScene.h"
#import "GameOverScene.h"
static const uint32_t projectileCategory = 0x1 << 0;
static const uint32_t monsterCategory = 0x1 << 1;
static const uint32_t enemyCategory = 0x1 << 1;
// 1
#interface MyScene () <SKPhysicsContactDelegate>
#property (nonatomic) SKSpriteNode * player;
#property (nonatomic) NSTimeInterval lastSpawnTimeInterval;
#property (nonatomic) NSTimeInterval lastUpdateTimeInterval;
#property (nonatomic) int monstersDestroyed;
#property (nonatomic) int enemysDestroyed;
#end
static inline CGPoint rwAdd(CGPoint a, CGPoint b) {
return CGPointMake(a.x + b.x, a.y + b.y);
}
static inline CGPoint rwSub(CGPoint a, CGPoint b) {
return CGPointMake(a.x - b.x, a.y - b.y);
}
static inline CGPoint rwMult(CGPoint a, float b) {
return CGPointMake(a.x * b, a.y * b);
}
static inline float rwLength(CGPoint a) {
return sqrtf(a.x * a.x + a.y * a.y);
}
// Makes a vector have a length of 1
static inline CGPoint rwNormalize(CGPoint a) {
float length = rwLength(a);
return CGPointMake(a.x / length, a.y / length);
}
#implementation MyScene
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
// 2
NSLog(#"Size: %#", NSStringFromCGSize(size));
// 3
self.backgroundColor = [SKColor colorWithRed:1.0 green:1.0 blue:1.0 alpha:1.0];
// 4
self.player = [SKSpriteNode spriteNodeWithImageNamed:#"player"];
self.player.position = CGPointMake(self.player.size.width/2, self.frame.size.height/2);
[self addChild:self.player];
self.physicsWorld.gravity = CGVectorMake(0,0);
self.physicsWorld.contactDelegate = self;
}
return self;
}
-(void)addMonster {
// Create sprite
SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:#"monster"];
monster.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:monster.size]; // 1
monster.physicsBody.dynamic = YES; // 2
monster.physicsBody.categoryBitMask = monsterCategory; // 3
monster.physicsBody.contactTestBitMask = projectileCategory; // 4
monster.physicsBody.collisionBitMask = 0; // 5
// Determine where to spawn the monster along the Y axis
int minY = monster.size.height / 2;
int maxY = self.frame.size.height - monster.size.height / 2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
monster.position = CGPointMake(self.frame.size.width + monster.size.width/2, actualY);
[self addChild:monster];
// Determine speed of the monster
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(-monster.size.width/2, actualY) duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
SKAction * loseAction = [SKAction runBlock:^{
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size won:NO];
[self.view presentScene:gameOverScene transition: reveal];
}];
[monster runAction:[SKAction sequence:#[actionMove, loseAction, actionMoveDone]]];
}
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast {
self.lastSpawnTimeInterval += timeSinceLast;
if (self.lastSpawnTimeInterval > 1) {
self.lastSpawnTimeInterval = 0;
[self addMonster];
}
}
- (void)update:(NSTimeInterval)currentTime {
// Handle time delta.
// If we drop below 60fps, we still want everything to move the same distance.
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTimeInterval;
self.lastUpdateTimeInterval = currentTime;
if (timeSinceLast > 1) { // more than a second since last update
timeSinceLast = 1.0 / 60.0;
self.lastUpdateTimeInterval = currentTime;
}
[self updateWithTimeSinceLastUpdate:timeSinceLast];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self runAction:[SKAction playSoundFileNamed:#"pew-pew-lei.caf" waitForCompletion:NO]];
// 1 - Choose one of the touches to work with
UITouch * touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
// 2 - Set up initial location of projectile
SKSpriteNode * projectile = [SKSpriteNode spriteNodeWithImageNamed:#"projectile"];
projectile.position = self.player.position;
projectile.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:projectile.size.width/2];
projectile.physicsBody.dynamic = YES;
projectile.physicsBody.categoryBitMask = projectileCategory;
projectile.physicsBody.contactTestBitMask = monsterCategory;
projectile.physicsBody.contactTestBitMask = enemyCategory;
projectile.physicsBody.collisionBitMask = 0;
projectile.physicsBody.usesPreciseCollisionDetection = YES;
// 3- Determine offset of location to projectile
CGPoint offset = rwSub(location, projectile.position);
// 4 - Bail out if you are shooting down or backwards
if (offset.x <= 0) return;
// 5 - OK to add now - we've double checked position
[self addChild:projectile];
// 6 - Get the direction of where to shoot
CGPoint direction = rwNormalize(offset);
// 7 - Make it shoot far enough to be guaranteed off screen
CGPoint shootAmount = rwMult(direction, 1000);
// 8 - Add the shoot amount to the current position
CGPoint realDest = rwAdd(shootAmount, projectile.position);
// 9 - Create the actions
float velocity = 480.0/1.0;
float realMoveDuration = self.size.width / velocity;
SKAction * actionMove = [SKAction moveTo:realDest duration:realMoveDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[projectile runAction:[SKAction sequence:#[actionMove, actionMoveDone]]];
}
- (void)projectile:(SKSpriteNode *)projectile didCollideWithMonster:(SKSpriteNode *)monster {
NSLog(#"Hit");
[projectile removeFromParent];
[monster removeFromParent];
self.monstersDestroyed++;
if (self.monstersDestroyed > 80) {
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size won:YES];
[self.view presentScene:gameOverScene transition: reveal];
}
}
- (void)didBeginContact:(SKPhysicsContact *)contact
{
// 1
SKPhysicsBody *firstBody, *secondBody, *thirdBody;
if (contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask)
{
firstBody = contact.bodyA;
secondBody = contact.bodyB;
thirdBody = contact.bodyB;
}
else
{
firstBody = contact.bodyB;
secondBody = contact.bodyA;
thirdBody = contact.bodyA;
}
// 2
if ((firstBody.categoryBitMask & projectileCategory) != 0 &&
(secondBody.categoryBitMask & enemyCategory) != 0 &&
(thirdBody.categoryBitMask & monsterCategory) != 0)
{
[self projectile:(SKSpriteNode *) firstBody.node didCollideWithMonster:(SKSpriteNode *) secondBody.node];
[self projectile:(SKSpriteNode *) firstBody.node didCollideWithEnemy: (SKSpriteNode *) secondBody.node];
}
}
- (void)addEnemy {
// Create sprite
SKSpriteNode * enemy = [SKSpriteNode spriteNodeWithImageNamed:#"enemy"];
enemy.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:enemy.size]; // 1
enemy.physicsBody.dynamic = YES; // 2
enemy.physicsBody.categoryBitMask = enemyCategory; // 3
enemy.physicsBody.contactTestBitMask = projectileCategory; // 4
enemy.physicsBody.collisionBitMask = 0; // 5
// Determine where to spawn the monster along the Y axis
int minY = enemy.size.height / 2;
int maxY = self.frame.size.height - enemy.size.height / 2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the monster slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
enemy.position = CGPointMake(self.frame.size.width + enemy.size.width/2, actualY);
[self addChild:enemy];
// Determine speed of the monster
int minDuration = 1.0;
int maxDuration = 6.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
SKAction * actionMove = [SKAction moveTo:CGPointMake(-enemy.size.width/2, actualY) duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
SKAction * loseAction = [SKAction runBlock:^{
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size won:NO];
[self.view presentScene:gameOverScene transition: reveal];
}];
[enemy runAction:[SKAction sequence:#[actionMove, loseAction, actionMoveDone]]];
}
- (void)projectile:(SKSpriteNode *)projectile didCollideWithEnemy:(SKSpriteNode *)enemy {
NSLog(#"Hit");
[projectile removeFromParent];
[enemy removeFromParent];
self.enemysDestroyed++;
if (self.enemysDestroyed > 80) {
SKTransition *reveal = [SKTransition flipHorizontalWithDuration:0.5];
SKScene * gameOverScene = [[GameOverScene alloc] initWithSize:self.size won:YES];
[self.view presentScene:gameOverScene transition: reveal];
}
}
#end
I am not sure if you realize this, but you never actually call addEnemy. Look through the code. You will find a call to addMonster but never addEnemy. Implementing the method is one thing- without calling the method, whatever is inside will never run.

Resources