iOS 7 - SpriteKit Buttons / MenuItem

时间:2013-09-20 09:48:19

标签: ios button menu sprite-kit

我正在尝试构建一个简单的iOS游戏。当我的主菜单加载并出现在屏幕上时,我想让按钮从侧面移动。我唯一的想法是将一些SKActions应用于SKSpriteNodes。但所以我必须创建自己的按钮类。有没有更简单的方法来实现这一目标?是否已经有来自cocos2d的类CCMenuItem / CCMenu?或者UIButtons有办法吗?

由于

2 个答案:

答案 0 :(得分:2)

我创建了一个按钮类,它具有SKSpriteNode属性,CGRect属性和Radius属性(对于圆形按钮)。当您使用图像初始化按钮时,它会根据图像大小设置CGRect和Radius属性。然后你可以通过调用它的-collisionRect方法来检查玩家是否按下按钮,该方法返回一个CGRect并检查“触摸”位置是否在该矩形内。这样按钮可以移动并仍然接收输入。这是代码......

////////////////////// HEADER FILE ////////////////////// < / p>

//
//  ZSButton.h
//  PEGO
//
//  Created by Zion Siton on 07/09/2013.
//  Copyright (c) 2013 zillustrator. All rights reserved.
//

#import <SpriteKit/SpriteKit.h>

@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

///////////////////执行文件//////////////////////

//
//  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 < [self myRadius]) {

        // HIT!!!
        return YES;
    }

    // MISS!!!
    return NO;

}

@end

//////////////////初始化按钮......

// Initialize BACK BUTTON
        backButton = [[ZSButton alloc] initWithTexture:
                      [[ZSGraphicLoader sharedGraphicLoader] frameByName:kBACK_BUTTON]];
        [self addChild:backButton];

//////////////////测试碰撞/用户按压

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
        }

    }

}

答案 1 :(得分:0)

创建自己的继承自SKSpriteNode的按钮类并添加操作只需几分钟就可以正常工作。 UIButton和SKScene在触摸方面似乎并不能很好地发挥作用,并且确实没有必要考虑所描述的方法非常好。您可以使用继承的SKSpriteNode成员将自己的纹理和颜色添加到按钮。