0

以下のコードに示すように、複数のスプライトを追加するクラスがあります。

    CCSprite *b = [CCSprite spriteWithFile:@"b"];
    b.position = ccp(100, 160);

    CCSprite *b2 = [CCSprite spriteWithFile:@"b2.png"];
    b2.position = ccp(115, 150);

    CCSprite *b3 = [CCSprite spriteWithFile:@"b3.png"];
    b.position = ccp(200, 150);

    CCSprite *b4 = [CCSprite spriteWithFile:@"b4.png"];
    b4.position = ccp(220, 145);

    b.anchorPoint = ccp(0.98, 0.05);
    b2.anchorPoint = ccp(0.03, 0.05);
    b3.anchorPoint = ccp(0.03, 0.05);
    b4.anchorPoint = ccp(0.95, 0.05);

    [self addChild:b z:1 tag:1];
    [self addChild:b2 z:1 tag:2];
    [self addChild:b3 z:1 tag:3];
    [self addChild:b4 z:1 tag:4];

タッチイベントのコードは次のとおりです。

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

//Swipe Detection - Beginning point
beginTouch = location;

for(int i = 0; i < [hairArray count]; i++)
{
    CCSprite *sprite = (CCSprite *)[hairArray objectAtIndex:i];
    if(CGRectContainsPoint([sprite boundingBox], location))
    {
        //selectedSprite is a sprite declared on the header file
        selectedSprite = sprite;
    }
}}

-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
//Move touched sprite
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

if(selectedSprite != nil)
{
    selectedSprite.position = ccp(location.x, location.y);
}}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
//End point of sprite after dragged
NSSet *allTouch = [event allTouches];
UITouch *touch = [[allTouch allObjects]objectAtIndex:0];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector]convertToGL:location];

endTouch = location;
posX = endTouch.x;

//Minimum swipe length
posY = ccpDistance(beginTouch, endTouch);

[self moveSprite];}

アクション自体は問題なく動作しますが、問題は、b2 をドラッグしたい場合、b3 と b4 を最初にドラッグする必要があることです。z-index と関係があるのか​​ 、それとも各スプライトに存在する透明な領域が原因なのかはわかりません。私がここに欠けているものはありますか?

4

1 に答える 1

1
if(CGRectContainsPoint([sprite boundingBox], location))
{
  //selectedSprite is a sprite declared on the header file
  selectedSprite = sprite;
 }

このコードは、すべてのスプライトをループしているときに新しいスプライトが見つかるとすぐに、現在選択されているスプライトを更新します。これは、3つのスプライトが重なっている場合、選択したスプライトが親のノードの配列の最後のスプライトであることがわかることを意味します。

注文について何も仮定できないため、これは明確に希望するものではありません。スプライトを優先するポリシーを決定する必要があります。編集するanchorPointと、バウンディングボックスと比較してスプライトの位置が変わる可能性があることに注意してください(バウンディングボックスがスプライトの外側にあるように)。

確実にするには、以下を有効にする必要があります。

#define CC_SPRITE_DEBUG_DRAW 1

ccConfig.h。これにより、スプライトの周囲に境界ボックスがレンダリングされます。

于 2013-02-05T02:26:22.680 に答える