cocos2d Children sprite collision detection with parent sprite - cocos2d-iphone

Hi stackoverflow community!
How do you detect a children sprite collision with a parent sprite in cocos2d?
Currently I have my codes like this:
CGSize screenSize = [[CCDirector sharedDirector]winSize];
parentJumper = [CCSprite spriteWithFile:#"inviBtn.png"];
jumper = [CCSprite spriteWithFile:#"jumperRight.png"];
plat = [[Platform alloc]init];
plat = [Platform spriteWithFile:#"platformBlack.png"];
plat.position = ccp(160,100);
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:(1.0/60)];
jumper.anchorPoint = ccp(0.5, 0);
jumper.position = ccp(0, 20);
parentJumper.position = ccp(screenSize.width/2, 0);
[self addChild:plat];
[self addChild:parentJumper];
[parentJumper addChild:jumper];
Now how do I detect the collision between "jumper" & "plat"?
Thanks for your help!

Usually you can check collision like this:
if(CGRectIntersectsRect([jumper boundingBox], [plat boundingBox])) {
//Handle collision<br>
}

Related

Why is Cocos2D update only firing when adding sprite to parent's CCSpriteBatchNode from child

I don't understand why the following is happening and I hope someones here can explain it.
I have a GameLayer (CCLayer) Class and a Food (CCNode) Class.
In the Gamelayer Class I create a bunch of food Objects that have a sprite as a property.
And I want to add these sprites to a CCSpriteBatchNode.
spriteBatch = [CCSpriteBatchNode batchNodeWithFile:#"bol.png"];
[self addChild:spriteBatch];
for (int i = 0; i < 1000; i++) {
Food * tempfood = [Food foodWithParentNode:self];
[spriteBatch addChild:[tempfood mySprite]];
[self addChild:tempfood];
}
When I use the code above, the sprites all show up on screen but don't move. (They should because I scheduled an Update in the Food Class (see below) and in this update the position of the food objects is changed)
-(id)initWithParentNode:(CCNode *)parentNode{
if((self = [super init]))
{
mySprite = [CCSprite spriteWithFile:#"bol.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
[[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[self scheduleUpdate];
}
return self;
}
-(void) update:(ccTime)delta
{
... DO SOME position calculations ...
[[self mySprite] setPosition:CGPointMake(newX, newY)];
}
But if I move the code that adds the sprite to the batch from the Game class to the food class the update that changes the position does work and the foods are moving on screen.
BUT WHY?
So this gives:
-(id)initWithParentNode:(CCNode *)parentNode{
if((self = [super init]))
{
mySprite = [CCSprite spriteWithFile:#"bol.png"];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
[[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];
[[ (GameLayer*) parentNode spriteBatch] addChild:mySprite];
[self scheduleUpdate];
}
return self;
}
I really can't see the difference between calling
[[ (GameLayer*) parentNode spriteBatch] addChild:mySprite];
from the food class or :
[spriteBatch addChild:[tempfood mySprite]];
from the 'parent' GameLayer
Ruben, the mySprite is a property with retain attribute?
The Food class can be loosing the memory reference of this property...
on init, try to set mySprite using self.mySprite, to retain this.
on .m or .h, put:
#property (nonatomic, retain) CCSprite *mySprite
and on init, use:
self.mySprite = [CCSprite spriteWithFile:#"bol.png"];

Issues with creating a CCParallaxNode in cocos2d?

I'm using a CCParallaxNode for a scrolling background in my GameLayer and I'm getting a bunch of errors and I can't figure out why.
"Initializer element is not a compile-time constant" and also "Unk
#implementation Background
// Load the sprites for each parallax layer, from background to foreground.
CCSprite* para1 = [CCSprite spriteWithFile:#"color.png"];
CCSprite* para2= [CCSprite spriteWithFile:#"ship-hd.png"];
CCSprite* para3 = [CCSprite spriteWithFile:#"twit.png"];
CCSprite* para4 = [CCSprite spriteWithFile:#"Start.png"];
// Set the correct offsets depending on the screen and image sizes.
para1.anchorPoint = CGPointMake(0, 1);
para2.anchorPoint = CGPointMake(0, 1);
para3.anchorPoint = CGPointMake(0, 0.6f);
para4.anchorPoint = CGPointMake(0, 0);
CGPoint topOffset = CGPointMake(0, screenSize.height);
CGPoint midOffset = CGPointMake(0, screenSize.height / 2);
CGPoint downOffset = CGPointZero;
// Create a parallax node and add the sprites to it.
CCParallaxNode* paraNode = [CCParallaxNode node];
[paraNode addChild:para1 z:1 parallaxRatio:CGPointMake(0.5f, 0) ositionOffset:topOffset];
[paraNode addChild:para2 z:2 parallaxRatio:CGPointMake(1, 0) positionOffset:topOffset];
[paraNode addChild:para3 z:4 parallaxRatio:CGPointMake(2, 0) positionOffset:midOffset];
[paraNode addChild:para4 z:3 parallaxRatio:CGPointMake(3, 0) positionOffset:downOffset];
[self addChild:paraNode z:0 tag:ParallaxSceneTagParallaxNode];
// Move the parallax node to show the parallaxing effect.
CCMoveBy* move1 = [CCMoveBy actionWithDuration:5 position:CGPointMake(−160, 0)];
CCMoveBy* move2 = [CCMoveBy actionWithDuration:15 position:CGPointMake(160, 0)];
CCSequence* sequence = [CCSequence actions:move1, move2, nil];
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:sequence];
[paraNode runAction:repeat];
#end
You need to enclose your code in an -(id)init method, like this:
#implementation Background
- (id)init
{
if (self = [super init]) {
// Load the sprites for each parallax layer, from background to foreground.
CCSprite* para1 = [CCSprite spriteWithFile:#"color.png"];
// ... the rest of your code ... //
}
return self;
}
#end

'Signature not found for selector - does it have the following form? -(void) name: (ccTime) dt' - cocos2d

I keep getting the same crash report "'Signature not found for selector - does it have the following form? -(void) name: (ccTime) dt'".
This is getting pretty annoying now, i only wanted to make the Backgound scrolling infinitely.
Here is my code:
-(id) init {
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
self.isTouchEnabled = YES;
self.isAccelerometerEnabled = YES;
CGSize screenSize = [CCDirector sharedDirector].winSize;
/*CCSprite *Player = [CCSprite spriteWithFile:#"Icon.png"];
Player.position = ccp(screenSize.width/2 -110, screenSize.height/2);
Player.scale = 0.7;
[self addChild:Player];
[self scheduleUpdate];*/
CCLOG(#"Screen width %0.2f screen height %0.2f",screenSize.width,screenSize.height);
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
bool doSleep = true;
world = new b2World(gravity, doSleep);
world->SetContinuousPhysics(true);
// Debug Draw functions
/*m_debugDraw = new GLESDebugDraw( PTM_RATIO );
world->SetDebugDraw(m_debugDraw);
uint32 flags = 0;
flags += b2DebugDraw::e_shapeBit;
//flags += b2DebugDraw::e_jointBit;
//flags += b2DebugDraw::e_aabbBit;
//flags += b2DebugDraw::e_pairBit;
//flags += b2DebugDraw::e_centerOfMassBit;
m_debugDraw->SetFlags(flags);*/
// Define the ground body.
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0, 0); // bottom-left corner
// Call the body factory which allocates memory for the ground body
// from a pool and creates the ground box shape (also from a pool).
// The body is also added to the world.
b2Body* groundBody = world->CreateBody(&groundBodyDef);
// Define the ground box shape.
b2PolygonShape groundBox;
// bottom
groundBox.SetAsEdge(b2Vec2(0,0), b2Vec2(screenSize.width/PTM_RATIO,0));
groundBody->CreateFixture(&groundBox,0);
// top
groundBox.SetAsEdge(b2Vec2(0,screenSize.height/PTM_RATIO), b2Vec2(screenSize.width/PTM_RATIO,screenSize.height/PTM_RATIO));
groundBody->CreateFixture(&groundBox,0);
//Set up sprite
BG1 = [CCSprite spriteWithFile:#"SkyAndClouds.png"];
BG1.anchorPoint = ccp(0,0);
BG1.position = ccp(0, 0);
BG1.scale = 0.5;
[self addChild:BG1];
BG2 = [CCSprite spriteWithFile:#"SkyAndClouds.png"];
BG2.anchorPoint = ccp(0,0);
BG2.position = ccp([BG1 boundingBox].size.width-1,0);
[self addChild:BG2];
[self schedule: #selector(AnimatedBG:) interval:0];
CCSpriteBatchNode *batch = [CCSpriteBatchNode batchNodeWithFile:#"Stick.png" capacity:150];
[self addChild:batch z:0 tag:kTagBatchNode];
[self addNewSpriteWithCoords:ccp(screenSize.width/2-110, screenSize.height/2)];
[self addZombieWithCoords:ccp(screenSize.width/2+240, screenSize.height/2)];
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Stick, you Better Run!" fontName:#"Helvetica" fontSize:16];
[self addChild:label z:0];
[label setColor:ccc3(0,0,255)];
label.position = ccp( screenSize.width/2, screenSize.height-17);
[self schedule: #selector(tick:)];
}
return self;
}
-(void)AnimatedBG {
BG1.position = ccp(BG1.position.x-1,BG1.position.y);
}
Your AnimatedBG method should be defined as:
-(void) AnimatedBG:(ccTime)dt {
}
You should also make sure you have a definition for your scheduled tick: method:
-(void) tick:(ccTime)dt {
}

cocos2d-iphone: cocosbuilder: CCScrollView How to position

I am using CCScrollView provided by cocosBuilder. I am trying to position it in the center of the screen using this code:
CCSprite *testSrprite = [CCSprite spriteWithFile:#"category-shop1"];
testSprite.position = ccp(0,0);
CCScrollView *test = [[CCScrollView alloc] initWithViewSize:CGSizeMake(200, 200) container:testSrprite];
test.position = ccp(winsize.width/2,winsize.height/2);
[self addChild:test];
But this is not positioning the layer. Is it a bug in CCScrollView?

CCRepeatForever action stops automatically while switching scenes

I am developing a simple game app.
I am required to have two screens one on left & other on right, just like on scroll view with pagination having 2 screens to toggle between. I am detecting swipes with the help of RootViewController class and successfully swiping left and right screens.
But the problem is I have to have infinitely running rotateBy action on both screens each running this action on single sprite placed on center of screen.
I am using a main scene called SelectWorld & two are it's sub scenes.
First screen is called factory & other is called stack.
Following is my code:
Select World Screen:
#implementation SelectWorld
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
SelectWorld* layer = [SelectWorld node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
[RootViewController singleton].swipeCallBackHandler = self;
[[RootViewController singleton] enableSwipeDetection];
factory = [[ColorfulFactory scene] retain];
stack = [[CoinsStack scene] retain];
[self addChild:factory];
isOnFactory = YES;
}
return self;
}
-(void)swipedLeft
{
if(isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = NO;
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:stack];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
-(void)swipedRight
{
if(!isOnFactory)
{
[self removeAllChildrenWithCleanup:YES];
isOnFactory = YES;
CCTransitionScene* transitionalScene = [CCTransitionSlideInL transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
}
}
Here's the code for factory only, same goes for stack-
#implementation ColorfulFactory
+(CCScene*)scene
{
CCScene* scene = [CCScene node];
ColorfulFactory* layer = [ColorfulFactory node];
[scene addChild:layer];
return scene;
}
-(id)init
{
if((self = [super init]))
{
CGSize size = [CCDirector sharedDirector].winSize;
CCLabelTTF* info = [CCLabelTTF labelWithString:#"Colorful Factory" fontName:#"Helvetica" fontSize:35.0];
info.position = ccp(size.width/2, size.height-50);
[self addChild:info];
CCSprite* sprite = [CCSprite spriteWithFile:#"Icon.png"];
sprite.position = ccp(size.width/2, size.height/2);
[self addChild:sprite];
sprite.tag = 123;
CCRotateBy* rotateBy = [CCRotateBy actionWithDuration:60 angle:360.0];
CCRepeatForever* repeatForever = [CCRepeatForever actionWithAction:rotateBy];
[sprite runAction:repeatForever];
repeatForever.tag = 456;
}
return self;
}
Now the problem is, in first two swipes actions are running fine, but as soon as I try to swipe more than two or three times, actions got stopped. I haven't written a single line to stop this in both the classes. I require it there running forever.
Your mistake is that you're re-using the same scene object for every scene change. You can't do that in cocos2d, you have to create a new instance of the scene object.
You should never retain a scene object, since there ought to be only one active scene at any one time (with the exception of the duration of a scene transitioning to another scene - but that's handled by cocos2d internally).
For example:
CCScene* factory = [ColorfulFactory scene];
CCTransitionScene* transitionalScene = [CCTransitionSlideInR transitionWithDuration:0.3 scene:factory];
[[CCDirector sharedDirector] replaceScene:transitionalScene];
If you need both objects in memory, make them layers (or nodes), not scenes. Don't use replaceScene but instead animate the layers with regular moveTo actions in and out of the screen.