0

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)に達すると、画面から消えるまで進み続けます。
問題の説明がそれほど明確ではないとあなたが言うなら、後で私はビデオをアップロードすることができます。

4

1 に答える 1

1

目的地が間違っている可能性があります(ロギングが役立ちます)。ゾンビの現在の位置は親のノード空間座標にあり、ゾンビのノード空間表記にある宛先に移動します。

于 2012-12-06T17:54:21.343 に答える