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)에 도달하면 화면에서 벗어날 때까지 계속 움직입니다.
나중에 문제의 설명이 너무 명확하지 않다고 말하면 동영상을 업로드 할 수 있습니다.
사실, [self.parent convertTouchToNodeSpace : touch];라고 써야했습니다. –