2012-12-06 2 views
0

나는 좀비 object.It 그냥 좀비 이미지를로드하고 사용자가 이동할 수 있도록 CCSprite를 서브 클래스 화 한 이동시키기 :적인 Cocos2D CCSprite 객체는 너무 많은

@implementation Zombie 

@synthesize speed; // CGFloat 


- (id) initWithPosition: (CGPoint) position 
{ 
    if(self= [super initWithFile: @"zombie_icon.png"]) 
    { 
     CCTouchDispatcher* dispatcher=[CCTouchDispatcher sharedDispatcher]; 
     self.position=position; 
     self.scale= 0.25; 
     speed= 50.0; 
     [dispatcher addTargetedDelegate: self priority: 0 swallowsTouches: YES]; 
    } 
    return self; 
} 

#pragma - mark Movements 

- (NSTimeInterval) timeFromDestination: (CGPoint) destination 
{ 
    CGFloat distance= sqrt(pow (fabs(self.position.x-destination.x),2) + pow (fabs(self.position.y-destination.y),2)); 
    return distance/speed; 
} 

- (BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    return YES; 
} 

- (void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event 
{ 
    CGPoint location=[self convertTouchToNodeSpace: touch]; 
    NSTimeInterval duration= [self timeFromDestination: location]; 
    [self runAction: [CCMoveTo actionWithDuration: duration position: location]]; 
} 



@end 

그래서 내 계층에서 나는이 수행

Zombie* zombie=[[Zombie alloc]initWithPosition: CGPointMake(200, 250)]; 
[self addChild: zombie]; 
[zombie release]; 

때로는 좀비가 제대로 움직이지 않는 경우가 있습니다.
예를 들어 좀비는 (100,100)에 있고, 나는 (200,200)을 클릭하면 그 방향으로 움직이지만, (200,200)에 도달하면 화면에서 벗어날 때까지 계속 움직입니다.
나중에 문제의 설명이 너무 명확하지 않다고 말하면 동영상을 업로드 할 수 있습니다.

답변

1

아마도 목적지가 잘못되었을 수 있습니다 (로깅 도움). 좀비의 현재 위치는 부모의 노드 공간 좌표이며, 좀비의 노드 공간 표기법을 사용하는 목적지로 이동합니다.

+0

사실, [self.parent convertTouchToNodeSpace : touch];라고 써야했습니다. –