【问题标题】:Using UIButton for a game over screen使用 UIButton 进行屏幕游戏
【发布时间】:2014-12-24 02:35:09
【问题描述】:

近一个月来,我一直在尝试在屏幕上创建游戏,并认为我不妨发布我当前的代码,看看是否有人可以提供帮助。我已经阅读了许多教程并观看了一些不错的视频,但我的代码似乎总是如此不同,以至于即使在搞砸了几个小时后我也会遇到错误。我试图创建的是一个按钮,在我的两个精灵发生碰撞后,它可以让您回到起始页面(使用故事板制作)。我可以制作将您带回起始屏幕的按钮,但我希​​望获得有关在碰撞检测之前不会显示的代码以及在何处声明按钮的所有内容的代码。出于某种原因,我总是会收到一些错误,即无法使用并且没有正确使用它们。任何帮助是极大的赞赏。我当前的代码:

Viewcontroller.h

#import <UIKit/UIKit.h>
#import <SpriteKit/SpriteKit.h>

@interface ViewController : UIViewController

@end

Viewcontroller.m

#import "ViewController.h"
#import "MyScene.h"

@implementation ViewController

- (void)viewDidLoad
{
[super viewDidLoad];

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

// Create and configure the scene.
MyScene * scene = [MyScene sceneWithSize:skView.bounds.size];
scene.scaleMode = SKSceneScaleModeAspectFill;

scene.viewController = self;

// Present the scene.
[skView presentScene:scene];
}

- (BOOL)shouldAutorotate
{
return YES;
}

- (NSUInteger)supportedInterfaceOrientations
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return UIInterfaceOrientationMaskAllButUpsideDown;
} else {
    return UIInterfaceOrientationMaskAll;
}
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}

@end

MyScene.h

#import <SpriteKit/SpriteKit.h>

@interface MyScene : SKScene <SKPhysicsContactDelegate>

@property (strong, nonatomic) UIViewController *viewController;

@end

MyScene.m

#import "MyScene.h"

@interface MyScene()

@property (nonatomic) NSTimeInterval lastTimeSceneRefreshed;

@property (nonatomic) SKSpriteNode* squirrelSprite;
@property (nonatomic) SKSpriteNode* lightNut;
@property (nonatomic) SKSpriteNode* appleSprite;

@property NSTimeInterval lastTouch;

@property BOOL atFirstPosition;
@property CGPoint firstPosition;



@end

@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;
    [self buildBackground];
    [self startScrolling];

    _firstPosition = CGPointMake(self.frame.size.width * 0.817f, self.frame.size.height * .40f);
    _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];

    // Declare SKAction that waits 2 seconds
    SKAction *wait = [SKAction waitForDuration:3.0];

    // Declare SKAction block to generate the sprites
    SKAction *createSpriteBlock = [SKAction runBlock:^{
        SKSpriteNode *lightnut = [SKSpriteNode spriteNodeWithImageNamed:@"lightnut.png"];
        BOOL heads = arc4random_uniform(100) < 50;
        lightnut.position = (heads)? CGPointMake(257,600) : CGPointMake(50,600);

        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];

        SKAction *moveNodeUp = [SKAction moveByX:0.0 y:-700.0 duration:1.3];
        [lightnut runAction: moveNodeUp];
    }];

    // Combine the actions
    SKAction *waitThenRunBlock = [SKAction sequence:@[wait,createSpriteBlock]];

    // Lather, rinse, repeat
    [self runAction:[SKAction repeatActionForever:waitThenRunBlock]];

}
return self;
}

- (void)update:(CFTimeInterval)currentTime {
// Updating background nodes
// We don't want to update backgrounds each frame (60 times per second)
// Once per second is enough. This will reduce CPU usage
if (currentTime - self.lastTimeSceneRefreshed > 1) {
    [self backgroundNodesRepositioning];
    self.lastTimeSceneRefreshed = currentTime;
}
}

- (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)
{

}
}

-(void)goToHomeScreen {
[self.viewController.navigationController popToRootViewControllerAnimated:YES];
}

@end

如果有其他需要补充的信息或需要澄清的地方,请告诉我,谢谢!

【问题讨论】:

  • 是否正确记录了“检测到联系人”?那是你想切换到主屏幕的时候吗?
  • 它正在被正确记录,我想要一个小框,上面写着游戏结束并带有一个按钮,以便在记录后返回开始屏幕。谢谢!
  • 在这种情况下,只需在didBeginContact: 中显示一个UIAlertController,并带有一个触发goToHomeScreen 方法的按钮。

标签: ios objective-c uibutton sprite-kit


【解决方案1】:

我希望得到关于在碰撞检测之前不显示的代码以及在哪里为按钮声明所有内容的代码

使用 NSNotificationCenter 将 UIButton 隐藏属性设置为 YES 或 NO。

猜你喜欢
  • 2013-11-21
  • 2010-10-13
  • 1970-01-01
  • 2013-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多