使用视图控制器进行场景游戏

时间:2014-11-28 06:53:07

标签: ios objective-c sprite-kit

我需要在碰撞检测后为我的应用程序制作一个结束屏幕。使用按钮返回主菜单/游戏的最终屏幕的最简单方法是什么?我可以使用ViewController吗?我已经阅读了很多教程,视频以及此处的所有帖子:

这是我目前的代码(不仅仅是一些重要的事情):

@implementation MyScene

static const int squirrelHitCategory = 1;
static const int nutHitCategory = 2;

- (instancetype)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
    self.physicsWorld.contactDelegate = self;

    _squirrelSprite = [SKSpriteNode spriteNodeWithImageNamed:@"squirrel"];
    _squirrelSprite.position = _firstPosition;
    _atFirstPosition = YES;

    _squirrelSprite.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:10];
    _squirrelSprite.physicsBody.categoryBitMask = squirrelHitCategory;
    _squirrelSprite.physicsBody.contactTestBitMask = nutHitCategory;
    _squirrelSprite.physicsBody.collisionBitMask =  nutHitCategory;

    _squirrelSprite.physicsBody.affectedByGravity = NO;

    self.physicsWorld.contactDelegate = self;

    [self addChild:_squirrelSprite];

    SKAction *wait = [SKAction waitForDuration:3.0];

        SKSpriteNode *lightnut = [SKSpriteNode spriteNodeWithImageNamed:@"lightnut.png"];

        lightnut.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(200,160)];

        lightnut.physicsBody.categoryBitMask = nutHitCategory;
        lightnut.physicsBody.contactTestBitMask = squirrelHitCategory;
        lightnut.physicsBody.collisionBitMask =  squirrelHitCategory;

        lightnut.physicsBody.affectedByGravity = NO;

        self.physicsWorld.contactDelegate = self;

        [self addChild: lightnut];

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// Check time since the last touch event
if (touch.timestamp-_lastTouch >= .9) {
    // Allow touch
    NSLog(@"greater than or equal to 3 seconds");

    if (_atFirstPosition)
    {
        SKAction *moveNodeLeft = [SKAction moveByX:-207.8 y:0.0 duration:0.85];
        [_squirrelSprite runAction: moveNodeLeft withKey:@"moveleft"];
    } else {
        SKAction *moveNodeRight = [SKAction moveByX:207.8 y:0.0 duration:0.85];
        [_squirrelSprite runAction: moveNodeRight withKey:@"moveright"];
    }
    _atFirstPosition = !_atFirstPosition;
    _squirrelSprite.xScale *= -1.0;
}
else {
    // Ignore touch
    NSLog(@"Seconds since last touch %g",touch.timestamp-_lastTouch);
}
// Store timestamp
_lastTouch = touch.timestamp;

}

-(void)didBeginContact:(SKPhysicsContact *)contact

{
NSLog(@"contact detected");
SKPhysicsBody *firstBody, *secondBody;

firstBody = contact.bodyA;
secondBody = contact.bodyB;

if(firstBody.categoryBitMask == squirrelHitCategory || secondBody.categoryBitMask == nutHitCategory)
{

}
}

我希望我的终端屏幕很简单,所以最简单的方法就是最好。谢谢!

1 个答案:

答案 0 :(得分:1)

对于我的游戏,我添加了一个UIViewController属性并将其分配给SKScene。

然后,您可以将视图添加为子视图。实施例...

SKScene *scene = [SKScene sceneWithSize:self.view.frame.size];
scene.viewController = self;

然后在你的场景中......

[self.viewController addSubview:yourView];

以下是我自己的一些游戏实现,以帮助您入门。

在您的故事板或XIB中,您可以添加一个带按钮的视图作为视图的属性。在我的例子中,我有一个scoreLabel,一个menuButton和一个restartButton属性,可以将它们全部设置为x.hidden = YES;正如我所希望的那样,在场景中触发它们,比如......

self.vc.menuButton.hidden = YES;
self.vc.restartButton.hidden = YES;

在你的GameViewController(这是UIViewController的子类)......

//header
@interface GameViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;
@property (weak, nonatomic) IBOutlet UIView *hudView;
@property (weak, nonatomic) IBOutlet UIButton *menuButton;
@property (weak, nonatomic) IBOutlet UIButton *restartButton;

@property (nonatomic) MazeScene *maze;

@end

//class
- (void)viewDidLoad
{
    [super viewDidLoad];

    // Configure the view.
    SKView * skView = (SKView *)self.view;

    // Debug Options
//    skView.showsFPS = YES;
//    skView.showsNodeCount = YES;
//    skView.showsPhysics = YES;

    // Create and configure the maze scene.
    CGSize sceneSize = skView.bounds.size;
    skView.ignoresSiblingOrder = YES;

    MazeScene *maze = [[MazeScene alloc] initWithSize:sceneSize];
    maze.scaleMode = SKSceneScaleModeAspectFill;
    maze.vc = self;
    [skView presentScene:maze];
    _maze = maze;
}

在你的SKScene ......

//header
@interface MazeScene : SKScene <SKPhysicsContactDelegate>

@property (nonatomic, weak) GameViewController *vc;

@end

//class
-(id) initWithSize:(CGSize)size {
    if (self = [super initWithSize:size]) {
        //(GameViewController*) is a static cast of a subclass of UIViewController.
        [self.view addSubview:((GameViewController*)self.vc).hudView];
        // [self restartGame] is a specific method for my game, don't worry about it
        [self restartGame];
    }
    return self;
}