drag on CCLayer sometimes working


Im having trouble to get the touches to work on a Custom class

-(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]; //sprite = (CCSprite *)[DragItems objectAtIndex:i]; //if(CGRectContainsPoint([sprite boundingBox], location)) if(sprite.tag ==12 && CGRectContainsPoint([sprite boundingBox],location)) { selectedSprite = sprite; //[self reorderChild:sprite z:BubbleDepthOntop]; } } } -(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; isTouched = NO; }

//little weird things... I can drag the sprites (6 bubbles) but but not every-time (they suddenly stall or if i drag my finger on an empty part of the screen a sprite suddenly jumps to that location from nowhere

I can drag but cant re-drag the same sprite I'm pretty new at this stuff so I guess I'm missing something simple, any help would be great

I have made a video of what is happening, using my old code, which has worked in other projects of mine - you'll see that the touches are working, but the object (Bubble*) is having trouble understanding that it is deselected. -

Normally I have only been using CCSprite, so I think that it is likely that im doing something wrong with the (Bubble*) class working with the touches

"selectedSprite = nil" isn't working

sometimes the custom class sprite can drag correctly, sometimes another (Bubble*) jumps to the current position of the touch (it should be impossible to drag my finger on an empty section of the screen and have a sprite appear there and yet ><....)

<a href="https://www.youtube.com/watch?v=VHaTpiVPP_w&feature=youtu.be" rel="nofollow">https://www.youtube.com/watch?v=VHaTpiVPP_w&feature=youtu.be</a>

//custom sprite #import "CCSprite.h" #import "cocos2d.h" @interface Bubble : CCLayer { CCSprite *BackBubble; CCSprite *FrontShine; CCRepeatForever *REP; CCLabelTTF *BubbleLabel; } @property (nonatomic, strong)CCSprite *BackBubble; @property (nonatomic, strong)CCSprite *FrontShine; @property (nonatomic, strong) CCLabelTTF *BubbleLabel; -(id)initWithBubbleWithLabel:(NSString*)Bubblepng opacity:(float)myOpacty gloss:(NSString*)glossypng posX:(float)X posY:(float)Y data:(int)myNumber; @end


@implementation Bubble @synthesize BackBubble,FrontShine,BubbleLabel; -(id)initWithBubbleWithLabel:(NSString*)Bubblepng opacity:(float)myOpacty gloss:(NSString*)glossypng posX:(float)X posY:(float)Y data:(int)myNumber { self=[super init]; { BackBubble = [CCSprite spriteWithSpriteFrameName:Bubblepng]; BackBubble.position = ccp(X,Y); [self addChild: BackBubble z:5]; FrontShine = [CCSprite spriteWithSpriteFrameName:glossypng]; FrontShine.position = ccp(BackBubble.contentSize.width/2,BackBubble.contentSize.height/2); [BackBubble addChild: FrontShine z:5]; //BubbleLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",myNumber]] fontName:@"Grinched" fontSize:35]; BubbleLabel = [CCLabelTTF labelWithString:[NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",myNumber]] fontName:@"Helvetica" fontSize:35]; BubbleLabel.position = ccp(FrontShine.contentSize.width/2, FrontShine.contentSize.height/2); BubbleLabel.color = ccc3(255,255,255); [BackBubble addChild:BubbleLabel z:200]; BubbleLabel.opacity =myOpacty; } return self; }

the Bubble sprites are being being added to level01 layer as child then moved wth the following code:

-(void)Scrolltick:(ccTime)ScrollTime { [self ScrollSprite:ScrollTime mySprite:Bubble01]; [self ScrollSprite:ScrollTime mySprite:Bubble02]; [self ScrollSprite:ScrollTime mySprite:Bubble03]; [self ScrollSprite:ScrollTime mySprite:Bubble04]; [self ScrollSprite:ScrollTime mySprite:Bubble05]; [self ScrollSprite:ScrollTime mySprite:Bubble06]; } -(void)ScrollSprite:(ccTime)dt mySprite:(Bubble*)mySprite { int MaxHeightofBubbles = 350; int minHeightofBubbles = 150; RandomNumber = [self generateRandomNumberBetweenMin:minHeightofBubbles Max:MaxHeightofBubbles]; float ConvertedRandom = [[NSNumber numberWithInt: RandomNumber] floatValue]; mySprite.position = ccp(mySprite.position.x+BubbleSpeed,mySprite.position.y); if (mySprite.position.x >= 1024+1) { mySprite.position = ccp (0-[mySprite boundingBox].size.width,ConvertedRandom); //mySprite.position.y+ConvertedRandom bubbleStartAgain = YES; } }


instead of CGRectContainsPoint([sprite boundingBox],location))<br /> use [self isTouchOnSprite:location:sprite]

-(BOOL) isTouchOnSprite:(CGPoint)touch:(CCSprite*)clip{ CGPoint local = [self convertToNodeSpace:touch]; Boolean b = CGRectContainsPoint([clip boundingBox], local); if (b) { return YES; }else { return NO; } }

updated project <a href="https://drive.google.com/open?id=0B8t1yL46Hq_FNE9EUG5zVUU2Zzg&authuser=0" rel="nofollow">link</a><br /> let me know if this works .


  • Incorrect moving direction when object in midair
  • Why does this perl script run way slower than it's c counterpart?
  • will this sinus approximation be faster than a shader CG sinus function?
  • JS Events that don't bubble (progress, loadedmetadata, etc)
  • Find and Replace using patterns in Xcode 8
  • Get last statement result in embedded IronPython v2
  • Pygame Line of Sight from Fixed Position
  • Is it normal that my cocos2d app increase real memory usage every second?
  • Knowing if a previous handler for an event returned false. defaultPrevented in IE < 9?
  • How to check if the screen hasn't been clicked in Android?
  • jQuery.on “touchstart mousedown” - both events are triggered on touchscreen device
  • return common fields in two files
  • How to Optimize mach_msg_trap
  • How to cache images only in disk using Kingfisher?
  • Setting a variable in a for loop (with temporary variable) Lua
  • JQuery UI selectable plugin - Multiple mouse drag selection and unselect option
  • Why do native C++ projects have a TargetFrameworkVersion?
  • Change color of row programmatically in WatchKit
  • Can't CTRL+Drag NSButton to custom NSView header
  • Vim ctags behaves strangely
  • jquery draggable stop event
  • Android libgdx prefs getting lost
  • init_seg and warning C4073 from library code?
  • How to print columns containing value
  • NUnit 3.0 TestCase const custom object arguments
  • xcode don't localize specific strings
  • Create Instant using a negative year
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Is there a way to do normal logging with EureakLog?
  • How to use remove-erase idiom for removing empty vectors in a vector?
  • Alert pop up with LWUIT
  • Get one-time binding to work for ng-if
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Why is an OPTIONS request sent to the server?
  • Fetching methods from BroadcastReceiver to update UI
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]