저는 Cocos 2d에서 회전식 포탑에 관한 Ray Wenderlich의 튜토리얼을 토대로 작업 중입니다 (여기를 참조하십시오 : http://www.raywenderlich.com/25791/rotating-turrets-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-2 참조). 내 게임이 세로 모드에 있어야하므로 포탑의 위치를 정확하게 얻을 수있었습니다. 인물 모드에서의 오프셋 문제 Cocos2d
포탑은 오른쪽으로 쏠 수 있지만 왼쪽으로는 촬영하지 못합니다. 여기 내 코드는 다음과 같습니다 도움을
- (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.png"] retain];
_nextProjectile.position = ccp(160, 20);
// Determine offset of location to projectile
CGPoint offset = ccpSub(location, _nextProjectile.position);
// Bail out if you are shooting down or backwards
if (offset.x <= 0) return;
// Determine where you wish to shoot the projectile to
int realX = winSize.width + (_nextProjectile.contentSize.width/2);
float ratio = (float) offset.y/(float) offset.x;
int realY = (realX * ratio) + _nextProjectile.position.y;
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 angleDegrees = CC_RADIANS_TO_DEGREES(angleRadians);
float cocosAngle = -1 * 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;
}
감사합니다! 대신
// Bail out if you are shooting down or backwards
if (offset.x <= 0) return
Y
의
안녕하세요.나는 당신의 대답을 당신의 링크 된 대답에서 사용했고 그것은 당신이 직접적으로 위쪽으로 쏘을 때를 제외하고는 효과적입니다, 발사체의 속도는 나머지 것보다 훨씬 더 빠릅니다. +50에 대한 아이디어가 있습니까? –
realMoveDuration 값을 똑바로 올리면 값을 확인해야합니다. 이것은 발사체의 속도이며 발사체의 끝점으로부터의 거리로 계산됩니다. NSLog에서 length 변수와 realMoveDuration을 알아 내야합니다. 그러면 문제를 발견해야합니다. – davbryn