触及Cocos2d,有些bug

时间:2012-07-27 06:16:26

标签: ios objective-c cocos2d-iphone gesture touches

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

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

这是代码。

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

1 个答案:

答案 0 :(得分: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];
       }
     }
}