2013-04-12 1 views
1

저는 Cocos 2d에서 회전식 포탑에 관한 Ray Wenderlich의 튜토리얼을 토대로 작업 중입니다 (여기를 참조하십시오 : http://www.raywenderlich.com/25791/rotating-turrets-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-2 참조). 내 게임이 세로 모드에 있어야하므로 포탑의 위치를 ​​정확하게 얻을 수있었습니다. enter image description here인물 모드에서의 오프셋 문제 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

답변

0

실제로 응용 프로그램을 세로 모드로 실행하도록 설정 했습니까? 아니면 방금 시뮬레이터를 회전시키고 터렛의 위치를 ​​변경 했습니까?

앱을 세로 방향으로 실행하도록 명시 적으로 설정하지 않으면 x 좌표와 y 좌표가 바뀝니다 (x는 예상대로 건너 뛰지 않고 ios 버튼에서 전화 상단으로 실행됩니다).

이 :) 제대로 I have answered this question before 변환된다

+0

안녕하세요.나는 당신의 대답을 당신의 링크 된 대답에서 사용했고 그것은 당신이 직접적으로 위쪽으로 쏘을 때를 제외하고는 효과적입니다, 발사체의 속도는 나머지 것보다 훨씬 더 빠릅니다. +50에 대한 아이디어가 있습니까? –

+0

realMoveDuration 값을 똑바로 올리면 값을 확인해야합니다. 이것은 발사체의 속도이며 발사체의 끝점으로부터의 거리로 계산됩니다. NSLog에서 length 변수와 realMoveDuration을 알아 내야합니다. 그러면 문제를 발견해야합니다. – davbryn

2

확인하려는 X 축;

+0

이것을 바꾸면 터렛이 아래를 향하고 내가 튕기는 곳에서 촬영하는 것이 아니라 촬영합니다. –

+1

y 좌표를 뒤집어 봅니다. 다른 레이아웃과 달리 cocos2d에는 하단에 y0이 있습니다. – gheese

0

여기이 문제는 당신이 수학을 복사 - 붙여 넣기 대신의 용도에 적절하게 편집했습니다 것입니다 경우. 레이의 코드에서 상향, 하향 또는 좌 대신 포탑의 오른쪽으로 항상 쏘는 것에 의존하는 몇 가지 가정이 있습니다. 여기

당신이보고해야 할 수학 코드입니다 : 탭 위치가 포탑의 왼쪽에 있다면 당신은 offset.x 0보다 작은있을 것이라는 점을 여기

// 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; 

주, 그래서 이것은이다 당신은 레이 (Ray)에게서 가져 왔지만 개정하지 않았습니다. gheesse가 말했듯이, 당신의 목적을 위해, 이것은 발사체의 원래 위치의 남쪽으로 쏘고 싶지 않기 때문에 offset.y로 설정되어야합니다. 그러나 이것은 여기의 문제의 일부일뿐입니다.

// Determine where you wish to shoot the projectile to 
int realX = winSize.width + (_nextProjectile.contentSize.width/2); 

여기 또 다른 큰 문제가 있습니다. 발사체가 어디로 가야 하는지를 결정하기 위해 레이의 수학을 수정하지 않았습니다. Ray의 코드에서, 그의 발사체는 화면 오른쪽에있는 위치에서 항상 끝나기 때문에 화면의 폭과 발사체의 크기를 사용하여 발사체가 원하는 실제 위치를 결정합니다. 이것은 당신이 당신의 발사체가 항상 옳다 머리 것이라는 가정을 가지고 있지 않기 때문에 문제를 일으키는 - 당신은 항상 올라갈 것 (힌트, 이와 유사한 코드가 정말 사용한다)

float ratio = (float) offset.y/(float) offset.x; 
int realY = (realX * ratio) + _nextProjectile.position.y; 

다시, 레이 그의 게임에 대한 그의 수학에 가정을하고 당신은 이것을 진정으로 교정하지 않았습니다. 귀하의 코드는 real 대신 realX 좌표에 영향을주는 터렛을 터 닝합니다. Ray는 항상 올바른 포탑을 촬영해야하는 좌표입니다.

당신은 앉아서 다시해야합니다.

+0

내 코드가 완전 쓰레기라는 것을 알게되어 유감입니다. 그러나 좌표를 뒤집어 썼습니다 만 찾을 수있는 것은 아무것도 아닙니다. 처음부터 시작해서 사람들이 어떤 분야에서 일할 지 지적 할 수 있다면 좋을 것이라고 생각했습니다. 내가 필요로하는 것은 나를 위해 좌표를 뒤집을 누군가입니다. –