Understanding cocos2d scenes, how they works really? - replace

I'm stuck on something about the scenes creation and replacement in cocos2d, so I'm going to ask precisely what seems to be misunderstood by me. I have a game (fully working except for scene swapping, sadly) with some little-games, now, if I had to do this starting with a cocos2d scene as menu I wouldn't have any problem, but since I did it starting with UIKit I truly need to know better how the scenes are working to fix it.
Firstly, is it required to start a scene in the appDelegate? since I'm starting with UIkit and the scene must be shown after you choice the game (say, out of 3 choices), which scene should I put in the appDelegate? and where exactly? I'm putting the scene in this method:
-(void) directorDidReshapeProjection:(CCDirector*)director
{
if(director.runningScene == nil)
//start scene
}
If I put the FIRST scene, the UIKit part works good and when I start the "game number TWO" as first choice (say we play this game for first) I got the Open GL 0x0506 error, then the scene start.
If I put the first scene, I choice the first game, and then quit and choice the second game, the scene is replaced properly without that error.
If I put the first scene, and I start the "game number 1" it works (obviously) because he has the scene loaded, but I cannot know which game will start as first the user.
I tried with an "intro scene" loaded at the appDelegate but I got the same problem. the problem basically is "how to start scene if you have more than one scene and don't know which will be called as first"...

The 'getting started with iOS' documentation will really clear up a lot of these questions. You can find it at developer.apple.com - https://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/Introduction/Introduction.html#//apple_ref/doc/uid/TP40007072
It explains just what an AppDelegate actually is, as well as how to use one properly. It is not immediately clear how to mix UIKit and cocos2d, but the above link cleared a lot up for me. Another very helpful resource is a tutorial by Ray Wenderlich -
http://www.raywenderlich.com/4817/how-to-integrate-cocos2d-and-uikit
From a bird's eye view, the CCDirector inherits from a UIWindow. Mixing UIKit and cocos2d is as simple as building your interface with UIKit, then at some point opening a UIWindow and allowing the CCDirector to start cocos2d. In a sense, the components act as almost two entirely separate entities.

Related

Cocos2D-X CCTransition shows background between the 2 scenes

I've been trying to successfully create a slide transition between 2 scenes for the past 2 hours and all I can find are just some answers in Objective-C and I have no idea how to translate them.
I'm using XCode 5 with Cocos2D-X 2.2.3 to develop for iOS.
CCTransitionSlideInT *trans = CCTransitionSlideInT::create(1.0, Scene2::scene());
CCDirector::sharedDirector()->replaceScene(trans);
Doing that makes my current scene slide down and gets replaced by black (background) THEN after the transition is done, the second scene appears without any transition effects; it just appears.
How can I slide the first scene with the second one attached to it?
I've seen that it's done in TestCPP. I tried tracing it but it got too complicated for me. All I really need is some clarification!
Thanks in advance! And I hope I provided all the necessary info.

Achieve Infinite Scrolling for a platformer game using cocos2d language objective c

I am trying to develop an 2D game using cocos2d library. I am still learning the framework.
Please understand that I am new to game development but not new to programming using objective c.
Here is the issue I am facing when it comes to my game development effort - I feel that I am missing the theoretical understanding of how to develop an infinite scrolling game. Is it possible for any of you to provide me some guidance on that ?
Here is my understanding of achieving infinite scrolling using cocos2d framework:
Cocos2d has a singleton director class which handles the current scene and scene transitions
In the current scene, I feel like I have to create an platform object consisting of several images and add them as a child to the current layer. And constantly run a move action to the platform sprite. So as and when I detect a particular image is off screen I have to replace it with another image. That way I will be able to create an infinite scrolling.
I am sorry if point 2 is not coherent. I just attempted to put my understanding of how to infinite scrolling.
Can you please help me with this ?
Thanks
I dissected how to implement scrolling with cocos2d-iphone in this article. What you probably want is the "fake scrolling" approach where two background images are moved and switch position after one completely left the screen.
You want to do this for the background layer only, not individual sprites. Your world isn't really moving, it's just the background panning that creates the illusion of movement. All sprites etc (player, enemies) movement is still relative to screen coordinates.
You'll find a working implementation in the code for my Learn Cocos2D 2 book in the Shoot'em Up project.
If you don't want to bother implementing this yourself, KoboldTouch supports endless/infinite scrolling for tilemaps. Here the game objects actually move along with the background infinitely (up to the maximum coordinates supported by float which is around +/- 16 million points).

Cocos2d change image of hero in accelerometer when an object hits it

Hi. I am new on this website and also in cocos2d. I am a student and I need your help.
I am making a game based on one of the tutorials in a cocos2d game development book. The concept is simple; different objects are falling from the top of the screen and I have to avoid or catch them by tilting the device. The main character, which is one which has to avoid objects, has different properties which can change by grabbing different objects (e.g. the player may have a shield if it grabs one). In order to display the shield I have to change the sprite of the player. I am not sure how I can achieve this. Could anyone help me in providing some guidelines on this?
Use setTexture to switch the image (texture) of your current sprite with another:
[playerSprite setTexture:[[CCTextureCache sharedTextureCache] addImage:#"playerWithShield.png"]];

Help Logically setting up Layers for Good Game Design (involving SpaceManager Physics, Music, and Game Logic)

Im in the middle of creating my first iPhone game - I have a background in OOP and in particular C++ so I had some questions with regards to how best to logically setup layers while maintaining functionality.
Currently I WANT for my game to have three main layers:
HUDLayer (All static objects on the screen are here - game controls, User score, pause button etc.)
PlayLayer (The Player, the main game loop and all of the game logic here)
Level Layer (The level images and the level physics objects that the player interacts with, and the background music specific to this level)
Notice I used the word WANT here - because for the life of me im constantly having to move logical objects around just to work within what appears to be Cocos2d's and spacemanagers structure.
Below are just some of the issues that I'm facing
I would like for my PlayLayer to be the scene thats loaded by the director - but if I do that then all of the HUDLayer objects get covered behind the PlayLayer, and dont stay in place like they should, hence the HUDLayer is my scene and I have had to do that just to make it work
I would like to play the background music (via simpleAudioEngine playBackgroundMusic) in the LEVEL layer because I want different levels to have different music. So far the ONLY way I have gotten background music to work is to have it in the TOP most layer i.e. in this case the HUDLayer
Because of the fact that I have to use an instance of the SpaceManagerCocos2d object to create physics bodies - it seems like my level layer has to be killed and just incorporated within my PlayLayer otherwise im having a nightmare of a time attempting to detect collisions between the player and the level.
Am I missing something very obvious here? is there a core understanding of layers that Im just not getting? More and more I feel like im being pushed by the framework to build the whole game inside of a single class and just to use layers as scenes.
Is anyone else having these problems? Am I approaching the architecture of the game wrong? Any help would really be appreciated.
Thanks in advance guys!
Well, each game is different. There are many good discussions on the cocos2d forums about architecture, some people prefer to use an MVC approach, some like using an Actor metaphor to contain physics objects, etc.
Here's my approach:
Use two CCLayer objects (GameLayer and HUDLayer) as child nodes of one CCScene (GameScene). These are the "view" objects.
Create a GameController singleton that can make changes to the game state (also stored in GameController, or in a separate file.) You could also subclass CCScene and call that your controller.
GameLayer is in charge of rendering the graphics of the game level and all actors in it; it also handles getting game input via touch events.
HUDLayer is placed at a higher z-index than the GameLayer and obviously has all of the CCSprite objects for the HUD and buttons.
Interaction between the HUDLayer and the GameLayer is managed via the GameController.
GameController does all of the state changing and game actions.
That's just my approach because it worked for my current game, and it by no means is the definitive answer.
I'd suggest that you look into using an Actor paradigm for your physics objects -- SpaceManager does a decent job, but you don't necessarily always want physics objects to extend CCSprite.

What is the best way to set up menus with cocos2d?

I'm new to development and I have played around with a few tutorials. I wonder what the best way to set up a menu for a game with cocos2d?
I want a MainMenu with a Startbutton, SettingsButton, HighScoresButton and a little info/creditsButton in the corner.
How should I set this up?
Should I have the MainMenu as a Scene and the others as layers or just make all of them as separate Scenes?
The buttons that I add, should they be a plain button and then I add the textLabel on top or should I make them complete with textLabel?
I would like the buttons to "wiggle" like it is made of jello when i tap them, how do I do that?
As I said, I am new to this but I wanna learn as much as I can before school starts. I'm currently taking a summer class in iphone development so I get a head start for the next semester. I would like to see experienced game developers to help me out with this since i want to work with that when I get older, if they could also show me how to structure a game and the design.
Thank you so much.
David H
You've got several questions buried in there, so I'll address them in order...
First, to save on memory, you should break sections of your game, including menus, into separate scenes and switch between them using:
[[CCDirector sharedDirector] replaceScene: yourScene];
Alternatively you can use pushScene: and popScene, but these hold pushed scenes in memory and can be very costly depending on what you've got in the scene.
As for how to design your buttons, that's completely up to you and what fits best with the game. Some games look fine with text buttons. Others need a more stylized button that is best created with images. Remember, a CCMenuItem (button) is just a CCNode, so you can layer images and text in almost any way you wish.
Animating buttons is going to be a manual thing. I suggest subclassing the CCMenuItem, or CCMenuItemImage and overriding the selected and unselected methods to animate the underlying images.
We used cocos2d for our game The Selfish Birdbreeder. You can find the game and source here and dig around. I'm pretty certain we have a main menu.
http://pyweek.org/e/BirdBreeder/