Changing Sprite Spawns - ios

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];

Related

Keeping track of SKNodes for a category

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"

how can we set tag for a CCSprite in cocos2D

I am new to cocos2D . I want to set Tag for CCSprite but this show me error in cococs2D version 3 while i have seen Answer on Stackoverflow [set tag] property but it does not work form me in cocos2d Version 3 .
What I requried is I have created two different Monster and I want to find out in Collision delegate which monster has collided .
Let me show you how I am creating Monster.
CCSprite *monster = [CCSprite spriteWithImageNamed:#"xyz.png"];
int minY = monster.contentSize.height / 2;
int maxY = self.contentSize.height - monster.contentSize.height / 2;
int rangeY = maxY - minY;
int randomY = (arc4random() % rangeY) + minY;
// 2
monster.position = CGPointMake(self.contentSize.width + monster.contentSize.width/2, randomY);
monster.physicsBody = [CCPhysicsBody bodyWithRect:(CGRect){CGPointZero, monster.contentSize} cornerRadius:0];
monster.physicsBody.collisionGroup = #"monsterGroup";
monster.physicsBody.collisionType = #"monsterCollision";
[_physicsWorld addChild:monster z:1];
// 3
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int randomDuration = (arc4random() % rangeDuration) + minDuration;
// 4
CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(-monster.contentSize.width/2, randomY)];
CCAction *actionRemove = [CCActionRemove action];
[monster runAction:[CCActionSequence actionWithArray:#[actionMove,actionRemove]]];
- (BOOL)ccPhysicsCollisionBegin:(CCPhysicsCollisionPair *)pair monsterCollision:(CCNode *)monster projectileCollision:(CCNode *)projectile
{
[monster removeFromParent];
[projectile removeFromParent];
score=score+1;
return YES;
}
here I want to fetch CCsprite by Tag or by image i am not sure how can i recognize CCsprite in collision delegate.
I'm not sure but I think you can set name property for a sprite when adding it like:
[self addChild:(CCNode *) z:(NSInteger) name:(NSString *)]
and then later get it by
getChildByName

How do i keep falling spritenodes within the view?

i am trying to make an iphone game similar to the raywenderlich : sprite Kit tutorial for Beginners Turorial. I am trying to make this in portait mode however, instead of the landscape mode which is used in the tutorial. I used this code and adjusted it so that the sprites fall from the top of the screen to the bottom, but sometimes the sprite are half outside the view or even more.
http://www.raywenderlich.com/42699/spritekit-tutorial-for-beginners
This is what i got:
SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:#"AcornFinal.png"];
// Determine where to spawn the monster along the X axis
int minX = self.frame.size.width;
int maxX = self.frame.size.width - monster.size.width;
int rangeX = maxX + minX;
int actualX = (arc4random() % + rangeX);
// Random position along the X axis as calculated above
// This describe from which way the acorns move
// - means moving from top to the right and + means moving from the top to the left
monster.position = CGPointMake(actualX,self.frame.size.height);
[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) duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[monster runAction:[SKAction sequence:#[actionMove, actionMoveDone]]];
So I believe that the problem probably is in these four lines of code :
// Determine where to spawn the monster along the X axis
int minX = self.frame.size.width;
int maxX = self.frame.size.width - monster.size.width;
int rangeX = maxX + minX;
int actualX = (arc4random() % + rangeX);
How can i fix this error>?
EDIT
solution was to change the first four lines into
int minX = monster.size.width;
int maxX = self.frame.size.width - monster.size.width;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX)+minX;
If the idea is to keep the monsters within the horizontal-bounds of the screen your math seems somewhat of. If the anchor point of the sprite is 0.5, 0.5 and the scene-size equals the screen I believe it should be:
int minX = monster.size.width / 2;
int maxX = self.frame.size.width - monster.size.width;
int x = (arc4random() % + maxX) + minX;

How to make sprites come down from top of screen?

I'm making a simple iOS game where the goal is to fire arrows from a bow at the bottom of the screen to destroy monsters coming from the top of the screen. I have this bit of code, but it only makes monsters spawn and move left from the right side of the screen. Here's the code:
- (void) spawnMonster
{
SKSpriteNode *monster = [SKSpriteNode spriteNodeWithImageNamed: #"Monster"];
monster.xScale = 0.5;
monster.yScale = 0.5;
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;
monster.position = CGPointMake(self.frame.size.width + monster.size.width / 2, actualY);
[self addChild: monster];
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
SKAction *actionMove = [SKAction moveTo: CGPointMake(-monster.size.width / 2, actualY) duration: actualDuration];
SKAction *actionMoveDone = [SKAction removeFromParent];
[monster runAction: [SKAction sequence: #[actionMove, actionMoveDone]]];
}
Suggestions? Thanks.
If you want objects falling from the top then you should set the arc4random function to the monster sprite's X and not Y. The Y should always be the top of your view. Remember x is the left/right axis and y is the up/down axis.

Spawning sprites randomly on x-axis

Hi I need to spawn balloons which are my sprites along the bottom of the x axis out of the screen randomly. I used this code from Ray Wenderliches website but when i click the button that starts the spawning they don't spawn?
#implementation MyScene2
- (void)addMonster {
// Create sprite
SKSpriteNode * monster = [SKSpriteNode spriteNodeWithImageNamed:#"balloon11.png"];
// Determine where to spawn the monster along the X axis
int minX = monster.size.height / 2;
int maxX = self.frame.size.height - monster.size.height / 2;
int rangeX = maxX - minX;
int actualX = (arc4random() % rangeX) + minX;
// Create the monster slightly off-screen along the right edge,
// and along a random position along the X axis as calculated above
monster.position = CGPointMake(self.frame.size.width + monster.size.width/2, actualX);
[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, actualX)
duration:actualDuration];
SKAction * actionMoveDone = [SKAction removeFromParent];
[monster runAction:[SKAction sequence:#[actionMove, 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];
}
Your main problem lies in this lines of code:
monster.position = CGPointMake(self.frame.size.width + monster.size.width/2 , actualX);
That method works like this: CGPointMake( x_coord , y_coord );.
Right now, you are setting the x value to width of the frame plus the width of the sprite, so this will cause the sprite to be spawned off the screen. You are giving the y coordinate the randomized value. Instead, use
CGPointMake(actualX , self.frame.size.height);
Now there is another problem with this code. These coordinates are based on the view, but monster.position is based on the SKScene. The SKScene origin usually does not match up with the origin for the view. So you have to do it like this:
monster.position = [CGPointMake(actualX , self.frame.size.height); locationInNode: self];
Also, I noticed that you are using frame.size.width for intended Y axis coordinates and frame.size.height for intended X axis coordinates. Width is for x and height is for y, so make sure you are using those properly.

Resources