当我触摸精灵时,如何执行动作我当前使用节点方法并检查精灵的名称,但是每次触摸精灵时都有任何方法可以使其更准确和响应。
简而言之:如何在用户触摸精灵时执行操作?
答案 0 :(得分:0)
我不确定sprite名称的含义,但我认为下面的代码示例是检查触摸了哪个SKSpriteNode的最佳方法。
#import "MyScene.h"
@implementation MyScene
{
SKSpriteNode *frank;
SKSpriteNode *sally;
}
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
frank = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(50, 50)];
frank.position = CGPointMake(100, 100);
[self addChild:frank];
sally = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
sally.position = CGPointMake(300, 100);
[self addChild:sally];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInNode:self.scene];
if (CGRectContainsPoint(frank.frame, touchLocation))
NSLog(@"Frank");
if (CGRectContainsPoint(sally.frame, touchLocation))
NSLog(@"Sally");
}
-(void)update:(CFTimeInterval)currentTime
{
//
}
@end
更新了其他问题
您可以像这样检查节点的位置:
if ((CGRectContainsPoint(sally.frame, touchLocation)) & (sally.position.y > 50))
NSLog(@"Sally's y position: %f",sally.position.y);