2012-08-05 4 views
0

나는 사용자가 상단의 상태 표시 줄에 병 하나를 던져서 깰 수있는 간단한 게임을하고있다. 사용자가 병 (UIView)을 잡으면 테스트하기 위해 touchesMoved를 사용하고 있으며 사용자의 손가락을 튀기는 방향에 응답하여 throw됩니다. 던지는 부분에 대해서는 작동하지만, 부러 지거나 부러진 병 png로 바꾸기를 원합니다. 프레임에서 벗어나면이 부분에 두통이 생깁니다. 여기에 코드의 일부입니다 지금은움직이는 물체가 프레임에 닿았을 때 어떻게 멈출 수 있습니까?

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event 
{ 

UITouch *aTouch = [touches anyObject]; 
CGPoint location = [aTouch locationInView:self.view]; 

throwView = [self.view hitTest:location withEvent:nil]; 

//if the movement not on self.view, it's on the bottle. 
if (bottleView != self.view) { 
CGPoint loc = [aTouch locationInView:self.bottleView]; 
CGPoint prevloc = [aTouch previousLocationInView:self.bottleView]; 

//getting the finger moment 
myFrame = self.bottleView.frame; 
deltaX = loc.x - prevloc.x; 
deltaY = loc.y - prevloc.y; 

myLocation = bottleView.center; 


//Here I try to multiply the finger movement to throw the bottles 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:.5]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

myFrame.origin.x += deltaX*7.0; 
myFrame.origin.y += deltaY*7.0; 
[self.bottleView setFrame:myFrame]; 
[bottleView setNeedsDisplay]; 
[UIView commitAnimations]; 


//if it hits the top of the frame.. 
    if (myFrame.origin.y <=20){ 
     [self hitTheTop]; 
    } 
}} 


    //this part gives me headache, I repeat the code again otherwise the bottle is broken before hitting the top. 

-(void) hitTheTop{ 
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:.5]; 
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

myFrame.origin.x += deltaX*7.0; 
myFrame.origin.y += deltaY*7.0; 
[self.bottleView setFrame:myFrame]; 
[bottleView setNeedsDisplay]; 
[UIView commitAnimations]; 

//I calculate the slope and find where x should be when y is 0, 40 is half width of the bottle 
myFrame.origin.x = myLocation.x + (myLocation.y/(-deltaY/deltaX)) - 40; 
myFrame.origin.y = 0; 

UIImageView *bottleBroken= 
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"broken.png"]]; 
[bottleBroken setFrame:CGRectMake(-20, -30, 130, 180)]; 
[self.bottleView setFrame:myFrame]; 
[bottleView addSubview:bottleBroken]; 
} 

, 병이 프레임 밖으로 날아 처음으로 내가 얻을, 아주 잠시 동안, 그것은 프레임의 가장자리에 다시 나타납니다 이미 그것을 고장 정말로 부 자연스럽게 보입니다. 사실 나는 던지기를 표시하는 타이머를 넣으 려하고 더 잘 관리 할 수 ​​있습니다. 그러나 나는 타이머가 없다면 그것을 할 수있는 더 쉬운 방법이 있다면 탐험하고 싶다.

답변

0

다음은 내 게임에서 충돌 감지를 수행하는 방법입니다.

NSMutableArray *collidees = [NSMutableArray arrayWithArray:[[GRSceneController sharedSceneController] sceneObjects]]; 

[collidees removeObject:self]; 

NSMutableArray *collisions = [[NSMutableArray alloc] init]; 

position.y += speed.y; 

for (GRBlock *obj in collidees) { 
    if ([self.collider doesCollideWithCollider:obj.collider]) 
     [collisions addObject:obj]; 
} 

if ([collisions count]) { 
    [(GRSceneObject<GRColliderDelegate> *)self didCollideWith:collisions collisionType:kVerticalCollision]; 
} 

[collisions removeAllObjects]; 

그럼 난 그냥 position.x

doesCollideWithCollider: 방법이

-(BOOL)doesCollideWithCollider:(GRColliderObject *)aCollider { 

    #if defined DEBUG_COLLIDER && defined LOG 
    logRect(hitBox); 
    logRect(aCollider.hitBox); 
    #endif 

    CGRect colliderBox = aCollider.hitBox; 

    return CGRectIntersectsRect(hitBox, colliderBox); 
} 

hitBox처럼 보인다는 입자 가속기와 같은 객체와 행위를 둘러싼 단지 박스를 위해 같은 일을 반복하고 didCollideWith:collisionType: 충돌을 처리합니다.

가장 중요한 것은 속도에 대한 변수가 있어야하고 어떤 종류의 속도를 생성하려면 position.y += speed.y이어야한다는 것입니다. 그리고 업데이트 후 position.y과 수평 충돌을 업데이트 한 후 수직 충돌을 확인하십시오. position.x

+0

"GRSceneController"란 무엇인가요? OPENGL에서 개발자 라이브러리를 찾을 수 없습니까? 내 생각에 내가 아는 바를 넘어선 것 같아. 미안 해요, 객관적으로는 아직 새로운데, 심지어 오픈 GL에도 준비가 안됐어. – Walter

+0

아, 그건 내가 만든 장면 컨트롤러 일 뿐이니까, 어디서나 찾을 수는 없을 것입니다. 그것은 단지 일반적인 지침으로 의미가 있으며, 복사 및 붙여 넣기가 아닙니다. –

관련 문제