所以在MyScene.m
我创建了我的Balloon对象并将其放入场景中。
for (int i = 0; i < 4; i++) {
Balloon *balloonObject = [[Balloon alloc] init];
balloonObject.position = CGPointMake(50 + (75 * i), self.size.height*.5);
[_balloonsArray addObject:balloonObject];
}
while (_balloonsArray.count > 0) {
[self addChild:[_balloonsArray objectAtIndex:0]];
[_balloonsArray removeObjectAtIndex:0];
}
这让我在屏幕上显示了4个气球。在Balloon.h
文件中,我有一个名为-(void)shrink
的方法,我想在-(void)touchesBegan
方法中的tapped balloonObject上调用它。我在下面尝试过这段代码,但它给了我一个NSInvalidArgumentException', reason: '-[SKSpriteNode shrink]: unrecognized selector sent to instance 0x17010c210'
错误。
for (UITouch *touch in touches) {
CGPoint location = [touch locationInNode:self];
Balloon *node = (Balloon *)[self nodeAtPoint:location];
if ([node.name isEqualToString:@"balloon"]) {
[node shrink];
}
}
Balloon.h
@interface Balloon : SKSpriteNode
-(void)shrink;
@end
Balloon.m
@implementation Balloon
{
SKSpriteNode *_balloon;
}
-(id)init{
self = [super init];
if (self){
_balloon = [SKSpriteNode spriteNodeWithImageNamed:@"balloonPicture"];
_balloon.name = @"balloon";
_balloon.position = CGPointMake(0, 0);
[self addChild:_balloon];
}
return self;
}
-(void)shrink{
// does something
}
答案 0 :(得分:3)
问题出在你的气球初始化中。您正在创建子SKSpriteNode并将名称设置为气球,而不是使用Balloon类。这就是你获得SKSpriteNode而不是气球的原因。
你可以这样做。
-(id)init{
self = [super init];
if (self){
self.texture = [SKTexture textureWithImageNamed:@"balloonPicture"];
self.size = self.texture.size;
self.name = @"balloon";
self.position = CGPointMake(0, 0);
}
return self;
}
在您创建气球的场景中
Ballon *balloon = [[Balloon alloc]init];
[self addChild:balloon];