CCEffectGlass in Cocos2d - cocos2d-iphone

I used CCEffectGlass in Cocos2d 3.2, it works good but I see double layer Or no layer at end like in image. Is it possible to hide double layer only inside circle ?
CODE:
+(void)showGlassEffectForSprite:(CCSprite*)effectBG parent:(CCNode*)inParent
{
CCSpriteFrame *normalMap = [CCSpriteFrame frameWithImageNamed:#"gtEffectsImage.png"];
CCSprite *reflectEnvironment = [CCSprite spriteWithImageNamed:#"gtEffectsGlass.jpg"];
reflectEnvironment.positionType = CCPositionTypeNormalized;
reflectEnvironment.position = ccp(0.5f, 0.5f);
reflectEnvironment.visible = NO;
[inParent addChild:reflectEnvironment];
CCEffectGlass *glass = [[CCEffectGlass alloc] initWithShininess:1.0f refraction:1.0f refractionEnvironment:effectBG reflectionEnvironment:reflectEnvironment];
glass.fresnelBias = 0.1f;
glass.fresnelPower = 2.0f;
glass.refraction = 0.75f;
CCSprite *sprite1 = [[CCSprite alloc] init];
sprite1.position = ccp(SW*0.1f, effectBG.position.y);
sprite1.normalMapSpriteFrame = normalMap;
sprite1.effect = glass;
sprite1.scale = 0.8f;
sprite1.colorRGBA = [CCColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.0f];
float duration = 4.0f ;
[sprite1 runAction:[CCActionRepeatForever actionWithAction:[CCActionSequence actions:
[CCActionMoveTo actionWithDuration:duration position:ccp(SW*1.4f, effectBG.position.y)],
[CCActionDelay actionWithDuration:1.0f],
[CCActionMoveTo actionWithDuration:duration position:ccp(-SW*0.4f, effectBG.position.y)],
[CCActionDelay actionWithDuration:1.0f],
nil
]]];
[inParent addChild:sprite1 z:5];
}
FULL Xcode Source : https://github.com/Gururajtallur/CCEffectGlassSample

Related

How do we enumerate and modify positions array of CCsprite inside Update method?

How do we enumerate and alter our object's position (contained in array) for each delta time?
I put some CCsprite objects inside array, then I displayed them in scene, but also I wanted to make them move with modifying update method, I failed on last part.
How do I get around this ?
#implementation GameScene
{
Hunter *_hunter;
Bird *_bird;
NSMutableArray *_arrayOfBirds;
}
-(void)update:(CCTime)delta{
CGSize viewSize = [CCDirector sharedDirector].viewSize;
float birdSpeed = 50;
for (Bird *birds in _arrayOfBirds) {
if (birds.position.x < 0) {
birds.flipX = YES;
}
if (birds.position.x > viewSize.width) {
birds.flipX = NO;
}
float distanceToMove = birdSpeed * delta;
float direction = birds.flipX ? 1 : -1;
float newX = birds.position.x + direction * distanceToMove;
float newY = birds.position.y;
birds.position = ccp(newX, newY);
}
}
-(void)addBird{
CGSize viewSize = [CCDirector sharedDirector].viewSize;
for (int i=0; i < 4; i++) {
_bird = [[Bird alloc]initWithBirdType:(i)];
_bird.position = ccp(viewSize.width * 0.5f + 30 * i , viewSize.height * 0.9f - 15* i);
[self addChild:_bird];
[_arrayOfBirds addObject:_bird];
}
}
You forgot to initialize your array
here (assuming ARC)
-(id) init {
if(self=[super init]) {
_arrayOfBirds = [[NSMutableArray alloc] init];
// the rest
}
return self;
}

Cocos2d-iphone v3 - draw a line and convert it so I could use it for CCSprite

http://i.stack.imgur.com/cjZBq.png
Right I've looked and I can seem to find the answer.
I'm using Cocos2d-iphone V3.
What I want to achieve is the above.
I want to take the devices width or height and and draw two lines.
I want to take the drawn lines and use the for the CCProgress.
Question:
How do I take the drawn lines and convert them to CCSprite?
If you can give me the code it would be great, but if you could give me a link to any tutorial or documentation so I can learn how to do this, that would be even better.
Thank you.
#LearnCocos2d
So i'm doing this now but I get a very weird result;
CGSize winSize = [[CCDirector sharedDirector] viewSize];
_progressWidth = winSize.width *2;
//CGPoint position = ccp(0.3, 0.5);
//CCTime interval = 1.0/60.0;
NSMutableArray * tempProgressFg = [[NSMutableArray alloc] initWithCapacity:[Utilities getRows]+1];
NSMutableArray * tempProgressBg = [[NSMutableArray alloc] initWithCapacity:[Utilities getRows]+1];
CCTexture * bg = [_progressBg texture];
CCTexture * fg = [_progressFg texture];
for (int i = 0; i < [Utilities getRows]; i++)
{
if (i == 0)
{
_progressBg.scaleX = _progressWidth;
_progressFg.scaleX = _progressWidth;
_progressBg.position = ccp(0, i * TILE);
_progressFg.position = ccp(0, i * TILE);
[tempProgressFg addObject:_progressFg];
[tempProgressBg addObject:_progressBg];
}
else
{
CCSprite * tempBg = [CCSprite spriteWithTexture:bg];
CCSprite * tempFg = [CCSprite spriteWithTexture:fg];
tempBg.scaleY = 1;
tempFg.scaleY = 1;
tempBg.scaleX = _progressWidth;
tempFg.scaleX = _progressWidth;
tempBg.position = ccp(0, i * TILE);
tempFg.position = ccp(0, i * TILE);
[self addChild:tempBg];
[self addChild:tempFg];
[tempProgressBg addObject:tempBg];
[tempProgressFg addObject:tempFg];
}
}
The result that i get is in this picture:
http://imgur.com/1dMpiUc,V47d65X
is the _progressBg and Fg;
are the tempBg and Fg (multiples of them);
I want to have them all look like number 1.
Am I doing something wrong? is that the correct way to coppy the bar?

Getting EXC_BAD_ACCESS signal received when i tried to get collision detection

I stuck at this position and don't know what went wrong in this,
I have enabled ARC in my project. And i made softBody as follows
Ball.h
B2Body *body[NUM_SEGMENT];
CCSprite *ball;
Ball.mm
ball = [CCSprite spriteWithFile:#"Ball1.2.png"];
ball.tag = 1;
for(int i=0;i<NUM_SEGMENT;i++){
float theta = deltaAngle*i;
float x = radius * cosf(theta);
float y = radius * sinf(theta);
b2Vec2 circlePosition = b2Vec2(x/PTM_RATIO,y/PTM_RATIO);
b2BodyDef bodyDef;
bodyDef.type = b2_dynamicBody;
bodyDef.position = (center + circlePosition);
bodyDef.userData = &ball;
body[i] = world->CreateBody(&bodyDef);
outerBodyFixture[i]=body[i]->CreateFixture(&fixtureDef);
[bodies addObject:[NSValue valueWithPointer:body[i]]];
}
And I have given physics to the tiles as follows,
Tile.h
CCSprite *tile;
Tile.mm
tile = [layer1 tileAt:ccp(i, j)];
tile.tag = 0;
b2BodyDef tileDef;
tileDef.type = b2_staticBody;
tileDef.position.Set((tile.position.x+(tile.contentSize.width/2))/(PTM_RATIO), (tile.position.y + (tile.contentSize.height/2))/PTM_RATIO);
tileDef.userData = &tile;
tileBody = world->CreateBody(&tileDef);
Now i tried to catch collision detection and I have made code which will print the tag number of colliding bodies. The code is as follows,
std::vector<MyContact>::iterator pos;
for (pos=_contactListener->_contacts.begin();
pos != _contactListener->_contacts.end(); ++pos) {
MyContact contact = *pos;
b2Body *bodyA = contact.fixtureA->GetBody();
b2Body *bodyB = contact.fixtureB->GetBody();
if (bodyA->GetUserData() != NULL && bodyB->GetUserData() != NULL) {
At this point Getting ERROR: EXC_BAD_ACCESS
CCSprite *spriteA = (__bridge CCSprite *) bodyA->GetUserData();
At this point Getting ERROR: EXC_BAD_ACCESS
CCSprite *spriteB = (__bridge CCSprite *) bodyB->GetUserData();
printf("contact :%d \n",spriteB.tag);
}
}
Don't Know whats wrong with this code,,Give me some solution for this
Your problem is that you store in userData a pointer to a pointer rather than the pointer itself.
tile is already pointing to a CCSprite instance and &tile points to a pointer to a CCSprite instance making your casting incorrect.
So change it to :
tileDef.userData = tile;

CCRotateBy actions are intermittently jumpy/jerky? Cocos2d

I have a subclass of cpCCSprite, which is just for using chipmunk spaceManager and my class looks like this:
#implementation Helmet
+(id) helmetWithGame:(Game*)game {
return [[[self alloc] helmetInit:game] autorelease];
}
- (id) helmetInit:(Game*)game {
cpShape *helmet_1;
cpShape *helmet_2;
cpShape *helmet_3;
cpShape *reference;
reference = [game.spaceManager addCircleAt:cpvzero mass:STATIC_MASS radius:2];
helmet_1 = [game.spaceManager addCircleToBody:reference->body radius:20 offset:cpv(-5, 2)];
helmet_2 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(16, -14)];
helmet_3 = [game.spaceManager addCircleToBody:reference->body radius:8 offset:cpv(8, -14)];
reference->group =1;
helmet_1->group =1;
helmet_2->group =1;
helmet_3->group =1;
[self initWithFile:#"Helmet.png"];
[self setBody:reference->body];
self.spaceManager = game.spaceManager;
self.autoFreeShapeAndBody = YES;
gameScreenSize = game.contentSize;
return self;
}
- (void) generateAndShowOn:(Game *)game {
float startX = (float)((arc4random_uniform(101)) + 100);//returns number from 100 to 200 and casts it as a float
float startY;
int endXRange = (game.contentSize.width * .8) - (game.contentSize.width * .5);
float endX = (float)((arc4random_uniform(endXRange)) + (game.contentSize.width * .5));
float endY;
BOOL shouldStartTop;
if ((arc4random_uniform(101)) < 50) {//returns number from 0 to 100 and checks to see if it is less than 50. If it is, than the helmut starts at the top
shouldStartTop = YES;
startY = game.contentSize.height + (self.contentSize.height * .5);
endY = -self.contentSize.height;
}
else {
shouldStartTop = NO;
startY = -(self.contentSize.height * .5);
endY = game.contentSize.height + self.contentSize.height;
}
self.position = ccp(startX, startY);
[game addChild:self];
ccBezierConfig bezier;
bezier.controlPoint_1 = ccp (startX, startY);
bezier.controlPoint_2 = ccp (endX, endY);
bezier.endPosition = ccp (endX, endY);
id rotate =[CCRotateBy actionWithDuration:1.5f angle:360];
id curve = [CCBezierTo actionWithDuration:1.5f bezier:bezier];
id spawn = [CCSpawn actions:rotate, curve, nil];
[self runAction:spawn];
[self schedule:#selector(doneAnimating) interval:1.6];
}
- (void) doneAnimating{
if ([[self delegate]respondsToSelector:#selector(helmetPastBounds:)]) {
[[self delegate]helmetPastBounds:self];
}
}
- (void) dealloc {
CCLOG(#"%s", __PRETTY_FUNCTION__);
[super dealloc];
}
#end
So a new instance of this class is called every second, so that there can be more than one helmet on the screen at a time. Then I have another method that is called just slightly after the acctions have finished to remove the current helmet and deallocate it. But the problem I am having is every so often the actions are jumpy/jerky and that is with the FPS at 60. Do I need to do something different with the actions or what could be causing this?

Block of code running in iPhone simulator and not on iPhone 4? (COCOS2D)

I am using the ccTouchBegan and ccTouchEnded, and for some reason there is a block of code that runs perfectly on iPhone simulator and doesn't run at all on iPhone 4. This is what my methods look like:
-(BOOL)ccTouchBegan:(UITouch*)touch withEvent:(UIEvent *)event {
firstTouch = [self convertTouchToNodeSpace:touch];
if (!self.currentFootball.footballHasEmbarked) {
//arrow.position = ccp(fPos.x, fPos.y);
float newAnc = (120 + (self.currentFootball.contentSize.width * .5f) + (arrow.contentSize.width * .5f)) / arrow.contentSize.width;
arrow.anchorPoint = ccp(-newAnc, .5);//self.currentFootball.position;
arrow.position = self.currentFootball.position;//ccp(fPos.x-120, fPos.y);
arrow.rotation = 180;
arrow.visible = YES;
arrow.scale = 0.5f;
//powerLevel.position = fPos;
powerLevel.position = self.currentFootball.position;
powerLevel.rotation = 180;
powerLevel.percentage = 0;
powerLevel.visible = YES;
outLine.position = self.currentFootball.position;
outLine.rotation = 180;
outLine.visible = YES;
CCProgressFromTo *powerBarGoUp = [CCProgressFromTo actionWithDuration:1.0f from:0.0f to:100.0f];
CCProgressFromTo *powerBarGoDown = [CCProgressFromTo actionWithDuration:1.0f from:100.0f to:0.0f];
id action = [CCRepeatForever actionWithAction:[CCSequence actions:powerBarGoUp, powerBarGoDown, nil]];
[powerLevel runAction:action];
return YES;
}
else {
return NO;
}
}
-(void) ccTouchEnded:(UITouch*)touch withEvent:(UIEvent *)event {
const float max = 100;
CGPoint endTouch = [self convertTouchToNodeSpace:touch];
if (endTouch.x > firstTouch.x) {
endTouch = ccp(firstTouch.x, endTouch.y);
//CCLOG(#"I ran");
}
arrow.visible = NO;
powerLevel.visible = NO;
outLine.visible = NO;
self.currentFootball.footballHasEmbarked = YES;
self.currentFootball.spiraling = YES;
if (self.currentFootball) {
[smgr addBody:self.currentFootball.body];
}
if (CGPointEqualToPoint(endTouch, firstTouch)) {
CCLOG(#" I have run");
float anchorPointDist = ccpLength(endTouch);
float distFactor = distFromFb + anchorPointDist;
projectionAnchorPoint = ccp(firstTouch.x + distFactor,firstTouch.y);
}
CGPoint diff = ccpSub(endTouch, projectionAnchorPoint);
float len = powerLevel.percentage;
CGPoint norm = ccpNormalize(diff);
if (len > max){
len = max;
}
[self.currentFootball applyImpulse:ccpMult(norm, (len * 300))];
pos = self.currentFootball.position.y;
[self schedule:#selector(newFootball)];
}
This is the block of code that will not run on my iPhone.
if (CGPointEqualToPoint(endTouch, firstTouch)) {
CCLOG(#" I have run");
float anchorPointDist = ccpLength(endTouch);
float distFactor = distFromFb + anchorPointDist;
projectionAnchorPoint = ccp(firstTouch.x + distFactor,firstTouch.y);
}
Am I not doing something right?
Print out the values for endTouch and firstTouch. They may vary by a very small amount on device, as it's harder to keep your finger in the same location as opposed to a mouse pointer. If that's the case, you may want to accept end touches within a range of the first touch.
Perhaps there is some slight difference in the points you are comparing, making the result of that comparison always false. Try using this method instead of CGPointEqualToPoint.
BOOL ccpFuzzyEqual(CGPoint a, CGPoint b, float variance);