CCSprite not appearing on screen when in action/block - ios

Hello I am trying to make a game in cocos2d-swift, what I am trying to get is when the user taps on the screen and then two CCSprites that are on the screen stop and remove them selfs from the scene. Once they remove the same exact sprites again show up but have a random chance to become 3 other sprites (all properties same except color and type). The problem is I have everything working the code is executing in the correct areas but the sprites are not appearing on the iphone.
Here is the main code to replace the existing sprites.
- (void)resetPerimeterShape {
[baseShape removeFromParent];
[innerShape removeFromParent];
id resetAction = [CCActionCallBlock actionWithBlock:^(void){
baseShape = [CCSprite spriteWithImageNamed:baseShapeShape];
baseShape.position = ccp(self.contentSizeInPoints.width/2,self.contentSizeInPoints.height/2);
baseShape.color = baseShapeColor;
baseShape.rotation = shapeZRotation;
[baseShape setScale:baseShapeSize];
[self addChild:baseShape];
innerShape = [CCSprite spriteWithImageNamed:innerShapeShape];
innerShape.position = ccp(self.contentSize.width/2,self.contentSize.height/2);
innerShape.color = innerShapeColor;
innerShape.rotation = shapeZRotation;
[innerShape setScale:baseShapeSize];
NSLog(#"starting");
//this is running and outputting so i am 100% sure this function is called.
[self addChild:innerShape];
NSLog(#"done");
}];
id reRunAction = [CCActionCallBlock actionWithBlock:^(void){
//endsscene wip
}];
[self runAction:[CCActionSequence actionWithArray:#[resetAction,reRunAction]]];
}
This is a snippet of the code where it is called.
else{
//[self runAction:self.sounds];
NSString *A = [self randomShape];
NSString *B = [self randomShape];
if ([A isEqualToString:B]) {
baseShapeShape = A;
innerShapeShape = A;
}
else {
baseShapeShape = #"SQUARE (1).png";
innerShapeShape = B;
}
innerShapeColor = [self randomColor];
baseShapeColor = [self randomColor];
shapeZRotation = [self randomRotation];
[self resetPerimeterShape];
nap = true;
}

Did you forget to run the action? For instance:
[self runAction:resetAction];

Found the Solution I was Scaleing the sprite by 0 so it basically didn't show up

Related

Using CCScrollView in Cocos2d V3

I'm wondering if anyone can help me with this. I want to add 4 CCScrollViews, that will scroll horizontally, on a CCNode. The CCNode, positioned and held on the device in portrait mode, will fill the entire screen with a normalized contentSize set to cover the entire screen.
So in essence the scroll views will pile on top of each other with the 4th being at the bottom of the screen and the 1st being at the top. Now, I have managed to add the 4 CCScrollViews but only one responds to touches. The others are flat an unmovable. It's almost as though the last CCScrollView added to the node is overlaying the other three and it is the only thing responding to touch requests. All 4 CCScrollViews have their delegate property set to self.
I'm someone coming at Cocos2d with a fair bit of UIKit experience. So, i'm trying to apply my UIScrollView mode of thinking to all of this. Having had a good Google about and coming up with little, I'm wondering if SO can help. I've even been considering winding UIScrollView into Cocos2d.
I guess my main issues here are two-fold. One, I have an issue with touch response. Two, I have an issue with the paging aspect and control of content-size. Via trial and error, I'm sort of getting along but if someone could perhaps write up a bit of a best-practive guide to CCScrollView implementation, specifically where one does not set the contentSize of the CCNode or CCspriteFrame that's added to larger contentView does not fill the entire width of the screen.
Thanks in advance.
#import "CCToolKit.h"
#import "GameShip.h"
typedef enum ShipPart
{
ShipPartHead,
ShipPartBody,
ShipPartWings,
ShipPartBoosters
} ShipPart;
#implementation GameShip
-(instancetype)init {
if (self = [super init]) {
[self addBackground];
[self addScrollTo:ShipPartHead forQtyOfParts:3];
[self addScrollTo:ShipPartBody forQtyOfParts:4];
[self addScrollTo:ShipPartWings forQtyOfParts:3];
[self addScrollTo:ShipPartBoosters forQtyOfParts:3];
}
return self;
}
-(void)addBackground {
CCSprite *bg = [CCSprite spriteWithImageNamed:kGameMainBackGround];
bg.positionType = CCPositionTypeNormalized;
bg.position = ccp(0.5f,0.5f);
[self addChild:bg];
}
-(void)addScrollTo:(ShipPart)shipPart forQtyOfParts:(int)partQty {
NSString *imageFileNameSegment;
switch (shipPart) {
case ShipPartHead:
imageFileNameSegment = #"head";
break;
case ShipPartBody:
imageFileNameSegment = #"body";
break;
case ShipPartWings:
imageFileNameSegment = #"wings";
break;
case ShipPartBoosters:
imageFileNameSegment = #"boosters";
break;
default:
break;
}
CCNode *scrollViewContents = [CCNode node];
scrollViewContents.contentSizeType = CCSizeTypeNormalized;
scrollViewContents.contentSize = CGSizeMake(partQty * 0.65, 0.25f);
NSLog(#"scrollView,height %f", scrollViewContents.boundingBox.size.height);
for (int i = 1; i <= partQty; i++) {
NSString *imageFileName = [NSString stringWithFormat:#"%#%d.png", imageFileNameSegment, i];
CCSprite *shipPartSprite = [CCSprite spriteWithImageNamed:imageFileName];
shipPartSprite.positionType = CCPositionTypeNormalized;
shipPartSprite.position = ccp((i + 0.5f) / partQty, 0.5f);
[scrollViewContents addChild:shipPartSprite];
}
CCScrollView *scrollView = [[CCScrollView alloc] initWithContentNode:scrollViewContents];
scrollView.pagingEnabled = YES;
scrollView.horizontalScrollEnabled = YES;
scrollView.verticalScrollEnabled = NO;
scrollView.color = [CCColor redColor];
scrollView.contentSize = CGSizeMake(0.5f, 0.25f);
scrollView.positionType = CCPositionTypeNormalized;
scrollView.position = ccp(-1.0f, ((shipPart * 0.25f) -0.1f));
scrollView.delegate = self;
//scrollView.description = [NSString stringWithFormat:#"%d", shipPart];
[self addChild:scrollView];
//[scrollView setHorizontalPage:1];
}

Deallocate SKScene after transition to another SKScene in SpriteKit

I have a view controller that has three skscenes as children.
When I transition from one to another, the old skscene doesn't get deallocated.
I want it to get deallocated as if it was never there.
Example:
When I first load the app, only 1 skscene is visible (say it takes up 100mb memory), then I transition to another (100mb more), and then the third (300mb memory).
I would end up with 300mb memory and I want to have 100 at all times.
How can I achieve this?
My view controller:
//
// ViewController.m
// Paddle Jumper
//
// Created by Chance Daniel on 1/18/14.
// Copyright (c) 2014 Max Hudson. All rights reserved.
//
#import "Flurry.h"
#import "ViewController.h"
#import "startViewController.h"
#implementation ViewController{
BOOL sceneSetUp;
}
- (void)viewWillLayoutSubviews
{
if(!sceneSetUp){
[super viewWillLayoutSubviews];
// Configure the view.
SKView * skView = (SKView *)self.view;
//skView.showsFPS = YES;
//skView.showsNodeCount = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if([[defaults objectForKey:#"firstTime"] intValue] != 1){
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"firstTime"];
[defaults setObject:#"ggr" forKey:#"skinSelected"];
[defaults setObject:[NSNumber numberWithInt:2] forKey:#"ggrOwned"];
[defaults setObject:[NSNumber numberWithInt:5000] forKey:#"gona"];
[defaults setObject:[NSNumber numberWithInt:1500] forKey:#"points"];
[defaults setObject:[NSNumber numberWithInt:7] forKey:#"livesLeftValue"];
[defaults setObject:[NSNumber numberWithInt:3] forKey:#"shieldsLeftValue"];
[defaults setObject:[NSNumber numberWithInt:2] forKey:#"lvlTwoLeftValue"];
[defaults setObject:[NSNumber numberWithInt:0] forKey:#"lvlThreeLeftValue"];
}
if(![defaults objectForKey:#"tut_game1"]){
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"tut_game1"];
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"tut_store"];
[defaults setObject:[NSNumber numberWithInt:1] forKey:#"tut_daily"];
}
[defaults synchronize];
// Create and configure the scene.
SKScene * startScene = [StartViewController sceneWithSize:skView.bounds.size];
startScene.scaleMode = SKSceneScaleModeAspectFill;
// Present the scene.
[skView presentScene:startScene];
//[skView presentScene:scene];
sceneSetUp = YES;
}
}
-(void) switchScene{
}
- (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
An SKScene that won't release: //
// SettingsSKScene.m
// Paddle Jumper
//
// Created by Max Hudson on 3/15/14.
// Copyright (c) 2014 Max Hudson. All rights reserved.
//
#import "SettingsSKScene.h"
#import "gameViewController.h"
#import "StoreScene.h"
#interface SettingsSKScene (){
SKSpriteNode *bg;
SKSpriteNode *masterOverlay;
SKSpriteNode *wbox;
SKSpriteNode *gamecenter;
SKSpriteNode *max;
SKSpriteNode *chance;
SKSpriteNode *bryce;
SKSpriteNode *home;
SKSpriteNode *play;
SKSpriteNode *chance_link;
SKSpriteNode *max_link;
SKSpriteNode *bryce_link;
SKSpriteNode *fbButton;
SKSpriteNode *fbToggleYes;
SKSpriteNode *fbToggleNo;
SKLabelNode *story1;
SKLabelNode *story2;
SKLabelNode *story3;
SKLabelNode *chance_name;
SKLabelNode *max_name;
SKLabelNode *bryce_name;
SKLabelNode *chance_role;
SKLabelNode *max_role;
SKLabelNode *bryce_role;
SKLabelNode *chance_handle;
SKLabelNode *max_handle;
SKLabelNode *bryce_handle;
SKTexture *bg_texture;
SKTexture *gamecenter_texture;
SKTexture *wbox_texture;
SKTexture *max_texture;
SKTexture *chance_texture;
SKTexture *bryce_texture;
SKTexture *home_texture;
SKTexture *play_texture;
SKTexture *fb_texture;
SKTexture *toggle_yes_texture;
SKTexture *toggle_no_texture;
}
#end
#implementation SettingsSKScene
-(id) initWithSize:(CGSize)size{
if(self = [super initWithSize:size]){
masterOverlay = [SKSpriteNode spriteNodeWithColor:[SKColor blackColor] size:self.frame.size];
masterOverlay.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
[self addChild:masterOverlay];
bg_texture = [SKTexture textureWithImageNamed:#"settings_bg"];
wbox_texture = [SKTexture textureWithImageNamed:#"white_rect_bg"];
gamecenter_texture = [SKTexture textureWithImageNamed:#"gc"];
max_texture = [SKTexture textureWithImageNamed:#"max"];
chance_texture = [SKTexture textureWithImageNamed:#"chance"];
bryce_texture = [SKTexture textureWithImageNamed:#"bryce"];
home_texture = [SKTexture textureWithImageNamed:#"home_light"];
play_texture = [SKTexture textureWithImageNamed:#"play_light"];
fb_texture = [SKTexture textureWithImageNamed:#"fb_light"];
toggle_yes_texture = [SKTexture textureWithImageNamed:#"toggle_yes"];
toggle_no_texture = [SKTexture textureWithImageNamed:#"toggle_no"];
NSArray *to_preload = #[bg_texture, wbox_texture, gamecenter_texture, max_texture, chance_texture, bryce_texture, home_texture, play_texture, fb_texture, toggle_yes_texture, toggle_no_texture];
[SKTexture preloadTextures:to_preload withCompletionHandler:^{
[self fadeRemove:masterOverlay];
[self initialize];
}];
}
return self;
}
-(void) fadeRemove: (SKNode *) node{
double duration = arc4random() % 10;
duration *= 0.05;
SKAction *fadeOut = [SKAction fadeOutWithDuration:0.1+duration];
SKAction *remove = [SKAction runBlock:^{
[node removeFromParent];
}];
[node runAction:[SKAction sequence:#[fadeOut, remove]]];
}
-(void) initialize{
bg = [SKSpriteNode spriteNodeWithTexture:bg_texture];
bg.position = CGPointMake(self.frame.size.width/2, self.frame.size.height/2);
bg.zPosition = 0;
wbox = [SKSpriteNode spriteNodeWithTexture:wbox_texture];
wbox.position = CGPointMake(self.frame.size.width/2, 70);
wbox.alpha = 0.8;
wbox.zPosition = 2;
gamecenter = [SKSpriteNode spriteNodeWithTexture:gamecenter_texture];
gamecenter.position = CGPointMake(self.frame.size.width/2 + 100, self.frame.size.height/2-2);
gamecenter.name = #"gc";
gamecenter.zPosition = 2;
fbButton = [SKSpriteNode spriteNodeWithTexture:fb_texture];
fbButton.size = CGSizeMake(36, 36);
fbButton.position = CGPointMake(self.frame.size.width/2 - 115, self.frame.size.height/2-2);
fbButton.name = #"fb";
fbButton.zPosition = 2;
fbToggleNo = [SKSpriteNode spriteNodeWithTexture:toggle_no_texture];
fbToggleNo.position = CGPointMake(self.frame.size.width/2 - 50, self.frame.size.height/2-2);
fbToggleNo.size = CGSizeMake(fbToggleNo.size.width/3, fbToggleNo.size.height/3);
fbToggleNo.name = #"fbno";
fbToggleNo.zPosition = 2;
fbToggleYes = [SKSpriteNode spriteNodeWithTexture:toggle_yes_texture];
fbToggleYes.position = CGPointMake(self.frame.size.width/2 - 70, self.frame.size.height/2-2);
fbToggleYes.name = #"fbyes";
fbToggleYes.zPosition = 2;
int hpBuffer = 40;
int hpZ = 2;
home = [SKSpriteNode spriteNodeWithTexture:home_texture];
home.position = CGPointMake(hpBuffer, self.frame.size.height - hpBuffer);
home.zPosition = hpZ;
home.name = #"home";
play = [SKSpriteNode spriteNodeWithTexture:play_texture];
play.position = CGPointMake(self.frame.size.width - hpBuffer, self.frame.size.height - hpBuffer);
play.zPosition = hpZ;
play.name = #"play";
[self addChild:bg];
[self addChild:wbox];
[self addChild:gamecenter];
[self addChild:home];
[self addChild:play];
[self addChild:fbButton];
[self addChild:fbToggleNo];
[self addCredits];
[self addStory];
}
-(void) addCredits{
/* images */
int cmbZ = wbox.zPosition + 1;
int cmbY = wbox.position.y;
int cmbXUnit = 132;
int cmbX = self.frame.size.width/2 - (3*cmbXUnit)/2 + 20;
int cmbWidth = 40;
chance = [SKSpriteNode spriteNodeWithTexture:chance_texture];
max = [SKSpriteNode spriteNodeWithTexture:max_texture];
bryce = [SKSpriteNode spriteNodeWithTexture:bryce_texture];
chance.zPosition = cmbZ;
max.zPosition = cmbZ;
bryce.zPosition = cmbZ;
chance.position = CGPointMake(cmbX+cmbXUnit*0, cmbY+3);
max.position = CGPointMake(cmbX+cmbXUnit*1 + 10, cmbY);
bryce.position = CGPointMake(cmbX+cmbXUnit*2 + 10, cmbY+5);
chance.size = CGSizeMake(cmbWidth, (chance.size.height/chance.size.width)*cmbWidth);
max.size = CGSizeMake(cmbWidth, (max.size.height/max.size.width)*cmbWidth);
bryce.size = CGSizeMake(cmbWidth, (bryce.size.height/bryce.size.width)*cmbWidth);
[self addChild:chance];
[self addChild:max];
[self addChild:bryce];
/* names */
int cmb_nameXUnit = 30;
int cmb_nameY = wbox.position.y - 10;
int cmb_nameFontSize = 18;
chance_name = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
max_name = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
bryce_name = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
chance_name.text = #"Chance Daniel";
max_name.text = #"Max Hudson";
bryce_name.text = #"Bryce Daniel";
chance_name.fontColor = [SKColor blackColor];
max_name.fontColor = [SKColor blackColor];
bryce_name.fontColor = [SKColor blackColor];
chance_name.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
max_name.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
bryce_name.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
chance_name.position = CGPointMake(chance.position.x + cmb_nameXUnit, cmb_nameY);
max_name.position = CGPointMake(max.position.x + cmb_nameXUnit, cmb_nameY);
bryce_name.position = CGPointMake(bryce.position.x + cmb_nameXUnit, cmb_nameY);
chance_name.fontSize = cmb_nameFontSize;
max_name.fontSize = cmb_nameFontSize;
bryce_name.fontSize = cmb_nameFontSize;
chance_name.zPosition = cmbZ;
max_name.zPosition = cmbZ;
bryce_name.zPosition = cmbZ;
[self addChild:chance_name];
[self addChild:max_name];
[self addChild:bryce_name];
/* roles */
int cmb_roleY = wbox.position.y - 25;
int cmb_roleFontSize = 11;
chance_role = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
max_role = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
bryce_role = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
chance_role.text = #"Programmer";
max_role.text = #"Programmer";
bryce_role.text = #"Graphic Designer";
chance_role.fontColor = [SKColor darkGrayColor];
max_role.fontColor = [SKColor darkGrayColor];
bryce_role.fontColor = [SKColor darkGrayColor];
chance_role.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
max_role.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
bryce_role.horizontalAlignmentMode = SKLabelHorizontalAlignmentModeLeft;
chance_role.position = CGPointMake(chance.position.x + cmb_nameXUnit + 19, cmb_roleY);
max_role.position = CGPointMake(max.position.x + cmb_nameXUnit + 12, cmb_roleY);
bryce_role.position = CGPointMake(bryce.position.x + cmb_nameXUnit + 6, cmb_roleY);
chance_role.fontSize = cmb_roleFontSize;
max_role.fontSize = cmb_roleFontSize;
bryce_role.fontSize = cmb_roleFontSize;
chance_role.zPosition = cmbZ;
max_role.zPosition = cmbZ;
bryce_role.zPosition = cmbZ;
[self addChild:chance_role];
[self addChild:max_role];
[self addChild:bryce_role];
/* twitter handles */
int cmb_handY = wbox.position.y - 40;
int cmb_handFontSize = 10;
chance_handle = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
max_handle = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
bryce_handle = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
chance_handle.text = #"#ChanceOfThat";
max_handle.text = #"#max_hud";
bryce_handle.text = #"#BryceOfLife";
chance_handle.fontColor = [SKColor darkGrayColor];
max_handle.fontColor = [SKColor darkGrayColor];
bryce_handle.fontColor = [SKColor darkGrayColor];
chance_handle.position = CGPointMake(chance.position.x, cmb_handY);
max_handle.position = CGPointMake(max.position.x, cmb_handY);
bryce_handle.position = CGPointMake(bryce.position.x, cmb_handY);
chance_handle.fontSize = cmb_handFontSize;
max_handle.fontSize = cmb_handFontSize;
bryce_handle.fontSize = cmb_handFontSize;
chance_handle.zPosition = cmbZ;
max_handle.zPosition = cmbZ;
bryce_handle.zPosition = cmbZ;
[self addChild:chance_handle];
[self addChild:max_handle];
[self addChild:bryce_handle];
/* touchzones */
CGSize cmdL_size = CGSizeMake(120, 70);
SKColor *cmdL_color = [SKColor clearColor];
int cmdLZ = 3;
int cmdLX = cmbX + 40;
chance_link = [SKSpriteNode spriteNodeWithColor:cmdL_color size:cmdL_size];
max_link = [SKSpriteNode spriteNodeWithColor:cmdL_color size:cmdL_size];
bryce_link = [SKSpriteNode spriteNodeWithColor:cmdL_color size:cmdL_size];
chance_link.position = CGPointMake(cmdLX+cmbXUnit*0, cmbY);
max_link.position = CGPointMake(cmdLX+cmbXUnit*1 + 10, cmbY);
bryce_link.position = CGPointMake(cmdLX+cmbXUnit*2 + 10, cmbY);
chance_link.zPosition = cmdLZ;
max_link.zPosition = cmdLZ;
bryce_link.zPosition = cmdLZ;
chance_link.name = #"c_handle";
max_link.name = #"m_handle";
bryce_link.name = #"b_handle";
[self addChild:chance_link];
[self addChild:max_link];
[self addChild:bryce_link];
}
-(void) addStory{
int stX = self.frame.size.width/2;
int stZ = 2;
SKColor *stColor = [SKColor whiteColor];
story1 = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
story2 = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
story3 = [SKLabelNode labelNodeWithFontNamed:#"BebasNeue"];
story1.text = #"Gon";
story2.text = #"Help Gon-Gon and his friends get";
story3.text = #"Back to the time they came from!";
story1.fontColor = stColor;
story2.fontColor = stColor;
story3.fontColor = stColor;
story1.zPosition = stZ;
story2.zPosition = stZ;
story3.zPosition = stZ;
story1.position = CGPointMake(stX, self.frame.size.height - 55);
story1.fontSize = 50;
story2.position = CGPointMake(stX, self.frame.size.height - 95);
story2.fontSize = 30;
story3.position = CGPointMake(stX, self.frame.size.height - 120);
story3.fontSize = 20;
[self addChild:story1];
[self addChild:story2];
[self addChild:story3];
}
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touchUI = [touches anyObject];
CGPoint touchPoint = [touchUI locationInNode:self];
SKNode *touchNode = [self nodeAtPoint:touchPoint];
if([touchNode.name isEqualToString:#"home"]){
StartViewController *svc = [[StartViewController alloc] initWithSize:self.size];
SKTransition *fade = [SKTransition fadeWithColor :[SKColor blackColor] duration:0.4];
[self.view presentScene:svc transition:fade];
}
if([touchNode.name isEqualToString:#"play"]){
gameViewController *gvc = [[gameViewController alloc] initWithSize:self.size];
SKTransition *fade = [SKTransition fadeWithColor :[SKColor blackColor] duration:0.4];
[self.view presentScene:gvc transition:fade];
}
if([touchNode.name isEqualToString:#"gc"]){
NSDictionary * dict = [[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithBool:1], #"showGC", nil];
[[NSNotificationCenter defaultCenter]postNotificationName:#"kNotificationUpdateBannerView" object:self userInfo:dict];
}
if([touchNode.name isEqualToString:#"c_handle"]){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=ChanceOfThat"]];
int worked = [[UIApplication sharedApplication] openURL:urlApp];
if(!worked){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"https://twitter.com/#!/ChanceOfThat"]];
[[UIApplication sharedApplication] openURL:urlApp];
}
}
if([touchNode.name isEqualToString:#"m_handle"]){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=max_hud"]];
int worked = [[UIApplication sharedApplication] openURL:urlApp];
if(!worked){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"https://twitter.com/#!/max_hud"]];
[[UIApplication sharedApplication] openURL:urlApp];
}
}
if([touchNode.name isEqualToString:#"b_handle"]){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"twitter:///user?screen_name=BryceOfLife"]];
int worked = [[UIApplication sharedApplication] openURL:urlApp];
if(!worked){
NSURL *urlApp = [NSURL URLWithString: [NSString stringWithFormat:#"%#", #"https://twitter.com/#!/BryceOfLife"]];
[[UIApplication sharedApplication] openURL:urlApp];
}
}
}
#end
A similar problem was faced by the person who asked this question.
When asked whether he was able to solve it, they said:
Yes, I did, there wasn't anything I could do about it from the scene
or Sprite Kit for that matter, I simply needed to remove the scene and
the view containing it completely from the parent view, cut all its
bonds to the other parts of the system, in order for the memory to be
deallocated as well.
You should use separate views for each of your scene and transition between these views. You can follow these steps to make it look natural:
1 - At the point you want to transition from one scene to the other, take a snapshot of the scene using the following code:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, scale);
[self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then, add this image as a subview of the SKView of the current scene, and remove the scene.
2 - Initialise the new scene on another view, and transition between the two views using UIView animation.
3 - Remove the first view from it's superview.
I am just starting out with SpriteKit in Swift and I had the same problem: My intro scene was playing some music that I wanted to stop after transitioning to the main menu scene, and I figured the deinit would be a good place to put the AVAudioPlayer.stop() call in, but deinit was never being called.
After looking around I learned that it may be because of some strong references to the scene, so in my GameViewController:UIViewController subclass I changed this code:
let intro = IntroScene(size: skView.bounds.size)
skView.presentScene(intro)
to
skView.presentScene(IntroScene(size: skView.bounds.size))
and in the intro scene, that I wanted to be deallocated, I changed
let mainMenu = MainMenuScene(size: self.size)
let crossFade = SKTransition.crossFadeWithDuration(1)
self.scene.view.presentScene(mainMenu, transition: crossFade)
to
self.scene.view.presentScene(MainMenuScene(size: self.size),
transition: SKTransition.crossFadeWithDuration(1))
and it worked! After the transition was complete the deinit method got called.
I assume that the outgoing scene was not being deinitialized because there were variables holding references to it.
December 2019/Swift 5
Update:
My layout:
I have a single view controller that contains 2 SKViews which each of them have their own unique SKScene that are presented at the same time. One SKView & its SKScene is the main overworld where the player character is rendered, controlled, NPC's rendered, camera tracking, the whole shebang etc., & the other SKView & its SKScene display the mini map of the overworld.
You can imagine there are also quite a number of SKSpriteNode's & lot of them ALWAYS have some kind of SKAction/animation running non-stop (swaying trees for instance). My SKScenes even contain their own arrays pointing at specific groups of SKSpriteNodes, such as, character nodes, building nodes, tree nodes. This is for quick access & convenience purposes.
Plus, I have a few singletons that either contain an array of SKTextures, or character models, etc.. They are kept around as an optimization for quick data access rather than reading from disc/accessing storage every time I need something.
There are even UIKit elements used for the game UI inside the view controller.
Many other objects, such as, my models that contain data on characters, buildings, the entire game session all have some kind of delegates pointing at someone. On top of all of this the codebase is massive.
After observing memory in the debug session I found out the sure-fire way to make sure nothing is retained is to absolutely make sure the following:
Memory handling:
Pass in nil for scene presentation & set that skview's property/pointer to nil as well
Remove all view controller primary view subviews
Absolutely set every delegate you have to nil !!!!!!!!!!!!!!
Remove all observers IF you happen to have any
Anywhere you have an array/dictionary or pointer to some kind of object YOU created, set it to nil/empty it
CRITICAL: put all of the above into a single function/method & make sure to call it right before changing view controllers!
*NOTE: IF you have 1 view controller for the entire app (congrats on squeezing everything - youz overlord squeezer), then do NOT nil everything & use caution. BUT the previous scene still needs to be set to nil during presentation.
So something like this if you're jumping between view controllers:
/// Remove all pointers to any data, nodes & views.
fileprivate func cleanUp() {
guard self.skView != nil else { return }
// Session was passing data updates to this view controller; time to nil it
self.gameSessionModel.delegate = nil
self.skView.presentScene(nil)
self.skViewMiniMap.presentScene(nil)
self.skView = nil
self.skViewMiniMap = nil
for subview in self.view.subviews {
subview.removeFromSuperview()
}
}
/// Take the user back to the main menu module/view controller.
fileprivate func handleMenuButton() {
// First, clean up everything
self.cleanUp()
// Then go to the other view controller
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "MainViewController")
self.show(view, sender: .none)
}
Call handleMenuButton() function or whatever function you use to present another view controller, but make sure the cleanUp() function is called within it!
*NOTE: Classes such as 'gameSessionModel' are entirely my own custom classes. Xcode will throw an error on such things (unless you magically have the same one...) so delete such things. They are only used as example code in case you have delegates pointing at the current view controller.
If you're only presenting different SKScene's with a single SKView, then your cleanUp() function can end up being "lighter" as such:
/// Remove all pointers to any data, nodes & views.
fileprivate func cleanUp() {
self.skView.presentScene(nil)
// Previous scene was set to nil, its deinit called. Now onto the new scene:
let gameScene = self.setupGameScene(id: "perhapsYouUseStringIDsToDisntiguishBetweenScenes?")
self.skView.presentScene(gameScene)
}
*NOTE: Do NOT forget to use the SKScene's deinit method to remove anything you won't need. It's a practice I use all the time for all my classes to untangle anything I might have missed.
There's nothing wrong with SKScene or SKView, as long as I can see. Make sure the scene instance is not strongly reference anywhere else, especially inside a block. Blocks are highly probable to be ignored.
More about weak reference inside a block: https://stackoverflow.com/a/17105368/571489
As far as I see, you do have a block strongly referencing the scene instance:
[SKTexture preloadTextures:to_preload withCompletionHandler:^{
[self fadeRemove:masterOverlay];
[self initialize];
}];
EDIT: My previous idea about an NSTimer was irrelevant
To make sure this is an issue isolated to this scene, override the dealloc methods of all scenes you might have (including this one) like this:
-(void)dealloc {
NSLog(#"Dealloc <scene name>");
}
Look at your other scene transitions, see if they deallocate properly. Find the differences between these scenes. This will help you see if it's an isolated issue or a bigger problem. Once you have the problem fixed be sure to comment out or remove the dealloc method as it is overriding the one that actually deallocates the memory. Hopefully this helps!

NSArray removeObject removes all objects in array

My project creates a bomb, an explosion, then checks for collisions in the explosions and finally delete the bombs that didn't get hit in a collision. This is explained in more detail here. The following code does this.
-(void)placeBomb
{
NSLog(#"Bomb placed");
_circle = [[CCSprite alloc]initWithFile:#"Circle.png"];
CGPoint circle0position = ccp(_cat.position.x , _cat.position.y);
CGPoint c0TileCoordt = [self tileCoordForPosition:circle0position];
CGPoint c0TileCoord = [self positionForTileCoord:c0TileCoordt];
_circle.position = c0TileCoord;
[self addChild:_circle];
id fade = [CCScaleTo actionWithDuration:3.5 scale:0];
[_circle runAction:fade];
double delayInSeconds = 3.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[self explosionFromPoint:c0TileCoordt withSprite:_circle];
});
}
- (BOOL)isLocationBombable:(CGPoint)tileCoord;
{
if ([self isValidTileCoord:tileCoord] && ![self isWallAtTileCoord:tileCoord])
{
return YES;
}
else
{
return NO;
}
}
-(void)explosionFromPoint:(CGPoint)explosionPoint withSprite:(CCSprite*)sprite;
{
//int
explosionLenght += 1;
if (explosionLenght >= 7) //Just for testing purposes, don't have a way to increase it naturally.
{
explosionLenght = 1;
}
BOOL topB = YES;
BOOL leftB = YES;
BOOL bottomB = YES;
BOOL rightB = YES;
int bombX = (explosionPoint.x + 1);
int bombY = (explosionPoint.y + 1);
int bombNegX = (explosionPoint.x - 1);
int bombNegY = (explosionPoint.y - 1);
CGPoint top = ccp(explosionPoint.x, bombY);
CGPoint left = ccp(bombNegX, explosionPoint.y);
CGPoint bottom = ccp(explosionPoint.x, bombNegY);
CGPoint right = ccp(bombX, explosionPoint.y);
if (![self isLocationBombable:top])
{topB = NO;}
if (![self isLocationBombable:left])
{leftB = NO;}
if (![self isLocationBombable:bottom])
{bottomB = NO;}
if (![self isLocationBombable:right])
{rightB = NO;}
for (int i = 0; i <= explosionLenght; i++) {
int bombX = (explosionPoint.x + i);
int bombY = (explosionPoint.y + i);
int bombNegX = (explosionPoint.x - i);
int bombNegY = (explosionPoint.y - i);
CGPoint top = ccp(explosionPoint.x, bombY);
CGPoint left = ccp(bombNegX, explosionPoint.y);
CGPoint bottom = ccp(explosionPoint.x, bombNegY);
CGPoint right = ccp(bombX, explosionPoint.y);
CCSprite *circleTop = [[CCSprite alloc]initWithFile:#"Circle.png"];
CCSprite *circleLeft = [[CCSprite alloc]initWithFile:#"Circle.png"];
CCSprite *circleBottom = [[CCSprite alloc]initWithFile:#"Circle.png"];
CCSprite *circleRight = [[CCSprite alloc]initWithFile:#"Circle.png"];
int scaleTime = 5;
if ([self isLocationBombable:top] && topB == YES)
{
circleTop.position = [self positionForTileCoord:top];
[self addChild:circleTop];
id fadeTop = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleTop.position] two:[CCScaleTo actionWithDuration:scaleTime scale:0]];
[circleTop runAction:fadeTop];
}
if ([self isLocationBombable:left] && leftB == YES)
{
circleLeft.position = [self positionForTileCoord:left];
[self addChild:circleLeft];
id fadeLeft = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleLeft.position] two:[CCScaleTo actionWithDuration:scaleTime scale:0]];
[circleLeft runAction:fadeLeft];
}
if ([self isLocationBombable:bottom] && bottomB == YES)
{
circleBottom.position = [self positionForTileCoord:bottom];
[self addChild:circleBottom];
id fadeBottom = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleBottom.position] two:[CCScaleTo actionWithDuration:scaleTime scale:0]];
[circleBottom runAction:fadeBottom];
}
if ([self isLocationBombable:right] && rightB == YES)
{
circleRight.position = [self positionForTileCoord:right];
[self addChild:circleRight];
id fadeRight = [CCSequence actionOne:[CCMoveTo actionWithDuration:1 position:circleRight.position] two:[CCScaleTo actionWithDuration:scaleTime scale:0]];
[circleRight runAction:fadeRight];
}
}
[currentBombs addObject:sprite];
int a = [currentBombs count];
NSLog(#"cBCount: %i",a);
NSLog(#"Explosion done, call checkdamage");
[self schedule:#selector(checkDamageForBomb)];
[self performSelector:#selector(removeSprite:) withObject:sprite afterDelay:5];
}
-(void)removeSprite:(CCSprite *)sprite{
int aa = [currentBombs count];
NSLog(#"removeSprite startcall cbc: %i",aa);
if([currentBombs containsObject:sprite])
{
NSLog(#"Found sprite in array, deleted!");
[currentBombs removeObject:sprite];
int a = [currentBombs count];
NSLog(#"containObject cbc: %i",a);
}
else {
NSLog(#"Didn't find the object in array, didn't delete!");
int a = [currentBombs count];
NSLog(#"elseCO cbc: %i",a);
}
if (currentBombs.count == 0)
{
[self stopCheckDamage];
}
}
-(void)stopCheckDamage{
NSLog(#"StopCheckDamage");
[self unschedule:#selector(checkDamageForBomb)];
}
-(void)checkDamageForBomb{
for (CCSprite* bomb in currentBombs)
{
CGPoint bombPos = [self tileCoordForPosition:bomb.position];
for (int i = 0; i <= explosionLenght; i++) {
CGPoint playerPos = [self tileCoordForPosition:_cat.position];
int bombX = (bombPos.x + i);
int bombY = (bombPos.y + i);
int bombNegX = (bombPos.x - i);
int bombNegY = (bombPos.y - i);
CGPoint centre = bombPos;
CGPoint top = ccp(centre.x, bombY);
CGPoint left = ccp(bombNegX, centre.y);
CGPoint bottom = ccp(centre.x, bombNegY);
CGPoint right = ccp(bombX, centre.y);
//pastebin.com/biuQBfnv
if (CGPointEqualToPoint(top, playerPos) || CGPointEqualToPoint(left, playerPos) || CGPointEqualToPoint(bottom, playerPos) || CGPointEqualToPoint(right, playerPos))
{
playerHits += 1;
NSLog(#"Player hit %i",playerHits);
[currentBombs removeObject:bomb];
break;
}
}
}
}
My problem is with the -(void)removeSprite:(CCSprite *)sprite{method. This is supposed to delete only the one it got called with, but instead it kills them all, as you can see in this log.
15:14:02.499 Tile[1549:c07] Bomb placed
15:14:03.816 Tile[1549:c07] Bomb placed
15:14:05.501 Tile[1549:c07] cBCount: 1
15:14:05.501 Tile[1549:c07] Explosion done, call checkdamage
15:14:06.818 Tile[1549:c07] cBCount: 2
15:14:06.819 Tile[1549:c07] Explosion done, call checkdamage
15:14:06.819 Tile[1549:c07] CCScheduler#scheduleSelector. Selector already scheduled. Updating interval from: 0.00 to 0.00
15:14:10.503 Tile[1549:c07] removeSprite startcall cbc: 2 // has 2
15:14:10.503 Tile[1549:c07] Found sprite in array, deleted! //Line above and under
15:14:10.504 Tile[1549:c07] containObject cbc: 0 //Deleted 2, supposed to kill 1
15:14:10.505 Tile[1549:c07] StopCheckDamage
15:14:11.820 Tile[1549:c07] removeSprite startcall cbc: 0
15:14:11.820 Tile[1549:c07] Didn't find the object in array, didn't delete!
15:14:11.821 Tile[1549:c07] elseCO cbc: 0
15:14:11.821 Tile[1549:c07] StopCheckDamage
If you look at the log location in the code above, you will see that it deletes two instead of the one I wanted to. How can I prevent this behaviour or customise it to only kill the proper sprite?
Edit: To clarify
I thought that as I use the spritein the -(void)explosionFromPoint:(CGPoint)explosionPoint withSprite:(CCSprite*)sprite; this would somehow just give an unique ID to the object and know which one I was talking about. I'm new to coding.
You have the same sprite in the array twice. Both entries are removed. If you're going to use removeObject you need to create multiple sprite objects. Otherwise you need to use removeObjectAtIndex.
The problem as #HotLicks mentioned is you push the same instance in the array.
You should use a local CCSprite instance so your scheduled calls will use different instances. The reason for adding the same instance twice is because placeBomb is called twice before anything happens and in the second call you override the first instance you created. Since _circle is a pointer by the time both scheduled tasks will be called _circle will point to the same instance in both.
So change :
_circle = [[CCSprite alloc]initWithFile:#"Circle.png"];
To :
CCSprite *circle = [[CCSprite alloc]initWithFile:#"Circle.png"];
and update the rest of the method with circle and not _circle.
There is something that is going wrong in your code. Try checking something like that when you remove object from array do this:
if([currentBombs count]>1 )
[currentBombs removeObjectAtIndex:1];
If your code works fine. that is only one object removed from your array. Then I suggest you to check your removeSprite method print sprite object to check what's going wrong.
I think you can use tag values if you using same sprite objects.
You probably added the same (indiviudal) sprite twice?
Instead of logging a variable that has the count, you can log the object itself. It will print the value of its description. That will print out the class and address for all NSObject subclasses per default. Actually NSMutableArray (and similar NS... classes) print quite well.
NSLog ("%#",myObj);
Doing so you probably see more clearly what really happens.
Don't use class variable while creating bombs and try....
CCSprite * _circle = [[CCSprite alloc]initWithFile:#"Circle.png"];

How to restart a Cocos2D application?

I am making a game, and everything works except the restart button. I would like the game to start again from the restart method, which loads everything onto the screen and sets up gameplay. However I get an error saying "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HelloWorldLayer resetAll:]: unrecognized selector sent to instance 0x9142c70'"
Below is the code for the main class (I'm assuming you guys don't need the other classes, as they work fine):
#import "HelloWorldLayer.h"
#import "AppDelegate.h"
#import "Block.h"
#import "Character.h"
#implementation HelloWorldLayer
-(id) init{ //Init method
if(self = [super initWithColor:ccc4(84,56,8,26)]){ //Initialise with a black background
[self restart];
}
return self;
}
-(void) setUpMenus{
moveRight = [CCMenuItemImage itemWithNormalImage:#"GoRightButton.png" selectedImage:#"GoRightButtonSelected.png" target: self selector:#selector(moveRightMethod:)];
moveLeft = [CCMenuItemImage itemWithNormalImage:#"GoLeftButton.png" selectedImage:#"GoLeftButtonSelected.png" target: self selector:#selector(moveLeftMethod:)];
moveUp = [CCMenuItemImage itemWithNormalImage:#"GoUpButton.png" selectedImage:#"GoUpButtonSelected.png" target: self selector:#selector(moveUpMethod:)];
moveDown = [CCMenuItemImage itemWithNormalImage:#"GoDownButton.png" selectedImage:#"GoDownButtonSelected.png" target: self selector:#selector(moveDownMethod:)];
moveRight.position = ccp(player.image.position.x - 10, player.image.position.y);
moveLeft.position = ccp(player.image.position.x - 80, player.image.position.y);
moveUp.position = ccp(player.image.position.x - 40, player.image.position.y + 30);
moveDown.position = ccp(player.image.position.x - 40, player.image.position.y - 30);
myScore.position = ccp(player.image.position.x - 20, player.image.position.y + 100);
mainMenu = [CCMenu menuWithItems:moveRight, moveLeft, moveUp, moveDown, nil];
[self addChild: mainMenu];
}
-(void) enemyMoveTowardsPlayer{
CGPoint playerPosition = player.image.position;
[enemy moveToLocationEnemy:&playerPosition];
}
-(void) moveRightMethod: (id) sender{
if(enemyExists == YES){
[self enemyMoveTowardsPlayer];
}
if(player.image.position.x < 5000){
[player move];
[self changeBack];
}
}
-(void) moveLeftMethod: (id) sender{
if(enemyExists == YES){
[self enemyMoveTowardsPlayer];
}
if(player.image.position.x > 20){
[player moveLeft];
[self change];
}
}
-(void) moveUpMethod: (id) sender{
if(enemyExists == YES){
[self enemyMoveTowardsPlayer];
}
if(player.image.position.y < 7000){
[player moveUp];
[self changeUp];
}
}
-(void) moveDownMethod: (id) sender{
if(player.image.position.y > -100){
[player moveDown];
[self changeDown];
}
if(enemyExists == YES){
[self enemyMoveTowardsPlayer];
}
}
-(void) callSpawnEnemy{
CCSprite *enemyImage = [CCSprite spriteWithFile:#"Lizard_Sprite.png"]; //Enemy picture
enemy = [[Character alloc] initWithImageAndNameAndProjectile:enemyImage
andAnotherParam:#"Enemy"];
//Enemy instantiation
enemy.image.position = ccp(100,400);
[self addChild:enemy.image]; //Add the enemy image object
enemyExists = YES;
}
-(void)ccTouchesBegan:(NSSet*)touches withEvent:(UIEvent*)event{
//CODE FOR HANDLING TOUCH EVENTS. THE BELOW CODE SIMPLY CREATES AND INSTANTIATES A TOUCH OBJECT AND STORES THE LOCATION
CGPoint touchLocation;
for(UITouch *touch in touches ) {
touchLocation = [touch locationInView: [touch view]];
CGPoint prevLocation = [touch previousLocationInView: [touch view]];
touchLocation = [[CCDirector sharedDirector] convertToGL: touchLocation];
prevLocation = [[CCDirector sharedDirector] convertToGL: prevLocation];
CGPoint diff = ccpSub(touchLocation,prevLocation);
[self setPosition: ccpAdd(self.position, diff)];
}
//
NSLog(#"%f", touchLocation.x);
NSLog(#"%f",touchLocation.y);
}
-(void) change{ //Code to change the image to a leftward facing sprite
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:#"PlayerSpriteLeftHugeV2.png"];
[player.image setTexture: tex];
}
-(void) changeBack{ //Code to change the image to a rightward facing sprite
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:#"PlayerSpriteHugeV2.png"];
[player.image setTexture: tex];
}
-(void) changeUp{ //Code to change the image to a leftward facing sprite
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:#"PlayerSpriteUp.png"];
[player.image setTexture: tex];
}
-(void) changeDown{ //Code to change the image to a rightward facing sprite
CCTexture2D* tex = [[CCTextureCache sharedTextureCache] addImage:#"PlayerSpriteDown.png"];
[player.image setTexture: tex];
}
-(void) generate2dMap{ //Method to generate a 130 by 80 block map (10400 blocks)
for(int i = 20; i < 13000; i+=100){ //X coordinate. Increment by length of each block.
for(int g = 20; g < 8000; g+=100){ //Y coordinate. Increment by length of each block.
CGPoint p = CGPointMake(i, g); //Point for the block. Changes every time the loop goes through
int randNumber = rand() % 11; //Random number to determine the block type
if(randNumber < 1){ //Only 1 in 10 blocks should be gold blocks
Block* gold = [[Block alloc] initWithTypePositionAndImage:#"Gold" andImageName:[CCSprite spriteWithFile:#"GoldBlockHuge.png"]];
//Create a gold object and pass the appropriate information to the constructor
[allBlocks addObject: gold]; //Add the gold object to the mutable array
gold.image.position = p; //Set the block image position to be the current location
[self addChild:gold.image]; //Add the block image to the screen
}
else{ //More common. Should occur 9/10 times
Block* dirt = [[Block alloc]
initWithTypePositionAndImage:#"Dirt" andImageName:[CCSprite spriteWithFile:#"DirtBlockHuge.png"]];
//Create a dirt object and pass the appropriate information to the constructor
[allBlocks addObject: dirt]; //Add the dirt object to the mutable array
dirt.image.position = p; //Set the block image position to be the current location
[self addChild:dirt.image]; //Add the block image to the screen
}
}
}
}
-(void) checkScore{ //Update the score. Set the score object to be the score primative from the player object
[myScore setString:[NSString stringWithFormat:#"%d", player->score]];
stringScore = myScore.string;
}
-(BOOL) checkForCollisions{ //Method to check for collision detection
for(Block *b in allBlocks){ //Loop through all blocks to check if any have collided with the player image
CGRect playerRect = CGRectMake(player.image.position.x, player.image.position.y, 90, 84);
//Rectangle object for the player
CGRect blockRect = CGRectMake(b.image.position.x, b.image.position.y, 50, 50);
//Rectangle object for each block
if(CGRectIntersectsRect(playerRect, blockRect)){ //If the block is in the state of intersecting the player
amountOfBlocks++;
collisionSprite = b; //Set the collision object to be the block
if([collisionSprite.type isEqualToString:#"Gold"]){ //If it's collided with a gold object...
[player incrementScoreBy:100]; //Then increment the score by 100
}
else if([collisionSprite.type isEqualToString:#"Bronze"]){
[player incrementScoreBy:20];
}
else if([collisionSprite.type isEqualToString:#"Silver"]){
[player incrementScoreBy:50];
}
else if([collisionSprite.type isEqualToString:#"Dirt"]){
[player incrementScoreBy:2];
}
return YES; //There has been a collision, and you should terminate this round of the method
}
if(enemyExists == YES){
CGRect enemyRect = CGRectMake(enemy.image.position.x, enemy.image.position.y, 80,80);
if(CGRectIntersectsRect(enemyRect, blockRect)){
collisionSprite = b;
enemyHitBlock = YES;
return YES;
}
if(CGRectIntersectsRect(enemyRect, playerRect)){
enemyHitPlayer = YES;
return YES;
}
}
}
return NO; //There has not been a collision, and you should terminate this round of the method
}
-(void) update: (ccTime) dt{ //Update method called on regular interval that checks for collisions, updates the score
if(isDone == NO){
if([self checkForCollisions]){ //Check for collisions
[self removeChild:collisionSprite.image cleanup:YES]; //Remove the sprite if there has been a collision
[allBlocks removeObject:collisionSprite]; //Remove the object if there has been a collision
if(enemyExists == YES){
if(enemyHitPlayer == YES){
[self removeChild:enemy.image cleanup:YES];
enemyHitPlayer = NO;
enemyExists = NO;
}
if(enemyHitBlock == YES){
[self removeChild:collisionSprite.image cleanup:YES]; //Remove the sprite if there has been a collision
enemyHitBlock = NO;
}
}
}
mainMenu.position = ccp(player.image.position.x - 10, player.image.position.y);
myScore.position = ccp(player.image.position.x - 20, player.image.position.y + 100);
if(enemyExists == NO){
[self callSpawnEnemy];
}
[self checkScore]; //Update the score
if([stringScore intValue] >= 200){
[self endGame];
}
}
}
-(void) endGame{
isDone = YES;
endGameString = #"You reached the max score in ";
stringBlockCount = [NSString stringWithFormat:#"%d blocks", amountOfBlocks];
totalString = [endGameString stringByAppendingString:stringBlockCount];
endGameMessage = [CCLabelTTF labelWithString:totalString fontName:#"Times New Roman" fontSize:20];
CGSize winSize = [[CCDirector sharedDirector] winSize];
CGPoint point = ccp(winSize.height/2,winSize.width/2);
endGameMessage.position = point;
[self stopAllActions];
[self runAction:[CCFollow actionWithTarget:endGameMessage]];
[self removeAllChildrenWithCleanup:YES];
[self addChild: endGameMessage];
restart = [CCMenuItemImage itemWithNormalImage:#"RestartButton.png" selectedImage:#"RestartButtonSelected.png" target: self selector:#selector(resetAll:)];
restart.position = ccp(point.x - 500, point.y - 250);
restartMenu = [CCMenu menuWithItems:restart, nil];
[self addChild:restartMenu];
}
-(void) restart{
[self removeAllChildrenWithCleanup:YES];
NSLog(#"Got here");
isDone = NO;
amountOfBlocks = 0;
allBlocks = [[NSMutableArray alloc] init]; //Instantiate array that holds all blocks
collisionSprite = [[Block alloc] init]; //Instantiate object that holds the collision object
enemyCollisionSprite = [[Character alloc] init];
enemyHitBlock = NO;
enemyHitPlayer = NO;
[self generate2dMap]; //Generate the map
self.isTouchEnabled = YES; //Enable touch technology
CCSprite *playerImage = [CCSprite spriteWithFile:#"PlayerSpriteHugeV2.png"]; //Player image object
player = [[Character alloc] initWithImageAndNameAndProjectile:playerImage andAnotherParam:#"Player"];
//Instantiate player object
player.image.position = ccp(0,210); //Set player image position
//Set player projectile image position
stringScore = [NSString stringWithFormat:#"%d", player->score]; //Score object
myScore = [CCLabelTTF labelWithString:stringScore fontName:#"Times New Roman" fontSize:20];
//CCLabel representation of score
myScore.position = ccp(20,300); //Set position of label score
myScore.color = ccc3(255,255,255); //Set color of label score
[self addChild:myScore]; //Add the score object
[self addChild:player.image]; //Add the player image object
[self callSpawnEnemy];
[self setUpMenus];
[self moveForward];
[self schedule:#selector(update:) interval:0.005]; //Schedule updating on 0.005 second intervals
}
+(void) resetAll{
[[CCDirector sharedDirector] replaceScene:[HelloWorldLayer scene]];
}
-(void) moveForward{ //Method to move the camera forward
[self runAction:[CCFollow actionWithTarget:player.image]];
}
+(CCScene*) scene{
CCScene *scene = [CCScene node];
HelloWorldLayer *layer = [HelloWorldLayer node];
[scene addChild: layer];
return scene;
}
-(void) dealloc{
[super dealloc];
}
-(void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
-(void) leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
AppController *app = (AppController*) [[UIApplication sharedApplication] delegate];
[[app navController] dismissModalViewControllerAnimated:YES];
}
#end
Any thoughts?
Jake
It seems that you called a class method(+) where you need to call the instance method(-). You just need to reset all the resources but not the whole object. And, as a class method, your +(void)resetAll method is meaningless for it has no return value.
I suggest, you may put all your resource-init codes into a single method, when you need to restart, just call [self removeAllChildrenWithCleanup:] and call that specified resource-init method.

inserting time delay with cocos2d

I am trying to add several labels that appear sequentially with a time delay between each. The labels will display either 0 or 1 and the value is calculated randomly. I am running the following code:
for (int i = 0; i < 6; i++) {
NSString *cowryString;
int prob = arc4random()%10;
if (prob > 4) {
count++;
cowryString = #"1";
}
else {
cowryString = #"0";
}
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2] ,[CCCallFuncND actionWithTarget:self selector:#selector(cowryAppearWithString:data:) data:cowryString], nil]];
}
the method that makes the labels appear is this:
-(void)cowryAppearWithString:(id)sender data:(NSString *)string {
CCLabelTTF *clabel = [CCLabelTTF labelWithString:string fontName:#"arial" fontSize:70];
CGSize screenSize = [[CCDirector sharedDirector] winSize];
clabel.position = ccp(200.0+([cowries count]*50),screenSize.height/2);
id fadeIn = [CCFadeIn actionWithDuration:0.5];
[clabel runAction:fadeIn];
[cowries addObject:clabel];
[self addChild:clabel];
}
The problem with this code is that all the labels appear at the same moment with the same delay. I understand that if i use [CCDelayTime actionWithDuration:0.2*i] the code will work. But the problem is that i might also need to iterate this entire for loop and have the labels appear again after they have appeared the first time. how is it possible to have actions appear with delay and the actions dont always follow the same order or iterations???
Maybe i did not really understand what you want to do. But if you need some control when your labels appear (to iterate something) make something like this:
-(void) callback
{
static int counter = 0;
//create your label and label action here
// iterate through your labels if required
counter++;
if (counter < 6)
{
double time = 0.2;
id delay = [CCDelayTime actionWithDuration: time];
id callbackAction = [CCCallFunc actionWithTarget: self selector: #selector(callback)];
id sequence = [CCSequence actions: delay, callbackAction, nil];
[self runAction: sequence];
}
else
{
//calculate the result and run callback again if required
//don't forget to write counter = 0; if you want to make a new throw
}
}
The problem is that your are scheduling all the actions to fire off at the same time.
Changing
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2] ,[CCCallFuncND actionWithTarget:self selector:#selector(cowryAppearWithString:data:) data:cowryString], nil]];
for
[self runAction:[CCSequence actions:[CCDelayTime actionWithDuration:0.2 * i] ,[CCCallFuncND actionWithTarget:self selector:#selector(cowryAppearWithString:data:) data:cowryString], nil]];
should fix your problem

Resources