0
我写了下面的程序,它必须做到以下几点:当用户触摸移动精灵时,它必须从场景中移除。Cocos2d触摸,一些错误
但是,当我运行我的代码时,会发生以下情况:当我触摸最高的精灵时,它会消失,而它的邻居。我该如何解决它?
这是代码。
UPD:我测试了这个代码更小的* .png文件,并且一切工作正常。但是在更大的* .png文件(像200x200像素,iPhone模拟器一样),我有一个bug:对象与其最近的邻居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
THX您的回复,但它不能正常工作:还是一样的错误发生。 – 2012-07-27 08:51:03
@Taras Murzenkov,嘿编辑你的问题,并添加GameplayLayer.h接口声明,并显示代码,你初始化这个类 – Guru 2012-07-27 08:56:01
@Raj,CCSprites有一个boundingBox属性。 '我的想法yourSprite.boundingBox'。 – tallen11 2012-07-27 18:44:43