2014-11-28 1 views
2

충돌 감지 후 내 앱의 종료 화면을 만들어야합니다. 주 메뉴/게임으로 돌아 가기 버튼이있는 종료 화면을 만드는 가장 쉬운 방법은 무엇입니까? 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

내 게임의 경우 UIViewController 속성을 추가하고 SKScene에 할당합니다.

그런 다음보기를 하위보기로 추가 할 수 있습니다. 예 ... 장면에서 다음

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

...

[self.viewController addSubview:yourView]; 
여기

내 자신의 게임의 구현의 일부는 당신이 시작하는 데 도움이하는 것입니다.

스토리 보드 또는 XIB에서보기의 단추로보기가 추가 된보기를 추가하십시오. 제 경우에는 scoreLabel, menuButton 및 restartButton을 속성으로 가지며 모두 x.hidden = YES로 설정할 수 있습니다. 나는 그런 (의 UIViewController의 서브 클래스이다) 당신의 GameViewController에서

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

... 당신의 SKScene에서

//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; 
} 

... 같은 장면을 트리거에 의해 원하는대로

//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; 
} 
+0

방법에 대한 유용한 자습서가 있습니까? 나는 아직도 프로그래밍에 익숙하지 않다. 고맙습니다! – Ryanc

+0

@Ryanc 시작하기에 도움이 될만한 코드를 사용하여 답변을 업데이트했습니다. 나는 내 스토리 보드 사진을 게시하지 않았지만 더 많은 도움이 필요하면 알려 주시기 바랍니다. – Scott

관련 문제