来自SKScene类的呼叫方法

时间:2014-09-05 05:55:15

标签: ios objective-c iphone xcode sprite-kit

我正在尝试从我的ViewController.m类调用我的Scene类中的newExplosion方法(在touchesBegan方法中)。问题是它没有将SKEmitterNode添加到View(这是newExplosion方法应该做的)。当touchesBegan方法在Scene类中时,它可以工作,但是当从我的ViewController类调用它时,它不起作用。

这是我的Scene类:

#import "Scene.h"
#import <SpriteKit/SpriteKit.h>

@implementation Scene : SKScene

- (void) newExplosion: (float)x : (float) y {
    SKEmitterNode *emitter =  [NSKeyedUnarchiver unarchiveObjectWithFile:[[NSBundle mainBundle] pathForResource:@"crouchEffect" ofType:@"sks"]];
    emitter.position = CGPointMake(x,y);
    [self addChild:emitter];
}
// This works VVVV
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self newExplosion:100 :100];
}

这是我的ViewController.m类中与SpriteKit相关的东西:

- (void)viewDidLoad{
    SKView * skView = _skView;
    SKScene * scene = [Scene sceneWithSize:skView.bounds.size];
    scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:scene];
}
// Doesn't work VVVV
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    Scene * ss = [[Scene alloc] init];
    [ss newExplosion:100:100];
} 

我在newExplosion方法中添加了一条调试消息,看它是否实际运行它并将其打印出来。所以它被调用,但没有将SKEmitterNode添加到视图中。

1 个答案:

答案 0 :(得分:0)

每次触摸屏幕时都会创建一个新场景,但是你从不将它添加到视图中,我想你想做这样的事情

//In view controller interface add
{
  Scene * _scene;
}

//In implementation, change the following
- (void)viewDidLoad{
    SKView * skView = _skView;
    _scene = [Scene sceneWithSize:skView.bounds.size];
    _scene.scaleMode = SKSceneScaleModeAspectFill;
    [skView presentScene:_scene];
}
// Doesn't work VVVV
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [_scene newExplosion:100:100];
}