I am new in cocos2d.I follow Raywenderlich tutorial series.
I wish to set turrets in below of the screen in portrait mode.when user touch that location fire bullet and turret fire position at the touch location. how can i do that.
Below is my image of turret position.
\$\begingroup\$
\$\endgroup\$
1
-
\$\begingroup\$ @AlexM.i follow the Raywenderlich tut series and i try to implement below link code. stackoverflow.com/questions/20903881/…. Thanx for your reply... \$\endgroup\$– Renish DadhaniyaJan 5, 2014 at 5:53
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (_nextProjectile != nil) return;
// Choose one of the touches to work with
UITouch *touch = [touches anyObject];
CGPoint location = [self convertTouchToNodeSpace:touch];
// Set up initial location of projectile
CGSize winSize = [[CCDirector sharedDirector] winSize];
_nextProjectile = [[CCSprite spriteWithFile:@"projectile2-Hd.png"] retain];
_nextProjectile.position = ccp(winSize.width/2,20);
// Determine offset of location to projectile
CGPoint offset = ccpSub(location, _nextProjectile.position);
NSLog(@"offset.x : %f",offset.x);
NSLog(@"offset.x : %f",offset.y);
// Bail out if you are shooting down or backwards
if (offset.y <= 0) return;
int realY = winSize.height - (_nextProjectile.contentSize.width/2);
float ratio = (float) offset.x / (float) offset.y;
int realX = (realY * ratio) + _nextProjectile.position.x;
CGPoint realDest = ccp(realX, realY);
// Determine the length of how far you're shooting
int offRealX = realX - _nextProjectile.position.x;
int offRealY = realY - _nextProjectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Determine angle to face
// float angleRadians = atanf((float)offRealY / (float)offRealX);
float angleRadians = atanf((float)offRealX / (float)offRealY);
float angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
// float cocosAngle = -1 * angleDegrees;
float cocosAngle = angleDegrees;
float rotateDegreesPerSecond = 180 / 0.5; // Would take 0.5 seconds to rotate 180 degrees, or half a circle
float degreesDiff = _player.rotation - cocosAngle;
float rotateDuration = fabs(degreesDiff / rotateDegreesPerSecond);
[_player runAction:
[CCSequence actions:
[CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
[CCCallBlock actionWithBlock:^{
// OK to add now - rotation is finished!
[self addChild:_nextProjectile];
[_projectiles addObject:_nextProjectile];
// Release
[_nextProjectile release];
_nextProjectile = nil;
}],
nil]];
// Move projectile to actual endpoint
[_nextProjectile runAction:
[CCSequence actions:
[CCMoveTo actionWithDuration:realMoveDuration position:realDest],
[CCCallBlockN actionWithBlock:^(CCNode *node) {
[_projectiles removeObject:node];
[node removeFromParentAndCleanup:YES];
}],
nil]];
_nextProjectile.tag = 2;
// [[SimpleAudioEngine sharedEngine] playEffect:@"pew-pew-lei.caf"];
}