Reputation: 3
My game runs the menu scene but when i press the play button and it transitions to the main scene all i can see is a gray screen.
heres my code: Myscene.h
#import <SpriteKit/SpriteKit.h>
@interface MyScene : SKScene
@property (strong, nonatomic) SKLabelNode *scoreLabel;
@property (nonatomic) NSInteger score;
@property (nonatomic) BOOL gameOver;
@property SKEmitterNode *shatter;
typedef NS_ENUM(NSInteger, SpriteType) {
SpriteTypeBackground,
SpriteTypeSquare
};
Myscene.m
#import "MyScene.h"
@interface MyScene()
@property SKSpriteNode *background;
@property SKSpriteNode *square;
@end
@implementation MyScene
#define kNumberOfColors 2 //Setting total number of possible colors
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
[self runAction:[SKAction repeatActionForever: [SKAction sequence:@[[SKAction performSelector:@selector(addSquareAndBackground) onTarget:self] ]]]];
//Score Label
float margin = 10;
self.scoreLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
self.scoreLabel.text = @"Score: 0";
self.scoreLabel.fontSize = [self convertFontSize:14];
self.scoreLabel.zPosition = 4;
self.scoreLabel.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
self.scoreLabel.position = CGPointMake(margin, margin);
[self addChild:self.scoreLabel];
}
return self;
}
- (float)convertFontSize:(float)fontSize
{
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
return fontSize * 2;
} else {
return fontSize;
}
}
-(void)addSquareAndBackground {
_background = [self createSpriteWithType:SpriteTypeBackground];
_square = [self createSpriteWithType:SpriteTypeSquare];
}
-(void)removeSquareAndBackground {
[_background removeFromParent];
[_square removeFromParent];
}
-(SKSpriteNode *) createSpriteWithType:(SpriteType)type {
// Select a color randomly
NSString *colorName = [self randomColorName];
SKSpriteNode *sprite;
if (type == SpriteTypeBackground) {
NSString *name = [NSString stringWithFormat:@"%@Outline",colorName];
sprite = [SKSpriteNode spriteNodeWithImageNamed:name];
sprite.name = name;
sprite.size = CGSizeMake(CGRectGetHeight(self.frame), CGRectGetWidth(self.frame));
}
else {
sprite = [SKSpriteNode spriteNodeWithImageNamed:colorName];
sprite.name = [NSString stringWithFormat:@"%@Sprite",colorName];
sprite.size = CGSizeMake(50, 50);
}
sprite.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
[self addChild:sprite];
return sprite;
}
- (NSString *) randomColorName {
NSString *colorName;
switch (arc4random_uniform(kNumberOfColors)) {
case 0:
colorName = @"blue";
break;
case 1:
colorName = @"pink";
break;
// Add more colors here
default:
break;
}
return colorName;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInNode:self];
SKNode *node = [self nodeAtPoint:location];
NSString *pinkShatterPath = [[NSBundle mainBundle] pathForResource:@"pinkShatter" ofType:@"sks"];
SKEmitterNode *pinkShatter = [NSKeyedUnarchiver unarchiveObjectWithFile:pinkShatterPath];
pinkShatter.particlePosition = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
if (node == _square) {
// Extract the color name from the node name
NSArray *squareNameParts = [node.name componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
// Extract the color name from the node name
NSArray *backgroundNameParts = [_background.name componentsSeparatedByCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]];
// Compare if the colors match
if ([backgroundNameParts[0] isEqualToString: squareNameParts[0]]) {
//Add SKAction to add 1 to score label
[SKAction performSelector:@selector(removeSquareAndBackground) onTarget:self];
[self addChild:pinkShatter];
self.score += 10;
//NSLog(@"Score"); for console error detection
} else {
//NSLog(@"Lost"); //Add SKAction to display game-over menu
SKLabelNode *gameOverLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];
gameOverLabel.text = @"You Lose!!!!!";
gameOverLabel.fontSize = 20;
gameOverLabel.zPosition = 4;
gameOverLabel.position = CGPointMake(CGRectGetMidX(self.frame),
CGRectGetMidY(self.frame));
[gameOverLabel setScale:0.1];
[self addChild:gameOverLabel];
[gameOverLabel runAction:[SKAction scaleTo:1.0 duration:0.5]];
self.gameOver = YES;
return;
}
}
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
[self.scoreLabel setText:[NSString stringWithFormat:@"Score: %ld", (long)self.score]];
}
@end
Before i added the menu scene it worked fine now after the addition i cant seem to get it to work.
Upvotes: 0
Views: 85
Reputation: 64477
Are you aware of what you are doing here?
[self runAction:
[SKAction repeatActionForever:
[SKAction sequence:@[
[SKAction performSelector:@selector(addSquareAndBackground) onTarget:self]
]]]];
First of all the sequence is completely superfluous because you aren't actually sequencing multiple actions, you only have one:
[self runAction:
[SKAction repeatActionForever:
[SKAction performSelector:@selector(addSquareAndBackground) onTarget:self]
]];
And repeating a performSelector will cause the selector to run repeatedly with no delay. The performSelector action has no duration, it runs instantly. Repeating it forever will create background sprites, either one per frame or, depending on how SK interprets this, possibly even running the selector in an endless loop until the app crashes.
Upvotes: 1