Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I created a button class that has a SKSpriteNode property, a CGRect property and a Radius property (for round buttons). When you initialise the button with an image it set's it's CGRect and Radius property based on the image size. You can then just check if a player's hitting the button by calling to it's -collisionRect method which return's a CGRect and check if the "touch" location is within that rect. This way the button's can move around and still receive input. Here's the code...</p> <p>////////////////////// HEADER FILE //////////////////////</p> <pre><code>// // ZSButton.h // PEGO // // Created by Zion Siton on 07/09/2013. // Copyright (c) 2013 zillustrator. All rights reserved. // #import &lt;SpriteKit/SpriteKit.h&gt; @interface ZSButton : SKNode { SKSpriteNode *mySprite; CGRect myRect; CGFloat myRadius; } @property (nonatomic, strong)SKSpriteNode *mySprite; @property (nonatomic, assign)CGRect myRect; @property (nonatomic, assign)CGFloat myRadius; - (id)initWithTexture:(SKTexture *)texture; - (BOOL)didRadialCollideWith:(CGPoint)aPoint; @end </code></pre> <p>/////////////////// IMPLEMENTATION FILE //////////////////////</p> <pre><code>// // ZSButtonA.m // PEGO // // Created by Zion Siton on 07/09/2013. // Copyright (c) 2013 zillustrator. All rights reserved. // #import "ZSButton.h" @implementation ZSButton @synthesize mySprite, myRect, myRadius; - (id)init { // Use a default button image return [self initWithTexture:[SKTexture textureWithImageNamed:@"ButtonDefault.png"]]; } - (id)initWithTexture:(SKTexture *)texture { self = [super init]; if (self) { // Assumes there is a texture mySprite = [SKSpriteNode spriteNodeWithTexture:texture]; [self addChild:mySprite]; // Assumes the collision area is the size of the texture frame myRect = [mySprite frame]; // Assumes the texture is square myRadius = myRect.size.width * 0.5; } return self; } - (CGRect)myRect { CGPoint pos = [self position]; CGFloat originX = pos.x - (mySprite.frame.width * 0.5); CGFloat originY = pos.y - (mySprite.frame.height * 0.5); CGFloat width = mySprite.frame.width; CGFloat height = mySprite.frame.height; myRect = CGRectMake(originX, originY, width, height); return myRect; } - (BOOL)didRadialCollideWith:(CGPoint)aPoint { // Get the distance between the button centre and the touch // ZSMath is a class I wrote that mainly performs trigonometric functions CGFloat distance = [ZSMath distanceFromA:[self position] toB:aPoint]; // Perform a radial collision check if (distance &lt; [self myRadius]) { // HIT!!! return YES; } // MISS!!! return NO; } @end </code></pre> <p>////////////////// TO INITIALISE A BUTTON...</p> <pre><code>// Initialize BACK BUTTON backButton = [[ZSButton alloc] initWithTexture: [[ZSGraphicLoader sharedGraphicLoader] frameByName:kBACK_BUTTON]]; [self addChild:backButton]; </code></pre> <p>////////////////// TO TEST FOR A COLLISION/USER PRESS</p> <pre><code>for (UITouch *touch in touches) { // Get the user's touch location CGPoint location = [touch locationInNode:self]; // If the user taps on BACK Button Rectangle if (CGRectContainsPoint(backButton.myRect, location)) { // DO A RADIAL CHECK if ([backButton didRadialCollideWith:location]) { // DO BACK BUTTON STUFF } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload