无法在拖动事件中取消选择我的CCSprite,selectedSprite = nil无法正常工作

时间:2014-10-20 09:22:20

标签: cocos2d-iphone

我正在使用以前在其他项目中工作的代码,唯一的区别是这次我使用的是自定义精灵类似乎没有取消选择 我在这里制作了一个关于问题的视频

https://www.youtube.com/watch?v=VHaTpiVPP_w&feature=youtu.be

-(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];


DragItems = [[NSMutableArray alloc]initWithObjects:

         Bubble01,
         Bubble02,
         Bubble03,
         Bubble04,
         Bubble05,
         Bubble06,
         nil];

for(int i = 0; i < [DragItems count]; i++)
{
sprite = (Bubble *)[DragItems  objectAtIndex:i];
//if(sprite.tag ==12 && CGRectContainsPoint([sprite boundingBox],location))
if(CGRectContainsPoint([sprite boundingBox], location))
{
    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];

selectedSprite.position = ccp(location.x, location.y);
NSLog(@"Position: %f %f",location.x, location.y);
}

-(void) ccTouchesEnded:(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];

selectedSprite = nil;

}

1 个答案:

答案 0 :(得分:0)

drag on CCLayer sometimes working

在rahul_send89的帮助下,我们解决了这个问题, 首先,自定义类cclayer被更改为接受自己的触摸,然后在init中 [self setContentSize:[BackBubble boundingBox] .size];用来确定图层的大小 (所以其他接触火,这可能会让我感到烦恼)

上面的链接包含所有代码。再次感谢您的帮助!