Is it possible to detect which CCScene is currently showing on the scene? I have 2 CCScenes in my game and I want a certain action to occur if one is showing.
Also quick related question, if I wanted to check if a CCMenu is not showing currently would I do something like
if (!menu) {
//Menu is not showing currently
}
I am a bit of a noob when it comes to Cocos2D so please forgive me :)
Thanks!
You can use the CCDirector to tell which scene is running.
[[CCDirector sharedDirector] runningScene];
As for whether the menu is showing. You would have to check with the parent of the menu. If the parent where your CCLayer, then you could check by
// assume menu is set up to have tag kMenuTag
CCMenu * menu = [self getChildByTag:kMenuTag];
If the menu is child of some other node, you can get the parent through a similar method and get a reference to the menu.
If the menu == nil, it is not showing.
UPDATE
In cocos2d, you are discouraged from keeping references to all of your sprites, instead you should be giving each node a unique tag and use that to reference it. To achieve your first goal, you can give your scene a tag in your 2 respective CCLayer classes.
You can set up your unique tags in an enum in a file called Tags.h, then import that into any classes that need access to your tags
Example Tags.h
enum {
kScene1Tag = 0,
kScene2Tag = 1,
kMenuTag = 2};
Then in your layer class
+(id) scene
{
// 'scene' is an autorelease object.
CCScene *scene = [CCScene node];
scene.tag = kScene1Tag;
// 'layer' is an autorelease object.
HelloWorld *layer = [HelloWorld node];
// add layer as a child to scene
[scene addChild: layer];
// return the scene
return scene;
}
Now when you grab the current scene you can check against the tags
int currentSceneTag = [[CCDirector sharedDirector] runningScene].tag;
if (currentSceneTag == kScene1Tag) {
} else if (currentSceneTag == kScene2Tag) {
}
The tag property is from CCNode which is the base class of CCLayer, CCScene, CCSprite, CCMenu...
This how to find out which scene is running
if ([CCDirector sharedDirector].runningScene == yourScene1) {
// your scene 1 is showing
} else {
// your scene 2 is showing
}
and to find out if a node is child of the running scene
BOOL isShowing = NO;
CCNode *node = yourMenu;
while (node != nil) {
if (node == [CCDirector sharedDirector].runningScene) {
isShowing = YES;
break;
} else {
node = node.parent;
}
}
if (isShowing) {
// your menu is in the display hierarchy
}
Related
I need some help sending the player's score when they lost over to another scene but can't figure out how.
This is the code I have right now:
if(CGRectIntersectsRect(playerOne.boundingbox, object.boundingbox))
{
[self gameOver];
};
-(void)gameOver
{
[[CCDirector sharedDirector] replaceScene:[GameOver scene] withTransition:[CCTransistion transitionFadeWithDuration:1]
}
So basicly when these two's bounding box collide, the game is over and send you over to the gameOver scene.
How can I send over the score over to the Game Over scene too?
Thanks!
Add a property into the GameOver class named score. Then, before you replace the scene, set the score property to whatever the score is.
Example (note: this is untested so some types might be wrong as I've never used Cocos2d):
-(void)gameOver {
NSInteger score = /* get the score here */;
GameOver *scene = [GameOver scene];
[scene setScore:score];
[[CCDirector sharedDirector] replaceScene:scene withTransition:[CCTransistion transitionFadeWithDuration:1]];
}
Now, you can just fetch the score in the GameOver scene like so:
-(void)someMethod {
NSInteger score = [self score];
NSLog("Score: %d",score);
}
So I have my main view where I can do things such as this:
player = [Hero spriteNodeWithImageNamed:#"player.png"];
player.position = CGPointMake(512, 384);
[self addChild:player];
This is done in the GameScene, which is my main scene, its loaded in the view controller like so:
-(void) presentFirstScene
{ // create and present first scene
GameScene* myScene = [GameScene sceneWithSize:self.view.bounds.size];
[self.kkView presentScene:myScene];
}
Now If I have a different class called A (an instance of this class can be found in GameScene) how can I add SKSpriteNodes in A to the main GameScene.
What is easiest/most efficient way of doing this?
Thanks,
Lewis
You can create method in your class A which returns SKSpriteNodes, for example:
// in .m
-(SKSpriteNode*)getHero {
SKSpriteNode *hero = [SKSpriteNode spriteWithImageNamed:#"player.png"];
// Add more customisation here
return hero;
}
And in your GameScene you can call it like that:
SKSpriteNode *hero = [a getHero];
hero.position = CGPointMake(512, 384);
[self addChild: hero];
Remember to add class declaration to .h file:
-(SKSpriteNode*)getHero;
Hope this what you are after.
If A is generating nodes without GameScene explicitly requesting them, then you could use the delegate pattern. Have A call a method on its delegate when it has a new node to add.
I have a scene where the user chooses level in a game, then i want to switch to a game scene with the level the user chose. I want to create the game scene with the right level but to do this i need to pass an integer which is the level-number to the game scene so i know which level to load.
My question is, how do i accomplish this? I have looked at the userdata property of nodes but could not figure out how to use it to accomplish what i want. Any help would be deeply appreciated.
When you call [self.view presentScene:scene transition:transition] inside of your level selection scene, first set up your new scene with the level number. SO:
//GameScene.h
//This is the public interface for your game scene
#import SpriteKit;
#interface GameScene : SKScene
#property (nonatomic, assing)NSInteger levelNumber;
#end
And then
//Level Selection Scen
//Select.m
#import "GameScene.h"
... Rest of your code
User selects level and you prepare for transition and call
- (void)transitionToLevel:(NSInteger)level{
GameScene *destinationScene = [[GameScene alloc] initWithSize:self.size];
destinationScene.levelNumber = level;
SKTransition *transition = //Set up transition here
[self.view presentScene:destinationScene transition:transition];
}
And in your game scene's implementation
//GameScene.m
#import "GameScene.h"
#implementation GameScene
-(id)initWithSize:(CGSize){
if (self.levelNumber == 1){
[self playLevelOne]; //Where this is where you play your game
}
else if (self.levelNumber == 2){
[self playLevelTwo];
}
}
- (void)playLevelOne{
//Level Implementation
}
- (void)playLevelTwo{
//Level Implementation
}
#end
You would do this for each level you plan to support
// First Scene
NSUserDefaults.standardUserDefaults().setInteger(12345, forKey:"SCORE")
// Second Scene
if let myScore: AnyObject = NSUserDefaults.standardUserDefaults().objectForKey("SCORE") {
println(myScore)
finalScore = myScore as IntegerLiteralType
}
I have a sprite-kit game where I need to be checking often to see if the player has lost
- (void)update:(NSTimeInterval)currentTime
{
for (SKSpriteNode *sprite in self.alienArray ) {
if (sprite.position.y < 10) {
LostScene *lostScene = [[LostScene alloc] initWithSize: CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
NSLog(#"about to present");
[self.view presentScene:lostScene transition:[SKTransition fadeWithDuration:0.5]];
}
}
}
but when this method gets called (which I know is happening), no scene presents. What am I doing wrong? I believe it has something to do with the transition, because when I take it out, it works fine
You should add a property of the existing scene like: BOOL playerHasLost and edit your update method:
- (void)update:(NSTimeInterval)currentTime
{
if(!playerHasLost)
{
for (SKSpriteNode *sprite in self.alienArray )
{
if (sprite.position.y < 10)
{
playerHasLost = YES;
LostScene *lostScene = [[LostScene alloc] initWithSize: CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
NSLog(#"about to present");
[self.view presentScene:lostScene transition:[SKTransition fadeWithDuration:0.5]];
break;//to get out of for loop
}
}
}
}
So this way as soon as the sprite get to position that you treat as lost position it will set the variable to YES, present the scene and it will not do it again.
The reason is simply that the update method gets called every frame, with a running transition the scene will be presented anew every frame and thus it appears as if nithing is happening. You should see the NSLog spamming the log console.
Situation:
I'm getting some mysterious crashing shortly after a CCCallFunc. In short, we have a button. The button has a tag to identify it later. When the button is pressed, we run some actions to animate it, and when the animation is done, we CCCallFunc another method to transition to another scene. We crash shortly after the CCCallFunc. Source and errors below.
Point Of Crash (in cocos2d source):
// From CCActionInstant.m of cocos2d
-(void) execute
{
/*** EXC_BAD_ACCESS on line 287 of CCActionInstant.m ***/
[targetCallback_ performSelector:selector_];
}
#end
Snapshot of Thread 1:
My Code:
Below is some source taken from MenuLayer.m (a simple menu to display a button).
// from MenuLayer.m
// …
#implementation MenuLayer
-(id) init{
if((self=[super init])) {
/****** Create The Play Button (As a CCMenu) ********/
CCSprite *playSprite = [CCSprite spriteWithFile:#"playbutton.png"];
CCMenuItemSprite *playItem = [CCMenuItemSprite itemFromNormalSprite:playSprite selectedSprite:nil target:self selector:#selector(animateButton:)];
playItem.tag = 3001;
playItem.position = ccp(160.0f, 240.0f);
CCMenu *menu = [CCMenu menuWithItems:playItem, nil];
menu.position = ccp(0.0f, 0.0f);
[self addChild:menu z:0];
}
}
// ...
- (void)animateButton:(id)sender{
/*** Run an animation on the button and then call a function ***/
id a1 = [CCScaleTo actionWithDuration:0.05 scale:1.25];
id a2 = [CCScaleTo actionWithDuration:0.05 scale:1.0];
id aDone = [CCCallFunc actionWithTarget:self selector:#selector(animationDone:)];
[sender runAction:[CCSequence actions:a1,a2,aDone, nil]];
}
- (void)animationDone:(id)sender{
/*** Identify button by tag ***/
/*** Call appropriate method based on tag ***/
if([(CCNode*)sender tag] == 3001){
/*** crashes around here (see CCActionInstant.m) ***/
[self goGame:sender];
}
}
-(void)goGame:(id)sender{
/*** Switch to another scene ***/
CCScene *newScene = [CCScene node];
[newScene addChild:[StageSelectLayer node]];
if ([[CCDirector sharedDirector] runningScene]) {
[[CCDirector sharedDirector] replaceScene:newScene]];
}else {
[[CCDirector sharedDirector] runWithScene:newScene];
}
}
Use CCCallFuncN instead of CCCallFun.
CCCallFuncN passes the Node as parameter, the problem with CCCallFun is that you are loosing reference of the node.
I test your code with CCCallFuncN and works ok.
Just a hunch. Besides checking for memory leaks, try to schedule a selector with a 0 second interval instead of directly sending the goGame message. I have a suspicion that director's replaceScene causes a cleanup of the scene and all objects associated with it. That in turn could leave the CCCallFunc action in an undefined state. Although normally it works fine - which is to say that this is just another indication about something sketchy, memory- respectively object-lifetime-management-wise.
Btw, if you support iOS 4 as a minimum, use CCCallBlock instead of CCCallFunc. That's safer and cleaner.