cocos2d iphone - drag on CCLayer sometimes working -



cocos2d iphone - drag on CCLayer sometimes working -

im having problem touches work on 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 = 0; < [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... can drag sprites (6 bubbles) but not every-time (they stall or if drag finger on empty part of screen sprite jumps location nowhere

i can drag cant re-drag same sprite i'm pretty new @ stuff guess i'm missing simple, help great

i have made video of happening, using old code, has worked in other projects of mine - you'll see touches working, object (bubble*) having problem understanding deselected. -

normally have been using ccsprite, think im doing wrong (bubble*) class working touches

"selectedsprite = nil" isn't working

sometimes custom class sprite can drag correctly, (bubble*) jumps current position of touch (it should impossible drag finger on empty section of screen , have sprite appear there , yet ><....)

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

//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; } homecoming self; }

the bubble sprites beingness being added level01 layer kid moved wth next 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)) utilize [self istouchonsprite:location:sprite]

-(bool) istouchonsprite:(cgpoint)touch:(ccsprite*)clip{ cgpoint local = [self converttonodespace:touch]; boolean b = cgrectcontainspoint([clip boundingbox], local); if (b) { homecoming yes; }else { homecoming no; } }

updated project link allow me know if works .

cocos2d-iphone

Comments

Popular posts from this blog

assembly - What is the addressing mode for ld, add, and rjmp instructions? -

vowpalwabbit - Interpreting Vowpal Wabbit results: Why are some lines appended by "h"? -

Is there a way to convert an HTML page styled with Bootstrap CSS into email-compatible html? -