0

我编写了以下程序,它必须执行以下操作:当用户触摸移动精灵时,它必须从场景中移除。

但是,当我运行我的代码时,会发生以下事情:当我触摸最上面的精灵时,它会消失在它的邻居身上。我该如何解决?

这是代码。

UPD:我已经在较小的 *.png 文件上测试了这段代码,一切正常。但是在更大的 *.png 文件(如 200x200 像素,iPhone 模拟器)上,我有一个错误:对象在 touchEvent 上与其最近的邻居一起删除。

定义

    #import <Foundation/Foundation.h>
#import "cocos2d.h"


@interface GameplayLayer : CCLayer {

    NSMutableArray *arrayOfSprites;

}
@end
    #import "GameplayLayer.h"
@implementation GameplayLayer
    -(id)init
{
    self = [super init];
    self.isTouchEnabled=YES;
    arrayOfSprites=[[NSMutableArray alloc] init];

    if (self != nil) 
    {
        int j=100;

        for (int i=0; i<=2; i++) {
            [arrayOfSprites addObject:[CCSprite spriteWithFile:@"sv_anim_1-hd.png"]];
            [[arrayOfSprites objectAtIndex:i] setPosition:CGPointMake(j,j)];
            [self addChild:[arrayOfSprites objectAtIndex:i] z:0 tag:i];
            j+=100;
        }
        [self startRunning];
    }
    return self;                                 
}
    -(void)startRunning
{
    CGSize screenSize=[[CCDirector sharedDirector] winSize];
    for ( CCSprite * currentSprite in arrayOfSprites) 
    {
        [currentSprite runAction:[CCMoveTo actionWithDuration:10 position:CGPointMake(screenSize.height,screenSize.width/2)]];
    }
}
        -(void) registerWithTouchDispatcher
    {
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0
                                                  swallowsTouches:YES];
    }

        -(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
        return YES;
    }

        -(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
        CGPoint locationOfTouch=[self convertTouchToNodeSpace: touch];

        for (CCSprite *currentSprite in arrayOfSprites)
        {
            if (CGRectContainsPoint(currentSprite.boundingBox, locationOfTouch))
            {
                NSLog(@"Sprite was touched");
                [self removeChild:currentSprite cleanup:YES];
            }
        }

    }
    @end
4

1 回答 1

0

试试这个功能:

-(CGRect)getSpriteRect:(CCNode *)inSprite
{
    CGRect sprRect = CGRectMake(
                                inSprite.position.x - inSprite.contentSize.width*inSprite.anchorPoint.x,
                                inSprite.position.y - inSprite.contentSize.height*inSprite.anchorPoint.y,
                                inSprite.contentSize.width, 
                                inSprite.contentSize.height
                                ); 

    return sprRect;
}


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


    for (CCSprite *currentSprite in arrayOfSprites)
    {
      CGRect rect = [self getSpriteRect:currentSprite];

      if (CGRectContainsPoint(rect, location))
      {
          NSLog(@"Sprite was touched");
          [self removeChild:currentSprite cleanup:YES];
       }
     }
}
于 2012-07-27T06:23:45.960 回答