• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ios - 使用 View Controller 进行游戏结束场景

[复制链接]
菜鸟教程小白 发表于 2022-12-13 03:42:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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

这是我当前的代码(并非全部只是一些重要的事情):

@implementation MyScene

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

- (instancetype)initWithSizeCGSize)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)touchesBeganNSSet *)touches withEventUIEvent *)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)didBeginContactSKPhysicsContact *)contact

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

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

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

}
}

我希望我的片尾画面简单,所以制作它的最简单方法就是最好的。谢谢!



Best Answer-推荐答案


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

然后您可以将 View 添加为 subview 。示例...

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

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

[self.viewController addSubview:yourView];

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

在您的 Storyboard 或 XIB 中,无论您喜欢哪种方式,您都可以添加一个带有按钮的 View 作为 View 的属性。在我的例子中,我有一个 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) initWithSizeCGSize)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;
}

关于ios - 使用 View Controller 进行游戏结束场景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27183417/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap