0

newSprite.h / newSprite.m というスプライトをサブクラス化し、その中にスプライトを追加します。

CCSprite *nsprite = [CCSprite spriteWithFile:@"mouse.png"];
[self addChild: nsprite];

そして、gamelayer.m に次のコードを追加します。

newSprite *newp = [newSprite node];
newp.position = ccp(actualX, actualY);
[self addChild:newp];
[_NSMutableArrayName addObject:newp];

次のコードを使用して、どのスプライトに触れたかを検出すると

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  CGPoint location = [self convertTouchToNodeSpace: touch];

 for (CCSprite *target in _NSMutableArrayName) {
    if (CGRectContainsPoint(target.boundingBox, location)) {
        CCLOG(@"yes i am touched");
    }
  }
}

しかし、それは動作しません、スプライトが検出されないので、どこが間違っていますか? 助けてください、ありがとう

4

2 に答える 2

0

これを使用してみてください:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [self convertTouchToNodeSpace:touch];

    for (CCSprite *target in _NSMutableArrayName) {
        CGSize size = node.contentSize;
        CGRect r = CGRectMake(0.f, 0.f,
                              size.width, size.height);
        if (CGRectContainsPoint(r, local)) {
            CCLOG(@"yes i am touched");
        }
    }
}
于 2013-10-17T15:57:11.740 に答える
0

サブスプライトのタッチを検出しようとしており、親スプライトの境界を与えています。

まず、 nsprite をNewSpriteのクラス変数として取り、GameLayer から呼び出すときにその参照を保持します。次に、このメソッドを次のように変更してみてください。

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  UITouch *touch = [touches anyObject];
  CGPoint location = [self convertTouchToNodeSpace: touch];

  for (CCSprite *target in _NSMutableArrayName) {
    CCSize size = target.nSprite.contentSize;
    CCRect rect = CCRectMake(target.position.x - size.width/2, target.position.y - size.height/2, width, height);

    if (CGRectContainsPoint(rect, location)) {
        CCLOG(@"yes i am touched");
    }
  }
}
于 2013-10-17T20:49:57.993 に答える