ipad - Problems with touch detection -
ipad - Problems with touch detection -
i'm learning cocos2d , objective-c, , have problems touch detection.
i have several sprites in helloworldlayer
, draggablesprites (nsmutablearray). 2 of draggablesprites located 1 on (the bottom 1 bigger).
then have code shakes touched sprite:
- (void)selectspritefortouch:(cgpoint)touchlocation { ccsprite *touchsprite = nil; (ccsprite *sprite in dragablesprites) { if (cgrectcontainspoint(sprite.boundingbox, touchlocation)) { touchsprite = sprite; break; } } if (touchsprite != selsprite) { [selsprite stopallactions]; [selsprite runaction:[ccrotateto actionwithduration:0.1 angle:0]]; ccrotateto * rotleft = [ccrotateby actionwithduration:0.1 angle:-1.0]; ccrotateto * rotcenter = [ccrotateby actionwithduration:0.1 angle:0.0]; ccrotateto * rotright = [ccrotateby actionwithduration:0.1 angle:1.0]; ccsequence * rotseq = [ccsequence actions:rotleft, rotcenter, rotright, rotcenter, nil]; [touchsprite runaction:[ccrepeatforever actionwithaction:rotseq]]; selsprite = touchsprite; } } - (bool)cctouchbegan:(uitouch *)touch withevent:(uievent *)event { cgpoint touchlocation = [self converttouchtonodespace:touch]; [self selectspritefortouch:touchlocation]; homecoming true; }
but when touch top or bottom sprites, bottom 1 shaking (it added first in init).
i think need replace code cgrectcontainspoint(sprite.boundingbox, touchlocation)
, don't know how.
i spent several nights on forums still can't understand how create top sprite shake...
try more (notice cctouchesbegan
not cctouchbegan
):
- (void)startshakingsprite:(ccsprite*)sprite { [sprite stopallactions]; [sprite runaction:[ccrotateto actionwithduration:0.1 angle:0]]; ccrotateto* rotleft = [ccrotateby actionwithduration:0.1 angle:-1.0]; ccrotateto* rotcenter = [ccrotateby actionwithduration:0.1 angle:0.0]; ccrotateto* rotright = [ccrotateby actionwithduration:0.1 angle:1.0]; ccsequence* rotseq = [ccsequence actions:rotleft, rotcenter, rotright, rotcenter, nil]; [sprite runaction:[ccrepeatforever actionwithaction:rotseq]]; } - (void)cctouchesbegan:(nsset *)touches withevent:(uievent*)event { //get touch uitouch* touch = [touches anyobject]; //get location cgpoint location = [touch locationinview:[touch view]]; //convert location cocos2d coordinates cgpoint point = cgpointmake(location.x, 480 - location.y); //now go through array of objects , see contains touch for(id sprite in draggablesprites) { if(cgrectcontainspoint(sprite.boundingbox, point)) { [self startshakingsprite:sprite]; } } }
the way loop set in selectspritefortouch
cause first object in draggablesprites
selected, because of break
statement. new code, each sprite bounding box contains touch animated. code might not perfect because typed straight chrome, idea.
another of import thing note cannot run same action on 2 different sprites, this:
id action = [ccscaleto actionwithduration:1 scale:1.0]; [somesprite runaction:action]; [someothersprite runaction:action]; [onemoresprite runaction:action];
this code not work intended; lastly sprite(onemoresprite
) animated. in order re-use cc animations multiple sprites, utilize code:
id action = [ccscaleto actionwithduration:1 scale:1.0]; [somesprite runaction:action]; [someothersprite runaction:[[action copy] autorelease]]; [onemoresprite runaction:[[action copy] autorelease]];
we create re-create of action can run sprite (someothersprite
, onemoresprite
) without removing original sprite, somesprite
. then, autorelease don't have memory leak.
ipad cocos2d-iphone touch
Comments
Post a Comment