SpriteKit - TouchesMoved continues to report touch outside the sprite? - iphone

I have a sprite using a custom SKSpriteNode class added to a scene.
I want to do this effect: suppose I have 3 sprites side by side. I want the sprite to scale a little bit if it is touched or while the finger is touching it and as soon as the finger is not touching it, it should scale back to the original size.
What I mean is this: suppose the user gives a big finger slide from left to right, starting on the first sprite and ending past the last sprite. As the finger is sliding, I want the first sprite to scale up as soon as the finger enters its frame. As the finger continues to slide to right and reaches the second sprite, I want the first one to detect that the finger is not on its area and scale down to its original size. At the same time, the second sprite would scale up, because now the finger is inside its area. At any time the finger leaves the surface during the slide, just after passing the last sprite.
My problem is that I want to do this using logic inside the sprite class but when I implement touchesBegan, touchesMoved, etc., on that sprite's class it is not working because touchesMoved continues to report the finger even if it is not inside its area.
This is my touch logic inside the sprite's class:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
SKAction *scaleUp = [SKAction scaleTo:1.2 duration:0.2];
[self runAction:scaleUp];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self];
// CGPoint convertPT = [self convertPoint:touchLocation fromNode:self.parent];
NSLog(#"%#", NSStringFromCGPoint(touchLocation));
// if (CGRectContainsPoint(self.frame, touchLocation)) {
// NSLog(#"this is never fired?");
// }
}
The NSLog line will always print a location even if the finger is outside the layer...
I want TouchesMoved to stop firing after the finger is outside the sprite.
How do I do that?

I put this in a sub class and got good results.
You will still need some logic for when the begins outside the rect from within the game scene class.
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
SKAction *scaleUp = [SKAction scaleTo:1.2 duration:0.2];
[self runAction:scaleUp withKey:#"action"];
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
UITouch* touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self.parent];
if (![self containsPoint:loc]) {
SKAction *scaleDown = [SKAction scaleTo:1.0 duration:0.1];
[self runAction:scaleDown];
}
else if ([self containsPoint:loc]) {
SKAction *scaleUp = [SKAction scaleTo:1.2 duration:0.1];
[self runAction:scaleUp];
}
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self removeActionForKey:#"action"];
SKAction *scaleDown = [SKAction scaleTo:1.0 duration:0.1];
[self runAction:scaleDown];
}
I havent got the subclass method working to my liking, but I did get it working well from the main scene -- a mess of if statements ... but does the job
// add a ivar to scene enforce one zoom spite at a time, SKSpriteNode *_currentZoomRect;
- (void) addSomeBlocks
{
for (int i = 1; i <= 3; i++) {
SKSpriteNode *rect = [SKSpriteNode spriteNodeWithColor:[UIColor whiteColor] size:CGSizeMake(70, 70)];
rect.position = CGPointMake(100 * i , 160);
rect.name = #"inner";
[self addChild:rect];
}
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
UITouch* touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:loc];
for (SKNode *n in nodes) {
if ([n.name isEqualToString:#"inner"]) {
_currentZoomRect = (SKSpriteNode *) n;
SKAction *scaleUp = [SKAction scaleTo:2.0 duration:0.05];
[n runAction:scaleUp withKey:#"action"];
}
}
}
}
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
UITouch* touch = [touches anyObject];
CGPoint loc = [touch locationInNode:self];
NSArray *nodes = [self nodesAtPoint:loc];
for (SKNode *n in nodes) {
if ([n.name isEqualToString:#"inner"]) {
if (_currentZoomRect) {
// can only allow one zoom at a time
if (n == _currentZoomRect && !n.hasActions) {
SKAction *scaleUp = [SKAction scaleTo:2.0 duration:0.05];
[n runAction:scaleUp];
}
else if (n != _currentZoomRect) {
SKAction *scaleDown = [SKAction scaleTo:1.0 duration:0.05];
[_currentZoomRect runAction:scaleDown];
SKAction *scaleUp = [SKAction scaleTo:2.0 duration:0.05];
[n runAction:scaleUp];
_currentZoomRect = (SKSpriteNode *) n;
}
}
else {
SKAction *scaleUp = [SKAction scaleTo:2.0 duration:0.05];
[n runAction:scaleUp];
_currentZoomRect = (SKSpriteNode *) n;
}
}
}
if (![nodes count] && _currentZoomRect) {
SKAction *scaleDown = [SKAction scaleTo:1.0 duration:0.05];
[_currentZoomRect runAction:scaleDown];
_currentZoomRect = nil;
}
}
}
- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if (_currentZoomRect) {
SKAction *scaleDown = [SKAction scaleTo:1.0 duration:0.05];
[_currentZoomRect runAction:scaleDown];
_currentZoomRect = nil;
}
}

Related

How to detect in touchesEnded which nodes are still being pressed

I've been stuck on this problem for days. What I have is multiple SKSpriteNode's, one for a left arrow, right arrow and up arrow. When I hold down the right arrow I want my character to continue moving right while its being held down, on the other hand if you press the up Arrow then you will only jump once regardless of if you hold it down.
So my problem for example is when i hold the right arrow and then i press the up arrow, touchesEnded is called and it stops my character from moving right even though I still have my finger on the right arrow
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesBegan:touches withEvent:event];
for (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;
for (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 be the right way to approach the problem, but in touchesEnded I am trying to see if the touch is still in the desired Rect.
You need a way to identify the different nodes which are registering a touch. There is more than one way to do this but I have always found using the name property of a node to be the simplest and easiest to work with.
You already have the right idea by using the BOOLs to register the touch states.
I wrote some code to handle what you are trying to 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 {
for (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 {
for (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");
}

CCSprite Follow User Touch

So far, I have made my CCSprite move to the location of the user's touch on the screen using CCActionMoveTo, however I have only got it to work when the user simply taps.
I would like the CCSprite to move when the user drags their finger, instead of just tapping and moving along with the ability to change direction as the user drags changes direction - I am fairly new to cocos2d and have search for similar questions but have been unable to find any. I have posted my code below:
- (id)init
{
self = [super init];
if (!self) return(nil);
self.userInteractionEnabled = YES;
// Player sprite
_playerSprite = [CCSprite spriteWithImageNamed:#"PlayerSprite.png"];
_playerSprite.scale = 0.5;
_playerSprite.position = ccp(self.contentSize.width/2, 150);
[self addChild:_playerSprite];
return self;
}
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLoc = [touch locationInNode:self];
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:0.2f position:ccp(touchLoc.x, 150)];
[_playerSprite runAction:actionMove];
}
You will need to implement the touchMoved method and set your sprite position in there. Something like this:
- (void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInNode:self];
_playerSprite.position = touchLocation;
}
Try this (add a CGPoint property called previousTouchPos):
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLoc = [touch locationInNode:self];
self.previousTouchPos = touchLoc;
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:1.0f position:touchLoc];
[_playerSprite runAction:actionMove];
}
-(void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchLoc = [touch locationInNode:self];
CGPoint delta = ccpSub(touchLoc, self.previousTouchPos);
_playerSprite.position = ccpAdd(_playerSprite.position, delta);
self.previousTouchPos = touchLoc;
}
This sounds like an ideal use case for CCActionFollow:
https://www.makegameswith.us/gamernews/365/make-two-nodes-follow-each-other-in-cocos2d-30
If you use the variation where you provide the target position through a block you can use the latest touch position as target position.

iPhone - Wheel tracking finger jumps to same starting position?

I am trying to implement a selection wheel in an iphone application. I have got the wheel to track the users finger but now for some reason when the user touches the screen the wheel jumps so the same section of the wheel tracks the finger each time.
How would I get it so the wheel rotates like the user is dragging it from any point?
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
int tInput = [allTouches count]-1;
UITouch *touch =[[allTouches allObjects] objectAtIndex:tInput];
CGPoint location = [touch locationInView:self.view];
float theAngle = atan2( location.y-imageX.center.y, location.x-imageX.center.x );
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(theAngle);
imageX.transform = cgaRotate;
}
Any suggestion?
Right - that makes perfect sense - this is indeed what your code does.
What you need to add is the initial value where you started your drag as a relative rotation -- or track your last rotation.
A very elaborate way is shown below - but this should help get the point across.
#interface UntitledViewController : UIViewController {
CGPoint firstLoc;
UILabel * fred;
double angle;
}
#property (assign) CGPoint firstLoc;
#property (retain) UILabel * fred;
#implementation UntitledViewController
#synthesize fred,firstLoc;
- (void)viewDidLoad {
[super viewDidLoad];
self.fred = [[UILabel alloc] initWithFrame:CGRectMake(100,100,100,100)];
fred.text = #"Fred!"; fred.textAlignment = UITextAlignmentCenter;
[self.view addSubview:fred];
angle = 0; // we aint have rotated just yet...
};
// make sure we get them drag events.
- (BOOL)isFirstResponder { return YES; }
-(void)handleObject:(NSSet *)touches
withEvent:(UIEvent *)event
isLast:(BOOL)lst
{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
CGPoint curLoc = [touch locationInView:self.view];
float fromAngle = atan2( firstLoc.y-fred.center.y,
firstLoc.x-fred.center.x );
float toAngle = atan2( curLoc.y-fred.center.y,
curLoc.x-fred.center.x );
// So the angle to rotate to is relative to our current angle and the
// angle through which our finger moved (to-from)
float newAngle = angle + (toAngle - fromAngle);
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
fred.transform = cgaRotate;
// we only 'save' the current angle when we're done with the drag.
//
if (lst)
angle = newAngle;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch =[[[event allTouches] allObjects] lastObject];
// capture where we started - so we can later work out the
// rotation relative to this point.
//
firstLoc = [touch locationInView:self.view];
};
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleObject:touches withEvent:event isLast:NO];
};
-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self handleObject:touches withEvent:event isLast:YES];
}
Obviously you can do this a lot more elegant - and above misses a bit of 0 .. 2xPI capping you need.
Because the view itself is still the old one. You may want to fetch the zip example file in:
http://bynomial.com/blog/?p=77
and in PuttyView.m change the:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
....
else {
self.center = CGPointMake(self.center.x + touchPoint.x - touchStart.x,
self.center.y + touchPoint.y - touchStart.y);
into
else {
NSSet *allTouches = [event allTouches];
int tInput = [allTouches count]-1;
UITouch *touch =[[allTouches allObjects] objectAtIndex:tInput];
CGPoint location = [touch locationInView:self];
float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(theAngle);
self.transform = cgaRotate;
}
where you thus stay within the view and its relative angle. Alternatively - in your code - keep the relative angle itself.

Rotating image using objective C

I am trying to rotate an image. This I am succeeding in doing. The problem is that when I am clicking down and dragging slightly, the image rotates completely to reach the point where I have clicked and then rotates slowly as I drag the image clockwise. I am concerned to why it is rotating completely to the place that I am dragging. I want the dragging to start from the position it is found and NOT to rotate to the place my finger is down on and then starts from there.
This is my code:
-
(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
int len = [allTouches count]-1;
UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
CGPoint location = [touch locationInView:[self superview]];
float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );
totalRadians = theAngle;
[self rotateImage:theAngle];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
}
-(void) rotateImage:(float)angleRadians{
self.transform = CGAffineTransformMakeRotation(angleRadians);
CATransform3D rotatedTransform = self.layer.transform;
self.layer.transform = rotatedTransform;
}
Am I doing anything wrong?
Thanks!
Instead of creating the transformation from the angle that your getting from the touch, try incrementing totalRadians by its difference from the new angle, and then create the transform from that.
-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
NSSet *allTouches = [event allTouches];
int len = [allTouches count]-1;
UITouch *touch =[[allTouches allObjects] objectAtIndex:len];
CGPoint location = [touch locationInView:[self superview]];
float theAngle = atan2( location.y-self.center.y, location.x-self.center.x );
totalRadians += fabs(theAngle - totalRadians);
totalRadians = fmod(totalRadians, 2*M_PI);
[self rotateImage:totalRadians];
}
My first advice would be to switch to using the UIRotateGestureRecognizer if your app is for 4.0 and higher. It does the right thing and provides you with a rotation property.

Converting beginTouches and endTouches to ccBeginTouch and ccEndTouch

I'm converting the game I'm working on from UIkit to coocos2d. Using UIKIT I would use the code below code to pass the touch evens to a method. What would be the equivalent in Cocos2d?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Save the position
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil];
}
}
// Saves the first position for reference when the user lets go.
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
beginTouchPoint = touchPoint;
}
// Handles the continuation of a touch.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]];
}
}
-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{ id sender;
int directionSwiped;
int row,column;
CGFloat xDelta = position.x - beginTouchPoint.x;
CGFloat yDelta = position.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
What would be the equivalent using cocos2d?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Save the position
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil];
}
}
// Saves the first position for reference when the user lets go.
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
beginTouchPoint = touchPoint;
}
// Handles the continuation of a touch.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]];
}
}
-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{ id sender;
int directionSwiped;
int row,column;
CGFloat xDelta = position.x - beginTouchPoint.x;
CGFloat yDelta = position.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
I've tried to figure this out on my own, and I've spent hours on google, but I haven't come up with a workable solution.
I figured it out. I discovered that, what I was forgetting to do, was to add:
self.isTouchEnabled = YES;
to the Layer's init method.
After I did that the following code worked for me (beginTouchPoint and endTouchPoint are properties of the class):
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
beginTouchPoint = [[CCDirector sharedDirector]convertToGL:location];
}
// Handles the continuation of a touch.*
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
static BOOL isFirstTouch = YES;
UITouch* myTouch = [touches anyObject];
int row,column;
int directionSwiped;
CGPoint location = [myTouch locationInView: [myTouch view]];
endTouchPoint = [[CCDirector sharedDirector]convertToGL:location];
CGFloat xDelta = endTouchPoint.x - beginTouchPoint.x;
CGFloat yDelta = endTouchPoint.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}