How do i keep falling spritenodes within the view? - ios

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;

Related

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 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.

Animate Sprite Vertically

I am having real trouble getting a sprite to spawn on the screen at the top and then animate it from the top to the bottom. I have been following Ray Wenderlich’s tutorial on creating a simple game however that moves the sprite from right to left, I would like it to move from top to bottom and now I’m tremendously stuck! Below is my current code:
- (void)addComet:(CCTime)dt
{
// Make sprite
CCSprite *comet = [CCSprite spriteWithImageNamed:#"PlayerSprite.png"];
// Verticle spawn range
int minY = comet.contentSize.width;
int maxY = self.contentSize.height - comet.contentSize.height / 2;
int rangeY = maxY - minY;
int randomY = (arc4random() % rangeY) + minY;
// Position comets slightly off the screen
comet.position = CGPointMake(self.contentSize.width + comet.contentSize.width, randomY);
[self addChild:comet];
// Duration range comets take to fly across screen
int minDuration = 2.0;
int maxDuration = 4.0;
int rangeDuration = maxDuration - minDuration;
int randomDuration = (arc4random() % rangeDuration) + minDuration;
// Give comet animation
CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(0, 500)];
CCAction *actionRemove = [CCActionRemove action];
[comet runAction:[CCActionSequence actionWithArray:#[actionMove,actionRemove]]];
}
If anyone could point me in the right direction because I’ve been stuck on this for quite a while and just cannot get the sprite to spawn randomly at the top of the screen and then animate down to the bottom. I have also looked at sample code such as tweejump but have had no luck.
Now, I haven't got Cocos2D in any of my projects, so don't know if there are any typos, but generally it should probably look a little something like the below. The idea is that all the comets should start at the same Y-position (off-screen) but have a randomized horizontal (x) position...
- (void)addComet:(CCTime)dt
{
// Make sprite
CCSprite *comet = [CCSprite spriteWithImageNamed:#"PlayerSprite.png"];
NSInteger y = self.contentSize.height; // perhaps + comet.contentSize.height / 2, if the anchorPoint is 0.5, 0.5
// Random horizontal position
NSInteger maxX = self.contentSize.width;
NSInteger randomX = (arc4random() % maxX);
// Position comets slightly off the screen
comet.position = CGPointMake(randomX, y);
[self addChild:comet];
// Duration range comets take to fly across screen
NSInteger minDuration = 2.0;
NSInteger maxDuration = 4.0;
NSInteger rangeDuration = maxDuration - minDuration;
NSInteger randomDuration = (arc4random() % rangeDuration) + minDuration;
// Give comet animation
CCAction *actionMove = [CCActionMoveTo actionWithDuration:randomDuration position:CGPointMake(randomX, 0)]; // Moving it in a straight line vertically
CCAction *actionRemove = [CCActionRemove action];
[comet runAction:[CCActionSequence actionWithArray:#[actionMove,actionRemove]]];
}

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.

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

Resources