0

移動したい 6 つのスプライトがあります。

-(id) 初期化 {

if( (self=[super init]) )
{
    CGSize winSize = [[CCDirector sharedDirector] winSize];

    CCSprite * backGround = [CCSprite spriteWithFile:@"background_ipad.png"];
    backGround.position = ccp(winSize.width/2, winSize.height/2);
    [self addChild:backGround z:0];

    cloth1 = [CCSprite spriteWithFile:@"clothe_1.png"];
    cloth1.position = ccp(-200, -15);
    [self addChild:cloth1 z:1];
    cloth2 = [CCSprite spriteWithFile:@"clothe_2.png"];
    cloth2.position = ccp(130, 225);
    [self addChild:cloth2 z:2];

    cloth3 = [CCSprite spriteWithFile:@"clothe_3.png"];
    cloth3.position = ccp(365, 225);
    [self addChild:cloth3 z:3];

    cloth4 = [CCSprite spriteWithFile:@"clothe_4.png"];
    cloth4.position = ccp(-110, -15);
    [self addChild:cloth4 z:4];

    cloth5 = [CCSprite spriteWithFile:@"clothe_5.png"];
    cloth5.position = ccp(130, -20);
    [self addChild:cloth5 z:5];

    cloth6 = [CCSprite spriteWithFile:@"clothe_6.png"];
    cloth6.position = ccp(365, -15);
    [self addChild:cloth6 z:6];

}
return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

}

そして、この方法で移動します:

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL: location];
    //Sprite follows the finger movement.
    [cloth1 setPosition:location];
}

問題は、そこにスプライトを追加してスプライトを移動したいということです。//Follow the finger move にさらにスプライトを追加しようとしましたが、すべてのスプライトが指の動きに従います。1 つのスプライトを移動したい。例: 布 1 に触れると、布 1 が移動します。布 2 に触れると、布 2 が移動します。同時に両方ではありません。

誰かがこれを行う方法を教えてもらえますか?

4

2 に答える 2

1
@interface YourClass : NSObject
{
    NSMutableArray  *mSpriteArray;
    CCSprite  *mSpriteOnHand;
}

//実装時:

-(id) init 
{
  .. //your old code
  ..

   [mSpriteArray addObject: cloth1];
   [mSpriteArray addObject: cloth2];
   [mSpriteArray addObject: cloth3];
   [mSpriteArray addObject: cloth4];
   [mSpriteArray addObject: cloth5];
   [mSpriteArray addObject: cloth6];

}

-(void)onEnter
{
    [super onEnter];
    self.touchEnabled = YES; //    self.isTouchEnabled = YES;

}


- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    mSpriteOnHand = nil;

    for(CCSprite *cloth in mSpriteArray)
    {
        if(CGRectContainsPoint([cloth boundingBox], location))
        {
            mSpriteOnHand = cloth;
            break;
        }
    }
}


- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];

    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    if(mSpriteOnHand)
       mSpriteOnHand.position = location;

 }

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    mSpriteOnHand = nil;
}

-(void)onExit
{
   [mSpriteArray release]; 
   mSpriteArray = nil;
   [super onExit];
}
于 2013-02-21T15:53:28.120 に答える
0

returnステートメントは常に関数を終了し、呼び出し元の関数に制御を返します。あなたが書いた:

 return self;
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];

[[CCTouchDispatcher sharedDispatcher]...行は決して実行されません。

あなたのクラスは のサブクラスCCLayerですか? レイヤーのisTouchEnabledプロパティをYESに設定すると、このレイヤーが標準の (対象外の) タッチ デリゲートとして追加されます。

レイヤーでターゲットを絞ったタッチを使用する必要がある場合は、このタッチを主張している場合とそうYESでない場合に戻る必要があります。要求されたタッチの更新は、それを要求した代理人にのみ送信されます。-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)eventNO

どのスプライトがタッチされているかを判断するには: 「選択された」スプライトを格納するインスタンス変数を作成し、touchBeganメソッドでどのスプライトのバウンディング ボックスにタッチ位置が含まれているかを確認し、このスプライトをインスタンス変数に格納します (また、タッチしている場合にのみタッチを要求するように見えます)スプライト)。

[cloth1 setPosition:location];

-- スプライトはタッチ位置に「ジャンプ」します。通常、見た目は良くありません。touch と を取得locationInView:previousLocationInView:、それらを GL に変換し、差を取得して、その差によってスプライトの位置を変更します。

于 2013-02-21T15:46:25.137 に答える