IPhone and Cocos2d Sprites/Layers - iphone

I am using cocos2d-iphone to place Sprites onto a Layer to setup a game playfield. At certain points in the game, certain Sprites need to be removed based upon game conditions. What I would like to do is setup an array of Sprite pointers, but I have two questions:
What's the best way to place Sprite pointers in an array?
How does one remove the Sprite in cocos2d with only a pointer to the Sprite? I know how to do it from its parent layer, but that is too runtime intensive for the main game loop.
Thanks in advance!

The Sprite class inherits from CocosNode, so you should be able to call spritePointer.parent.remove(spritePointer)

I figured it out. If anyone else is interested, the way to do it is to declare an array of Sprite pointers, such as:
Sprite * mySprites[10][10]; // assuming a 10x10 playfield where obstacles get placed
Then, when setting up your Sprites:
mySprites[0][0] = [Sprite spriteWithFile: #"obstacle.png"];
[myLayer add:mySprites[0][0]];
To remove the Sprite:
[myLayer remove:mySprites[0][0]];

There's also [mySprite removeFromParentAndCleanup:YES].

Related

Creating a Body - Cocos2d/Box2d

I have a ball and another sprite. When the ball collides with the sprite it simulates falling.
My only problem is the other sprite is just on big image and the ball is on top of it, but there are spaces on the sprite and a lot of corners. I need to determine if the sprite has touch one of the corners. I know this is hard to understand.
So, my question is, is it possible to make a body without
b2PolygonShape blockShape;
and
blockShapeDef.shape = &blockShape;
OR
is there an alternative I can use? I cannot set the image as a box and it would take way to long to set edges because there are so many corners.
I have already set up the collision detection.
I really need help with this.
Thanks!
If you want it to react properly, you have to make a polygon using every single corner coordinate.
But don't be lazy about it. You can use SpriteHelper for creating *b2PolygonShape*s out of your sprites.
Or another alternative: VertexHelper

Boolean question in Cocos2d

I am making a game in Cocos2d. I have enemies in the game. I want them to shoot at the character. Currently, I have a boolean that says
buffDude.shoot = YES
when it is done moving. And in the Enemy class, I want it to detect if the boolean is YES or NO, and shoot if it is YES. And, while we're on that note, if I declare
buffDude.shoot = YES
in the
+(id)enemy
method, it will create a bullet at the bottom of the enemy sprite, but the bullet will not move. I know that it is because it didn't add the bullet to the Layer, it added it to the Enemy, but I don't know how to add it to the layer. Please Help! This is really driving me crazy, and help would be appreciated.
EDIT:
Okay, to get it to work, I just created a different layer and added the enemies and their bullets to that. Thanks for the help!
I am not really sure what you are trying to do. But I can only assume that you are adding a bullet sprite. And if you are adding a bullet sprite in you Enemy Class like: [self addChild:bulletSprite];, the Enemy class will own that bullet. Instead, if you would like the bullet to be visible in your game scene, you can add the same code in HelloWorldLayer.m instead.
But as I said, I'm not really sure what your problem is.

Cocos2d - Creating collidable Sprites?

Hello everyone I an very new to cocos2d, so I apologize if this is a simple question. I would like to create to sprites that collide when they hit each other.
For instance, one sprite, spriteA, is in a fixed position on the screen. And another sprite, spriteB, is moving around the screen. SpriteB will collide with spriteA. If that doesn't make sense or you don't understand it, tell me and I will elaborate further. Any help is appreciated. Thank YOU!
Try using Chipmunk or Box2d physics systems. These are included with Cocos2d and work by having a physics simulation that updates with every time the graphics change on screen.
The physics simulation will tell you if two objects are overlapping, and will provide physical attributes like weight, slipperiness (friction), speed and direction, which creates reactions like bouncing, sliding, realistic loss of speed and changes of direction on impact.
If you are new to physics simulation, here's a 30 second run down:
Create "bodies" in the physics simulation that represent each graphical sprite
Bodies are usually defined more simply than their graphical counterparts, like a circle, square or simple polygon shape
In order to update the graphics on the screen accurately, first you establish a pixels to meters ratio. Meters are notional (i.e. made up) measurement that is used in the physics sim
Each time the physics simulation "ticks", you update the graphics on screen accordingly
So if a body in the physics sim moves 1 meter, you might transform the pixel sprite 32 pixels
Here's a good tute on collision detection using Box2d.
http://www.raywenderlich.com/606/how-to-use-box2d-for-just-collision-detection-with-cocos2d-iphone
enjoy
Its actually very simple:
Just schedule a timer: [self schedule:#selector(checkForCollision:)];
Specify the method: - (void)checkForCollision:(ccTime)dt {}
In the curly braces, make CGRects for each sprite using CGRectMake.
Then in the same method, just call: if (CGRectIntersectsRect) {}
That easy!
Well technically speaking when the 2 sprites interact, or share at least one common point, then they are colliding. I'm a little confused by your question. Are you asking for direction on how to make the sprite move on screen, or are you asking how to handle the actual collision(such as calling a method if they collide)?

How do I add background in cocos2d?

I'm a total noob. Starting my first app. I'm using cocos2d and there's isn't a .xib file so how do I add a background image behind the sprites I have in my scene?
In Cocos2d you don't use xibs you have to place all the objects by code. Check the samples that come with Cocos2d to get a sense of how it works.
If you already know how to add sprites, adding a background is done the same way, it would be just another sprite just add it with a lower z-order.
[self addChild:myBackground z:0];
"self" would be the actual CCLayer
"myBackground" the sprite you created with the bakcground iamge.
and "z" would have to be lower than the z used for the other elements.

How can we get two animations run simultaneously?

I am writing a game in iPad using cocos2d.
I need to mix two actions,
One action is, to get the rotation of the object. I have 10 images of the rotation of an object. When we animate them we get the rotation of the object.
Second action is to move the object from one edge to other edge. I am able to get one action after the other.
But I need both actions at same time.
The object should rotate while it moves from one edge to opposite edge. How can I mix both the animations.
Thank You.
I hope this may help
[sprite runAction:[CCSpawn actions:action1, action2, nil]]
or try this when u got only 2 actions
[sprite runAction:[CCSpawn actionOne:action1 two:action2]]
or you can do like this
[sprite runAction:action1];
[sprite runAction:action2];
I have 10 images of the rotation of an object. When we animate them we get the rotation of the object.
You're halfway there.
Modify the images so that they go from edge to edge. Model both the rotation and the movement with your images. You'll probably need more than 10, though.
That way, you only have one animation to perform.