I am building a game for iphone, I am new to game devolopment and somewhat new to the language. I have balls that fall from the top of the screen to the bottom and the player is supposed to try and catch them. The problem is, I add the new sprites with an "addnewBallSprite" method, and later act on the position in my UpdateEveryFrame method. But if there is still a ball on the screen when a new ball is created the old ball stops moving. Is there a way I can make my change in position command control all instances of the ball sprite??? Please explain in detail. Below is my addNewBall method:
-(void) addNewBall {
int RandomXPosition = (arc4random() % 240) + 40;
int RandomBallSprite = (arc4random() % 5);
NSString *BallFileString = #"OrangeBall.png";
switch (arc4random() % 5) {
case 1:
BallFileString = #"OrangeBall.png";
break;
case 2:
BallFileString = #"GreenBall.png";
break;
case 3:
BallFileString = #"YellowBall.png";
break;
case 4:
BallFileString = #"PinkBall.png";
break;
case 0:
BallFileString = #"BlueBall.png";
break;
}
Ball = [CCSprite spriteWithFile:BallFileString];
Ball.position = ccp(RandomXPosition, 520);
[self addChild:Ball z:1];
}
If you want to run some action (i mean CCAction or something other if you want) on the sprite you should have a pointer to this sprite. So you can create an NSMutableArray to keep those pointers and then to interact with them. If the only children of you CCLayer are balls - you can take there pointers via calling children method on your CCLayer.
EDIT
If you have many balls the only way to interact with them is to keep their pointers. Add this declaration to your class:
NSMutableArray *ballArray_;
Initialize array in your class init method:
ballArray_ = [[NSMutableArray alloc] init];
When you create a new ball add the ball to the array:
[ballArray addObject:myNewBall];
And now in your update method you can have the access to each ball in your scene:
for (Ball *ball in ballArray)
{
//increase the position of the ball
}
When the ball is catched by User or out of the scene remove it from array:
[ballArray removeObject: someBall];
Related
I currently have this code in a collide statement where if collide with object then this particle happens but how do I stop it? As it goes on forever whereas I only want to happen a couple of times per contactetc
SKEmitterNode *emitter = [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:#"ff" ofType:#"sks"]];
emitter.zPosition = 0;
emitter.particlePositionRange = CGVectorMake(0, self.size.height);
emitter.position = CGPointMake(self.size.width, self.size.height/2);
[self addChild:emitter];
When you use the particle-editor you can set the maximum number of particles to create. It's the field below "Particle Texture".The official description is:
"The maximum number of particles that the emitter creates over the emitter’s lifetime. After this number is reached, no more particles are created by the emitter. Enter 0 to remove particle limits."
Also see: Particle Emitter Editor Guide
Of course, you should remove the emitter-node from its parent after it created the maximum number of particles. This can be done by creating an action-sequence that waits for a few seconds and removes the emitter-node from its parent [SKAction removeFromParent].
Use setParticleBirthRate:0 to stop emitting particles. This is the most realistic way to turn off an emitter.
If you want it to disappear immediately then use removeFromParent.
If using setParticleBirthRate remember the original value to turn it back on later. for instance.
#implementation GameScene
{
SKEmitterNode *rocketfire;
float rocketfire_birthrate;
}
// ...
-(void)init {
// ...
rocketfire_birthrate = rocketfire.particleBirthRate;
}
// turn on the emitter (thrust rocket) when the screen is touched
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[rocketfire setParticleBirthRate:rocketfire_birthrate];
}
// turn off the emitter on touches ended
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[rocketfire setParticleBirthRate:0];
}
This is similar to the answer here provided by Tapir, with the exception that I don't use the selector. I'm not sure if that is required in previous versions but in ios 10 I don't see my node counts increasing and it does work. ie. add an emitter, pop a specified number of particles and then remove the emitter. In this case, I've modified it to add a single particle.
-(void) gainPoints:(int)x y:(int)y {
NSString *pPlus1Path = [[NSBundle mainBundle] pathForResource:#"PlusEmitter" ofType:#"sks"];
SKEmitterNode *pEmitter = [NSKeyedUnarchiver unarchiveObjectWithFile:pPlus1Path];
// Place the emitter at the specified position
pEmitter.position = CGPointMake(x,y);
pEmitter.name = #"plus1";
pEmitter.particleLifetime = 1;
pEmitter.particleBirthRate = 1;
pEmitter.numParticlesToEmit = 1;
// Send the particles to the scene.
pEmitter.targetNode = self.scene;
[self addChild:pEmitter];
SKAction *pRemoveNode = [SKAction removeFromParent];
SKAction *pWaitForSecsN = [SKAction waitForDuration:1];
SKAction *pSequence = [SKAction sequence:#[pWaitForSecsN,pRemoveNode]];
[pEmitter runAction:pSequence];
}
SWIFT 4:
I think I have a slightly better approach for turning it off:
1) Put the birth rate to 0:
pEmitter.particleBirthRate = 0
2) Run a delay action for the lifetime:
let waitAction = SKAction.wait(forDuration: pEmitter.particleLifetime)
pEmitter.run(waitAction, completion: {
pEmitter.removeFromParent()
})
NOTE: Might not be 100% accurate if you have a range of partilcelifetime.
For those who want to freeze a particle emitter on a specific part of the animation, instead of removing it from screen, you can pause the emitter:
emitter.isPaused = true
when pause you need :
emitter.hidden = true
when restart you need :
emitter.hidden = false
emitter.resetSimulation()
I have a contact listener to detect collisions between a bullet and an asteroid. Inside the beginContact function i check for the sprite types and fire a block from within this function.
The block is set by the scene class when it is allocated, before the contact listener is assigned to the box2dworld. The bullets and asteroids both get box2d bodies as well as sprites.
The problem is that i can access the sprites and do whatever i want with them (run actions, stop actions etc.).
However, as soon as i call the removeChild function on the sprite, I get an EXC_BAD_ACCESS in the b2Contact::Update on listener->BeginContact(this).
Can anyone point me in the right direction?
My tick function:
//Physics simulation
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
b2Vec2 b2Position = b2Vec2(sprite.position.x/PTM_RATIO,
sprite.position.y/PTM_RATIO);
float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(sprite.rotation);
b->SetTransform(b2Position, b2Angle);
}
}
for(CCSprite *sprite in spritesToDelete) {
[self removeSprite:sprite];
[spritesToDelete removeObject:sprite];
sprite = nil;
}
My contact listener callback block:
contactListener->contactBlock = ^(CCSprite *A, CCSprite *B) {
NSLog(#"A: %#", [A class]);
NSLog(#"B: %#", [B class]);
CCSprite *bullet = nil , *asteroid = nil;
if ([(NSString *)A.userData isEqualToString:#"asteroid"] && [(NSString *)B.userData isEqualToString:#"bullet"])
{
asteroid = A;
bullet = B;
} else if ([(NSString *)B.userData isEqualToString:#"asteroid"] && [(NSString *)A.userData isEqualToString:#"bullet"])
{
asteroid = B;
bullet = A;
}
if (asteroid != nil && bullet != nil) {
NSLog(#"Asteroid Hit!");
[asteroid stopAllActions];
[bullet stopAllActions];
//If I keep the line below uncommented, I get the error. Adding actions and stuff does not make an issue, only removal of sprite is when I get the error.
[spritesToDelete addObject:bullet];
}
};
I guess you put a pointer to your sprite in b2UserData of b2Body or b2Fixure in order to retrieve when collision happens. When you delete the sprite, this pointer will point to a deleted instance and thus you receive EXC_BAD_ACCESS.
You should nullify the pointer to eliminate the problem
I want a snow particle effect to follow my sprite and I tried some methods but all that ends up happening is the snow will just stay still instead of following. I did this one tutorial (will post as soon as I find it) thats shows how it do it with fire but didn't work out at all. Any tutorials or suggestions will be appreciated. I believe i have to add some kind of code to the snippet part where it says create enemy off screen.
[self schedule:#selector(gameLogicboss:) interval:180 ];
[self schedule:#selector(updateboss:)];
-(void)addTarget1 {
Boss *target1 = nil;
if ((arc4random() % 2) == 0) {{
target1 = [WeakAndFastBoss boss];
}} else {
target1 = [WeakAndFastBoss boss];
}
// Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY = target1.contentSize.height/2;
int maxY = winSize.height - target1.contentSize.height/2;
int rangeY = maxY - minY;
int actualY = (arc4random() % rangeY) + minY;
// Create the target slightly off-screen along the right edge,
// and along a random position along the Y axis as calculated above
target1.position = ccp(winSize.width + (target1.contentSize.width/2), actualY);
[self addChild:target1 ];
// Determine speed of the target
int minDuration = target1.minMoveDuration;
int maxDuration = target1.maxMoveDuration;
int rangeDuration = maxDuration - minDuration;
int actualDuration = (arc4random() % rangeDuration) + minDuration;
// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration position:ccp(-target1.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self
selector:#selector(spriteMoveFinished:)];
[target1 runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
target1.tag = 1;
[_targets addObject:target1];
}
-(void)gameLogicboss:(ccTime)dt {
[self addTarget1];
iterations_++;
}
- (void)updateboss:(ccTime)dt {
CGRect projectileRect = CGRectMake(projectile.position.x - (projectile.contentSize.width/2), projectile.position.y - (projectile.contentSize.height/2), projectile.contentSize.width, projectile.contentSize.height);
BOOL bossHit = FALSE;
NSMutableArray *targetsToDelete = [[NSMutableArray alloc] init];
for (CCSprite *target1 in _targets) {
CGRect target1Rect = CGRectMake(target1.position.x - (target1.contentSize.width/2), target1.position.y - (target1.contentSize.height/2), target1.contentSize.width, target1.contentSize.height);
if (CGRectIntersectsRect(projectileRect, target1Rect)) {
//[targetsToDelete addObject:target];
bossHit = TRUE;
Boss *boss = (Boss *)target1;
boss.hp--;
if (boss.hp <= 0) {
_score ++;
[targetsToDelete addObject:target1];
}
break;
}
}
for (CCSprite *target in targetsToDelete) {
[_targets removeObject:target];
[self removeChild:target cleanup:YES];
_projectilesDestroyed++;
if (_projectilesDestroyed > 2) {
}
}
if (bossHit) {
//[projectilesToDelete addObject:projectile];
[[SimpleAudioEngine sharedEngine] playEffect:#"explosion.caf"];
}
[targetsToDelete release];
}
-(void)spriteMoveFinishedboss:(id)sender {
CCSprite *sprite = (CCSprite *)sender;
[self removeChild:sprite cleanup:YES];
GameOverScene *gameOverScene = [GameOverScene node];
[gameOverScene.layer.label setString:#"You Lose"];
[[CCDirector sharedDirector] replaceScene:gameOverScene];
if (sprite.tag == 1) { // target
[_targets removeObject:sprite];
} else if (sprite.tag == 2) { // projectile
[_projectiles removeObject:sprite];
}
}
I Have found this, at CCParticleSystem.h
/** #typedef tCCPositionType
possible types of particle positions
/
typedef enum {
/* Living particles are attached to the world and are unaffected by emitter repositioning. */
kCCPositionTypeFree,
/** Living particles are attached to the world but will follow the emitter repositioning.
Use case: Attach an emitter to an sprite, and you want that the emitter follows the sprite.
*/
kCCPositionTypeRelative,
/** Living particles are attached to the emitter and are translated along with it. */
kCCPositionTypeGrouped,
you should set it like
myparticleSystem.positionType=kCCPositionTypeGrouped;
Hope it helps.
You don't need to update the particle emitter's position with the sprite.
You can add a particle system to the sprite as a child.
The particle system does need to be typed as such:
CCParticleSystem * booster = [CCParticleSystem particleWithFile:#"boosterParticles.plist"];
//customize your particles' options
//assuming you have a sprite defined as _motherShip
[_motherShip addChild:booster];
/*
* now that the particles are the _motherShip's child, you must remember
* to set the position relative to the mothership's origin...
*/
particles.position = ccp(15,0);
...So now whenever _motherShip.position changes, the booster will follow. It will even rotate with the ship.
Very simple logic without getting into code:
I spawn a sprite and give it a location (x, y).
For each sprite, I also spawn a CCParticleSystem, give it the required particle type, spawn rates etc.
The CCParticleSystem location is now set to be the same (x,y) location as the sprite, and it should get updated as the sprite's (x,y) location is update.
As the sprite and the CCParticleSystem move around, this location (x, y) is getting updates constantly at random in your schedule method per the interval step time.
Hope that makes sense.
I do this
vehicleParticleSystem = [CCParticleSystemQuad particleWithFile:#"vehicleParticle.plist"];
vehicleParticleSystem.position = ccp(_ship.position.x - _ship.contentSize.width/3, _ship.position.y - _ship.contentSize.height/3);
if (UI_USER_INTERFACE_IDIOM() != UIUserInterfaceIdiomPad) {
vehicleParticleSystem.scale = 0.5;
}
[self addChild:vehicleParticleSystem z:-1];
and update its position with this
- (void) updateParticleSystem:(ccTime)dt {
vehicleParticleSystem.position = ccp(_ship.position.x - _ship.contentSize.width/3, _ship.position.y - _ship.contentSize.height/3);
}
which is called in the -(void) update:(ccTime)dt method.
The ship is moved by the user via a joypad.
hope this helps. The positioning for the particle is slightly behind the vehicle for an engine effect.
Did you tried to update your particle position taking advantage of the update method?
I'm doing something like the following lines in one of my games and it works as I think you expect
-(void) update:(CCTime)delta{
_sprite.position = CGPointMake(newXPosition, newYPosition);
_fire.position = CGPointMake(_sprite.position.x, _sprite.position.y);
}
I hope it helps!
As a very beginner in Cocos2D i´m trying to make an iPhone game where some cows move randomly around the screen. I used the code for moving the sprites from here: highoncoding.com/.../. I´m adding the sprites in the init method wia an addAnimal method:
-(void) addAnimal {
animal = [CCSprite spriteWithFile:#"cow.png"];
animal.position = [self generateRandomCoordinates];
[self addChild:animal];
[self startAnimation:animal];
}
My problem:
When i add more than one cow to my game, they move from that random spawn position to another random position and then the first cow stops and the other cow goes on correctly. The startAnimation command in the finishedMoving method goes always to the last sprite. That means i need better control over my sprites but how to da that right?
You can try to implement animal class, that will contain your sprite and incapsulate random movement. Smth like
#implementation Cow
- (id) init
{
self = [super init];
if( self != nil )
{
CCSprite* cowSprite = [CCSprite spriteWithFile:#"cow.png"];
[self addChild: cowSprite];
}
return self;
}
- (void) onEnter
{
[super onEnter];
[self makeRandomMovement];
}
- (void) makeRandomMovement
{
id randomMoveAction = // create your random move action here
id moveEndCallback = [CCCallFunc actionWithTarget: self selector: #selector(makeRandomMovement)];
id sequence = [CCSequence actionOne: randomMoveAction two: moveEndCallback];
[self runAction: sequence];
}
#end
In such way after ending random movement part, method makeRandomMovement will be called again to generate and start new random movement part.
then remake your addAnimal method to smth like
- (void) addAnimal
{
Cow* newCow = [Cow node];
[newCow setPosition: [self generateRandomPosition]];
[self addChild: newCow];
}
I have a game that I wrote. I am about ready to call it finished but I found a bug. Basically the game gets slower as the longer you play. My guess is this is due to sprites that are still being drawn off screen. I will paste the code below but basically the sprite is created in the "addNewBall" method. In this method it is added to an array which calculates its motion. After the ball reaches a position where it is off the screen it is removed from the array which causes it to stop moving but it is still being "drawn" off screen. How do I remove the sprite so the processor no longer calculates it. Thanks in advance for your help!
Tanner
Code:
-(void) addNewBall {
NumberOfBalls = NumberOfBalls + 1;
int RandomXPosition = (arc4random() % 240) + 40;
NSString *BallFileString = #"OrangeBall.png";
switch (arc4random() % 5) {
case 1:
BallFileString = #"OrangeBall.png";
break;
case 2:
BallFileString = #"GreenBall.png";
break;
case 3:
BallFileString = #"YellowBall.png";
break;
case 4:
BallFileString = #"PinkBall.png";
break;
case 0:
BallFileString = #"BlueBall.png";
break;
}
Ball = [CCSprite spriteWithFile:BallFileString];
Ball.position = ccp(RandomXPosition, 520);
BallIsMoving = YES;
[self addChild:Ball z:10];
[AllObjectsArray_ addObject:Ball];
[BallArray_ addObject:Ball];
}
//And here is where it is removed...
if (Ball.position.y <= -100) {
[BallArray_ removeObject: Ball];
}
You seem to be missing some conditions in your removal method. Don't you also want to remove the ball if its y position is greater than the screen height, or if its x position is off-screen? At any rate, in the same place that you're removing the ball from the array, you should add:
[self removeChild:Ball cleanup: YES]
I should also point out that your BallArray is probably redundant, since you're adding all the balls to another node anyway. If the only children of that node are Balls, you can get the array of balls using its children property. In this case, the child array would be: self.children (See http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a5e739ecda0c314283a89ac389dfca2fa for more info.)
If you have non-Ball children on the same node, you might want to add an intermediate node to simplify the design so that you can use one less array.
You said you are removing the objects from the arrays, but you didn't mention that you are also removing the sprite from the parent CCNode.
Check the methods from CCNode to remove childs: http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_node.html#a0d4e615f688458c74001acf10f0ae011
You could use:
[Ball removeFromParentAndCleanup:YES];
This will remove the ball from it's parent CCNode and will remove all actions and callbacks.
You need to specify your sprite,and you can use this following line..
[self removeChild:Ball cleanup: YES]