After rotating SKLabelNode it becomes difficult to get location in touchesBegan: - ios

I have an SKLabel Node setup as a button in my app and it works fine. However, when I rotate the SKLabelNode with this code my touchesBegan method no longer executes correctly and it becomes very difficult to have the SKLabelNode touches register correctly:
[_menuButton runAction:[SKAction sequence:#[[SKAction rotateByAngle:M_PI duration:.2],
[SKAction moveByX:0 y:-15 duration:.2]]]];
Here is my touches began method:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
CGPoint positionInScene = [touch locationInNode:self];
[self selectNodeForTouch:positionInScene];
if ([node.name isEqualToString:menuButtonName]) {
//Touched Menu Button
[self animateMenuAndShow:YES];
}
}
EDIT: One additional piece of information is that I'm using SKTEffects which requires that instead of using self.scene as my base I'm using _worldLayer to add all elements in the scene to. Is it possible that this is throwing off my touch calculations? And if so how can I correct this?
self.scaleMode = SKSceneScaleModeResizeFill;
self.anchorPoint = CGPointMake(0.5, 0.5);
// The origin of the pivot node must be the center of the screen.
_worldPivot = [SKNode node];
[self addChild:_worldPivot];
// Create the world layer. This is the only node that is added directly
// to the pivot node. If you have a HUD layer you would add that directly
// to the scene and make it sit above the world layer.
_worldLayer = [SKNode node];
_worldLayer.position = self.frame.origin;
[_worldPivot addChild:_worldLayer];
Also after following sangony's advice here is my updated touchesBegan: method which works well at first, but runs into the same problem after rotating the label by M_PI
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:_worldLayer];
CGPoint locationInMenu = [touch locationInNode:_menuBackground];
if (CGRectContainsPoint(_menuButton.frame, location)) {
//Touched Menu Button
if (!self.menuVisible) {
[self animateMenuAndShow:YES];
}
else
{
[self animateMenuAndShow:NO];
}
}
}
EDIT2:
Here is my animateMenuAndShow: method
-(void)animateMenuAndShow:(BOOL)show
{
if (show == YES) {
// self.paused = YES;
self.menuVisible = !self.menuVisible;
[self createMenu];
SKAction *showMenuAction = [SKAction sequence:#[
[SKAction moveTo:CGPointMake(self.size.width / 2, self.size.height/2) duration:0.3],
[SKAction runBlock:^{
[self jelly:self.menuBackground];
}],
[SKAction waitForDuration:1.5]
]];
[self.menuBackground runAction: showMenuAction completion:^{
self.paused = YES;
}];
if (_flipped == YES) {
[self.menuBackground runAction:[SKAction sequence:#[
[SKAction moveByX:0 y:0 duration:.2],[SKAction rotateByAngle:M_PI duration:.2]]]];// [SKAction rotateByAngle:M_PI duration:.2]
}
}
else
{
self.paused = NO;
SKAction *removeMenuAction = [SKAction sequence:#[
[SKAction moveTo:CGPointMake(self.size.width / 2, -self.size.height + 300) duration:0.3],
]];
[self.menuBackground runAction:removeMenuAction completion:^{
[self.menuBackground removeFromParent];
}];
self.menuVisible = !self.menuVisible;
}
}

Try this touchesBegan instead:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if (CGRectContainsPoint(myLabel.frame, touchLocation))
{
[self animateMenuAndShow:YES];
}
}

Related

iOS SpriteKit - Touch at a specific location, want node at specific zPosition

I have a project where when I tap I check to see if the tap is on a specific node. If it is then I create a new SpriteNode at that position that is only visible for .1s before being deleted. I want to be able to spam tapping on the screen but with that current check the node that I end up tapping on is the new SpriteNode that I've created.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if (node == shutdahellup){
[self runAction:[SKAction playSoundFileNamed:#"shut-da-hell-uh.mp3" waitForCompletion:NO]];
}else if (node == hotdamn){
[self runAction:[SKAction playSoundFileNamed:#"hot-damn.mp3" waitForCompletion:NO]];
}else if (node == wafflewednesday){
[self runAction:[SKAction playSoundFileNamed:#"waffle-wednesday.mp3" waitForCompletion:NO]];
}else if (node == damnwaffle){
[self runAction:[SKAction playSoundFileNamed:#"get-a-damn-waffle.mp3" waitForCompletion:NO]];
}
[self createFace:touch];
}
}
-(void) createFace:(UITouch *)touch {
SKSpriteNode * face = [SKSpriteNode spriteNodeWithImageNamed:#"shane.png"];
CGPoint location = [touch locationInNode:self];
face.position = location;
face.zPosition = 100;
[self addChild:face];
SKAction * wait = [SKAction waitForDuration:0.1];
SKAction * remove = [SKAction removeFromParent];
SKAction * sequence = [SKAction sequence:#[wait, remove]];
[face runAction:sequence];
}
Any ideas? Thanks!

How can I make a functioning animated button?

I have a button set up in a scene like this:
self.playButton = [SKSpriteNode spriteWithImageNamed:#"playbutton.png"];
self.playButton.size = CGSizeMake(100,100);
self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), 100);
[self addChild:self.playButton];
Then in touchesBegan:
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if([self.playButton containsPoint:location]){
self.playButton.texture = [SKTexture textureWithImageNamed:#"playbuttonpressed.png"];
[self.playButton runAction: [self touchButtonAction]];
}
Then in touchesEnded:
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if([self.playButton containsPoint:location]){
self.playButton.texture = [SKTexture textureWithImageNamed:#"playbutton.png"];
[self.view presentScene:self.nextScene transition:self.sceneTransition];
}
Then in touchButtonAction:
SKAction *toSmall = [SKAction scaleBy:0.8 duration:0.1];
return toSmall;
Here's the problem: When I tap the button, touchesEnded gets called before the action is finished, which causes for an incomplete animation which doesn't look good (it seems laggy). How can I make a button that finishes the animation before transitioning?
There are a couple of ways of accomplishing what you want. One of those is using blocks.
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if([self.playButton containsPoint:location]){
SKAction *block0 = [SKAction runBlock:^{
self.playButton.texture = [SKTexture textureWithImageNamed:#"playbuttonpressed.png"];
[self.playButton runAction: [self touchButtonAction]];
}];
SKAction *wait0 = [SKAction waitForDuration:1.0]; // <- however long it takes for the animation to complete
SKAction *block1 = [SKAction runBlock:^{
self.playButton.texture = [SKTexture textureWithImageNamed:#"playbutton.png"];
[self.view presentScene:self.nextScene transaction:self.sceneTransaction];
}];
[self runAction:[SKAction sequence:#[block0, wait0, block1]]];
}
For more information on using blocks, you can read the Apple Blocks Programming Guide.

Change SpriteNodes coordinates with User touch

I'm currently programming a small game with SpriteKit in Xcode for the iOS platform. I wrote a code which adds some SpriteNodes on 2 different "lines", so half of the nodes are moving with the y-coordinate 100 and the others with y = 200, and they're all moving from the left side to the right which an infinity loop. Now I want that the user can touch on one SpriteNode, then moves his finger to another SpriteNode, but it has to be located on another line, then removes his finger and the SpriteNode with the TOUCHBEGAN should change its y-coordinate with the TOUCHEND Node. How can I accomplish this?
-(void)add
{
SKSpriteNode *sprite2 = [SKSpriteNode spriteNodeWithImageNamed:#"test1.png"];
sprite2.position = CGPointMake(-40, self.frame.size.height / 2);
sprite2.size = CGSizeMake(100, 32);
SKSpriteNode *sprite3 = [SKSpriteNode spriteNodeWithImageNamed:#"test.png"];
sprite3.position = CGPointMake(-40, (self.frame.size.height / 2) + 90);
sprite3.size = CGSizeMake(32, 100);
[self addChild:sprite1];
[self addChild:sprite2];
SKAction *actionMove1 = [SKAction moveTo:CGPointMake(400, (self.frame.size.height / 2) - 90) duration:12];
SKAction *actionMove2 = [SKAction moveTo:CGPointMake(200, (self.frame.size.height / 2)) duration:12];
SKAction *actionMoveDone = [SKAction removeFromParent];
[sprite1 runAction:[SKAction sequence:#[actionMove1, actionMoveDone]]];
[sprite2 runAction:[SKAction sequence:#[actionMove2, actionMoveDone]]];
}
- (void)updateWithTimeSinceLastUpdate:(CFTimeInterval)timeSinceLast
{
self.lastSpawnTime += timeSinceLast;
if (self.lastSpawnTime > 2)
{
self.lastSpawnTime = 0;
[self add];
}
}
- (void)update:(CFTimeInterval)currentTime
{
CFTimeInterval timeSinceLast = currentTime - self.lastUpdateTime;
self.lastUpdateTime = currentTime;
if (timeSinceLast > 2)
{
timeSinceLast = 1.0 /60.0;
self.lastUpdateTime = currentTime;
}
[self updateWithTimeSinceLastUpdate:timeSinceLast];
}
You have to subclass SkSpriteNode:
to detect TouchEvents you have to set "userInteractionEnabled" to "YES"
to react on TouchEvents implement the touch method which fits for you needs:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}
In this method you can change the coordinates of your sprite
add these function like that in your sksence
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent )event {
/ Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if([node.name isEqualToString:#"plane"])
{
NSLog(#"_______touch ended");
//x and y position of object at Scene
NSLog(#"%f",node.position.x);
NSLog(#"%f",node.position.y);
NSLog(#"%#",node);
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if([node.name isEqualToString:#"plane"])
{
NSLog(#"_______touch mobing");
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if([node.name isEqualToString:#"plane"])
{
NSLog(#"______touch begin");
//x and y position of object at Scene
NSLog(#"%f",node.position.x);
NSLog(#"%f",node.position.y);
NSLog(#"%#",node);
}
}

Button pressed animation in Sprite-Kit

In a Sprite-Kit game I want to animate Buttons when they were pressed. Right now the code reacts directly and to not wait till the animation is executed. Furthermore I want the button to animate back when the user wipes his finger out of the button.
Here my code:
-(void)addStartButton:(CGSize)size {
self.start = [SKSpriteNode spriteNodeWithImageNamed:#"startButton1"];
self.start.position = CGPointMake(size.width/2, size.height/2);
self.start.name = #"start";
[self addChild:self.start];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
self.start.texture = [SKTexture textureWithImageNamed:#"startButton2"];
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}
}
This will give you 2 second delay before switching to another scene:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
SKAction *changeTexture = [SKAction runBlock:^{
self.start.texture = [SKTexture textureWithImageNamed:#"startButton2"];
}];
SKAction *wait = [SKAction waitForDuration:2.f];
SKAction *presentAnotherScene = [SKAction runBlock:^{
MyScene *myScene = [MyScene sceneWithSize:self.size];
[self.view presentScene:myScene transition:[SKTransition pushWithDirection:SKTransitionDirectionLeft duration:0.5]];
}];
[self runAction:[SKAction sequence:#[changeTexture,wait,presentAnotherScene]]];
}
}
Furthermore I want the button to animate back when the user wipes his finger out of the button.
This seems pointless, since you are transitioning to another scene when user presses the button.
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
startButton.texture = [SKTexture textureWithImageNamed:#"startButton2"];
}else{startButton.texture = [SKTexture textureWithImageNamed:#"startButton1"];}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
startButton.texture = [SKTexture textureWithImageNamed:#"startButton2"];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
if ([node.name isEqualToString:#"start"]) {
//Start Button Actions
}

touchesMoved on a specific moving object

Im using Spritekit to create a game for iOS 7. The game has circles moving across the screen using this code
_enemy = [SKSpriteNode spriteNodeWithImageNamed:#"greeny"];
_enemy.position = CGPointMake(CGRectGetMidX(self.frame)+300,
CGRectGetMidY(self.frame));
_enemy.size = CGSizeMake(70, 70);
_enemy.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:35];
_enemy.physicsBody.mass = 0;
_enemy.physicsBody.categoryBitMask = Collisiongreen;
[_enemies addObject:_enemy];
NSLog(#"Enemies%lu",(unsigned long)_enemies.count);
[self addChild:_enemy];
[_enemy runAction:[SKAction moveByX:-900 y:0 duration:4]];
[self runAction:[SKAction playSoundFileNamed:#"Spawn.wav" waitForCompletion:NO]];
[self runAction:[SKAction sequence:#[
[SKAction waitForDuration:1.4],
[SKAction performSelector:#selector(move)
onTarget:self],
]]];
I would like to have it that when the user touches one of the objects moving across the screen that they can move it with their finger either up or down (thats why I'm using touches moved)
The code i have set up right now is
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(_dead)
return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if(CGRectContainsPoint(_enemy.frame, location)){
[_enemy runAction:[SKAction moveTo:[[touches anyObject] locationInNode:self] duration:0.01]];
}
My problem is that if i touch it the object will move a few pixels but will stop right after. How can I get it to move with my finger and if i let go it will continue moving left like programed before? Thanks!!
So the trick here is that in the touchesMoved method, you want the enemy's postion set to the location of your touch. Use the touchesEnded method to execute a method that makes the enemy continue in the direction of your last touch. This is a rough non-tested example.
You need to store the difference of the current location with the previous location.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
if(_dead)
return;
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
if(CGRectContainsPoint(_enemy.frame, location)){
someBool = False;
[_enemy setPosition:location];
changeInX = location.x - lastX;
changeInY = location.y - lastY;
lastX = location.x;
lastY = location.y;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
someBool = True;
}
- (void)update:(NSTimeInterval)currentTime
{
...
if (someBool) {
enemy.position = CGPointMake((enemy.position.x + changeInX), (enemy.position.y + changeInY));
}
...
}

Resources