sprite kit - How to detect in touchesEnded which nodes are still being pressed -
i've been stuck on problem days. have multiple skspritenode's, 1 left arrow, right arrow , arrow. when hold down right arrow want character continue moving right while being held down, on other hand if press arrow jump once regardless of if hold down. problem example when hold right arrow , press arrow, touchesended called , stops character moving right though still have finger on right arrow
-(void) touchesbegan:(nsset *)touches withevent:(uievent *)event { [super touchesbegan:touches withevent:event]; (uitouch *touch in touches){ cgpoint location = [touch locationinnode:self]; if (cgrectcontainspoint(rightarrow.frame, location)){ [wizard settexture:[sktexture texturewithimagenamed:@"wizardright"]]; didtouchrightarrow = yes; islookingright = yes; islookingleft = no; rightarrow.alpha = 0.5; nslog(@"touching right"); } if (cgrectcontainspoint(leftarrow.frame, location)){ [wizard settexture:[sktexture texturewithimagenamed:@"wizardleft"]]; islookingright = no; islookingleft = yes; didtouchleftarrow = yes; leftarrow.alpha = 0.5; nslog(@"touching left"); } if (cgrectcontainspoint(uparrow.frame, location)){ didtouchuparrow = yes; uparrow.alpha = 0.5; nslog(@"touching up"); } -(void) touchesended:(nsset *)touches withevent:(uievent *)event { [super touchesbegan:touches withevent:event]; if (rightarrow.alpha != 1.0){ rightarrow.alpha = 1.0; } if (leftarrow.alpha != 1.0){ leftarrow.alpha = 1.0; } if (uparrow.alpha != 1.0){ uparrow.alpha = 1.0; (uitouch *touch in touches){ cgpoint location = [touch locationinnode:self]; if (cgrectcontainspoint(rightarrow.frame, location)){ nslog(@"touching right"); didtouchrightarrow = yes; } { nslog(@"not touching right"); didtouchrightarrow = no; } if (cgrectcontainspoint(leftarrow.frame, location)){ nslog(@"touching left"); didtouchleftarrow = yes; } else { nslog(@"not touching left"); didtouchleftarrow = no; } didtouchuparrow = no; }
this may not right way approach problem, in touchesended trying see if touch still in desired rect.
you need way identify different nodes registering touch. there more 1 way have found using name property of node simplest , easiest work with.
you have right idea using bools register touch states.
i wrote code handle trying accomplish:
#import "gamescene.h" @implementation gamescene { skspritenode *node0; skspritenode *node1; bool node0touch; bool node1touch; } -(void)didmovetoview:(skview *)view { self.backgroundcolor = [skcolor blackcolor]; node0 = [skspritenode spritenodewithcolor:[skcolor redcolor] size:cgsizemake(100, 100)]; node0.name = @"node0"; node0.position = cgpointmake(100, 300); [self addchild:node0]; node1 = [skspritenode spritenodewithcolor:[skcolor bluecolor] size:cgsizemake(100, 100)]; node1.name = @"node1"; node1.position = cgpointmake(400, 300); [self addchild:node1]; node0touch = false; node1touch = false; } -(void)touchesbegan:(nsset *)touches withevent:(uievent *)event { (uitouch *touch in touches) { cgpoint touchlocation = [touch locationinnode:self]; sknode *node = [self nodeatpoint:touchlocation]; if([node.name isequaltostring:@"node0"]) node0touch = true; if([node.name isequaltostring:@"node1"]) node1touch = true; } } -(void)touchesended:(nsset *)touches withevent:(uievent *)event { (uitouch *touch in touches) { cgpoint touchlocation = [touch locationinnode:self]; sknode *node = [self nodeatpoint:touchlocation]; if([node.name isequaltostring:@"node0"]) node0touch = false; if([node.name isequaltostring:@"node1"]) node1touch = false; } } -(void)update:(cftimeinterval)currenttime { if(node0touch) nslog(@"node0 touch"); if(node1touch) nslog(@"node1 touch"); }
Comments
Post a Comment