Does cocos2d support scaled position of ccsprite? - cocos2d-iphone

This is a diagram of question.
Below diagram is sprites of cocos2d.
I want to get a position of yellow sprite when green sprite scaled from 1.0 to 0.5 ratio.
I want to know if cocos2d support scaled position.
this is some code.
CCSprite *green = [CCSprite spriteWithFile:#"green.png"];
CCSprite *yellow = [CCSprite spriteWithFile:#"yellow.png"];
green.anchorPoint = CGPointZero;
yellow.anchorPoint = CGPointZero;
green.position = CGPointMake(0, 0);
yellow.position = CGPointMake(100, 100);
[green addChild:yellow];
[self addChild:green];
green.scale = 0.5;
CGPoint scaled = yellow.scaledposition(?) <=== How to get?

Try this:
CGPoint scaled = ccp(yellow.postion.x * green.scale, yellow.position.y * green.scale);

Related

CCLayerColor for Cocos2d v3

It seems CCLayerColor not found in Cocos2d 3.0
Here is my Cocos2d 2.0 code, I used CCLayerColor with 20% opacity.
ccColor4B color = {0,0,0,255};
CCLayerColor *fadeLayer = [CCLayerColor layerWithColor:color];
[self addChild:fadeLayer z:5];
fadeLayer.opacity = 128;
In Cocos2d v3, I tried CCNodeColor, but its not semi opaque..always black.
CCNodeColor *fadeLayer = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0 green:0 blue:0]];
[self addChild: fadeLayer z:5];
fadeLayer.opacity = 128;
How can I achieve colour layer with semi transparency in Cocos2d v3 ?
Problem Solved !
Solution is simple, now opacity range 0-1 not 1-255.
CCNodeColor *fadeLayer = [CCNodeColor nodeWithColor:[CCColor colorWithRed:0 green:0 blue:0]];
[self addChild: fadeLayer z:5];
fadeLayer.opacity = 0.25f; // this fixed my problem.

cocos2d sprite tag not being set

I'm using the following to create sprites with b2bodys
but I cant get the sprites tag to set. why isn't [sprite setTag:3]; working?
When I detect a collision with one of these sprites it says the sprite tag is 0
-(void) addNewSpriteAtPosition:(CGPoint)p
{
// CCLOG(#"Add sprite %0.2f x %02.f",p.x,p.y);
// Define the dynamic body.
//Set up a 1m squared box in the physics world
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position.Set(p.x/PTM_RATIO, p.y/PTM_RATIO);
b2Body *body = world->CreateBody(&bodyDef);
// Define another box shape for our dynamic body.
b2PolygonShape dynamicBox;
dynamicBox.SetAsBox(.5f, .5f);//These are mid points for our 1m box
// Define the dynamic body fixture.
b2FixtureDef fixtureDef;
fixtureDef.shape = &dynamicBox;
fixtureDef.density = 2;
fixtureDef.friction = 0.2f;
body->CreateFixture(&fixtureDef);
CCNode *parent = [self getChildByTag:kTagParentNode];
//We have a 64x64 sprite sheet with 4 different 32x32 images. The following code is
//just randomly picking one of the images
int idx = (CCRANDOM_0_1() > .5 ? 0:1);
int idy = (CCRANDOM_0_1() > .5 ? 0:1);
CCPhysicsSprite *sprite = [CCPhysicsSprite spriteWithTexture:spriteTexture_ rect:CGRectMake(32 * idx,32 * idy,32,32)];
[parent addChild:sprite];
[sprite setPTMRatio:PTM_RATIO];
[sprite setB2Body:body];
[sprite setPosition: ccp( p.x, p.y)];
[sprite setTag:3];
}

Making sprite bounce in an angle in box2d

In the game I'm trying to make, I have a ball sprite which bounces thanks to box2d. Here's how my current code looks:
-(id)init
{
ball = [CCSprite spriteWithFile:#"ball.png"];
ball.position = ccp(150, winSize.height * 0.78);
[self addChild:ball];
ball.tag = 2;
b2BodyDef ballBodyDef;
ballBodyDef.type = b2_dynamicBody;
ballBodyDef.position.Set(150/PTM_RATIO, 450/PTM_RATIO);
ballBodyDef.userData = ball;
_body = _world->CreateBody(&ballBodyDef);
b2CircleShape circle;
circle.m_radius = 26.0/PTM_RATIO;
b2FixtureDef ballShapeDef;
ballShapeDef.shape = &circle;
ballShapeDef.density = 0.5f;
ballShapeDef.friction = 1.0f;
ballShapeDef.restitution = 1.0f;
_ballFixture = _body->CreateFixture(&ballShapeDef);
b2Vec2 force = b2Vec2(160, 375);
_body->ApplyLinearImpulse(force, ballBodyDef.position);}
- (void)update:(ccTime) dt {
if(_isPaused == FALSE)
{
_world->Step(dt, 10, 10);
for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {
if (b->GetUserData() != NULL) {
CCSprite *sprite = (CCSprite *)b->GetUserData();
if(sprite.tag == 2)
{
sprite.position = ccp(b->GetPosition().x * PTM_RATIO,
b->GetPosition().y * PTM_RATIO);
sprite.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
}
}
}}
Bouncing itself works fine, my problem is there are instances wherein the ball would bounce on a straight line so to speak, either vertically or horizontally continuously which I am trying to avoid. So my question is, how can I make my ball sprite bounce at an angle instead of a straight line so it wouldn't get stuck bouncing infinitely in the same direction?
You could apply a tiny force or gravity change to the body or the world, "randomly" or at equal intervals.

Parallax scrolling on iPhone 4 and 5 screens

I am trying to implement landscape parallax scrolling which works both on iPhone 4 and the new iPhone 5. I started with a sprite which is 1136px in width (HD) and thought that I could use the same for the iPhone 4 as well. The problem is that it won't work on iPhone 4 anymore. If you're using an iPhone 5, screensize and sprite size are the same. Not so on the iPhone 4 which will result in awkward replacing of the sprite after you reached 1136px sidewards motion (i.e. the length of the sprite/iPhone 5's screen).
How can I implement endless parallax scrolling independent of the screen size / sprite size ratios?
Here is the code which updates the sprites so that they go ad infinitum (based on the code of the new Cocos2D 2 book by Itterheim):
for (CCSprite* sprite in spriteBatch.children)
{
NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];
CGPoint pos = sprite.position;
pos.x -= (scrollSpeed * factor.floatValue) * (delta * 50);
// Reposition stripes when they're out of bounds
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (pos.x < -screenSize.width)
{
pos.x += (screenSize.width * 2) - 2;
}
sprite.position = pos;
}
Here is its context:
#implementation ParallaxBackground
-(id) init
{
if ((self = [super init]))
{
CGSize screenSize = [[CCDirector sharedDirector] winSize];
// Get the game's texture atlas texture by adding it. Since it's added already it will simply return
// the CCTexture2D associated with the texture atlas.
CCTexture2D* gameArtTexture = [[CCTextureCache sharedTextureCache] addImage:#"game-art.pvr.ccz"];
// Create the background spritebatch
spriteBatch = [CCSpriteBatchNode batchNodeWithTexture:gameArtTexture];
[self addChild:spriteBatch];
bgLayerTotal = 3;
// Add the 6 different layer objects and position them on the screen
for (int i = 0; i < bgLayerTotal; i++)
{
NSString* frameName = [NSString stringWithFormat:#"bg%i.png", i];
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
sprite.anchorPoint = CGPointMake(0, 0.5f);
sprite.position = CGPointMake(0, screenSize.height / 2);
[spriteBatch addChild:sprite z:i];
}
// Add 7 more stripes, flip them and position them next to their neighbor stripe
for (int i = 0; i < bgLayerTotal; i++)
{
NSString* frameName = [NSString stringWithFormat:#"bg%i.png", i];
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName];
// Position the new sprite one screen width to the right
sprite.anchorPoint = CGPointMake(0, 0.5f);
sprite.position = CGPointMake(screenSize.width - 1, screenSize.height / 2);
// Flip the sprite so that it aligns perfectly with its neighbor
sprite.flipX = YES;
// Add the sprite using the same tag offset by numStripes
[spriteBatch addChild:sprite z:i tag:i + bgLayerTotal];
}
// Initialize the array that contains the scroll factors for individual stripes.
speedFactors = [NSMutableArray arrayWithCapacity:bgLayerTotal];
[speedFactors addObject:[NSNumber numberWithFloat:0.1f]];
[speedFactors addObject:[NSNumber numberWithFloat:3.0f]];
[speedFactors addObject:[NSNumber numberWithFloat:4.0f]];
NSAssert(speedFactors.count == (unsigned int)bgLayerTotal, #"speedFactors count does not match bgLayerTotal!");
scrollSpeed = 1.0f;
[self scheduleUpdate];
}
return self;
}
-(void) update:(ccTime)delta
{
for (CCSprite* sprite in spriteBatch.children)
{
NSNumber* factor = [speedFactors objectAtIndex:sprite.zOrder];
CGPoint pos = sprite.position;
pos.x -= (scrollSpeed * factor.floatValue) * (delta * 50);
// Reposition stripes when they're out of bounds
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (pos.x < -screenSize.width)
{
pos.x += (screenSize.width * 2) - 2;
}
sprite.position = pos;
}
}
Rather than using the screensize to repeat the background, just use the width of the largest background sprite (or the sum of the widths of the bg pieces, if they are broken up). You could also just hard-code the max width to 1136.
So, change:
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (pos.x < -screenSize.width)
{
pos.x += (screenSize.width * 2) - 2;
}
To something like:
CCSprite *bg = (CCSprite*)[spriteBatch getChildByTag:0];
if (pos.x < -bg.contentSize.width)
{
pos.x += (bg.contentSize.width * 2) - 2;
}

Cocos2D - remove layer opacity on region

I got a Scene with a layer with the top z-index wich one adds a dark effect to the whole scene setting its opacity. What i am trying to do now is to remove the shadow/dark effect in a concrete region (inside a cone/triangle) as the image shows (inside the red polygon). In other words, i want to have the same "luminosity" (zero opacity) inside the triangle as on the left side of the screen.
code:
-(id) init
{
if( (self=[super initWithColor:ccc4(0,0,0,128)] )) {
CGSize winSize = [[CCDirector sharedDirector] winSize];
CCLayerColor* layer1 = [CCLayerColor layerWithColor: ccc4(0, 0, 0, 180) width: winSize.width height: winSize.height];
layer1.position = ccp(50,0);
[self addChild: layer1 z:2];
CCSprite *background = [CCSprite spriteWithFile:#"background.png"];
background.position = ccp(background.contentSize.width/2, background.contentSize.height/2);
[self addChild:background];
CCSprite *player = [CCSprite spriteWithFile:#"Player.png"rect:CGRectMake(0, 0, 27, 40)];
player.position = ccp(player.contentSize.width/2, winSize.height/2);
[self addChild:player];
}
return self;
}
any idea on how to do this ?? maybe i should try to do it on another whay and not use a CCLayer to add the dark effect ??
Thanks in advance
The simplest way would be to use a CCSprite with a black image the size of the background. Then cut out the whole you need in an image program and draw that sprite over the background with lowered opacity.