Sunday, 15 January 2012

objective c - Sprites going the wrong way when ignoring transparent areas of image in cocos2d -



objective c - Sprites going the wrong way when ignoring transparent areas of image in cocos2d -

following guru's advice previous question, added code on init method in order prevent touch events on transparent areas of sprite:

path = cgpathcreatemutable(); cgpathmovetopoint(path, null, endtouch.x, endtouch.y); //250(y) height of path, 50(x) width //set width , height same size non-transparent part of sprite cgpathaddlinetopoint(path, null, 0, 250); cgpathaddlinetopoint(path, null, 50, 0); cgpathclosesubpath(path);

then modified touch event so:

-(void)cctouchesbegan:(nsset *)touches withevent:(uievent *)event { if(gamestat == true) { uitouch *touch=[touches anyobject]; cgpoint loc=[touch locationinview:[touch view]]; loc=[[ccdirector shareddirector] converttogl:loc]; begintouch = loc; for(int = 0; < [sprarray count]; i++) { ccsprite *sprite = (ccsprite *)[sprarray objectatindex:i]; if(cgrectcontainspoint([sprite boundingbox], loc)) { selectedsprite = sprite; //test path loc = [selectedsprite converttonodespace:loc]; if (cgpathcontainspoint(path, null, loc, no) ) { nslog(@"inside"); } else { nslog(@"outside"); } break; } } }} -(void)cctouchesmoved:(nsset *)touches withevent:(uievent *)event{ uitouch *touch = [touches anyobject]; cgpoint loc1 = [touch previouslocationinview:[touch view]]; cgpoint loc2 = [touch locationinview:[touch view]]; loc2 = [[ccdirector shareddirector]converttogl:loc2]; endtouch = location; int goup = loc2.y - loc1.y; if(goup > 0){ cgpoint locationpath = [selectedsprite converttonodespace:location]; if (cgpathcontainspoint(path, null, locationpath, no) ) { [selectedsprite setposition:location]; _spritetouch = true; } }}

now, works great problem sprites tend move wrong way. it's supposed follow touch swipe upwards no matter how swipe upwards, keeps going right instead. doing wrong here? please help me.

update: managed figure out how prepare code solved. i've updated code in case else comes same problem.

have tried changing this

loc = [selectedsprite converttonodespace:loc];

to following

loc = [selectedsprite converttoworldspace:loc];

[edit: updated sample] since phone call converttogl: point based formatted opengl based where:

coordinates y+ (0,0) |y+ | +------ y+ x- | x+ | | ---- (0,0) ---- | | | + ---- x+ | | (0,0) x+ y- opengl cocoa touch cartesian

touches origin screen (cocoa touch) not know opengl coordinate needs converted cocos2d ccdirector:

(cgpoint) converttogl:(cgpoint)p converts uikit coordinate opengl coordinate useful convert (multi) touchs coordinates current layout (portrait or landscape)

same goes other way around with

(cgpoint) converttoui:(cgpoint)p converts opengl coordinate uikit coordinate useful convert node points window points calls such glscissor

sometimes, got confuse , logging (instead of breakpoint) point determines coordinate in effect can figure out logic.

nslog(@"%@", nsstringfromcgpoint(touchpoint.position));

objective-c cocos2d-iphone

No comments:

Post a Comment