How to make a single sprite untouchble in cocos2d? - ios

I am developing a game with cocos2d. Naturally, I have a menu button.If it's clicked , there will be "void" called , where I stop all current actions with [[CCDirector sharedDirector] pause]; and introduce a menu.
Also, I have sprites that are stopped and have instructions if they are touched (just some MoveTo actions). When I click on sprites during pause there appears a mistake(as I think, because of that instruction).
So, I assume, that sprites should be untouchable during game pause. How can I make a single sprite untouchable? Are there better ways to avoid such a mistake?

Once you use [[CCDirector sharedDirector] pause]; then your complete cocos2d is paused. So if you have written some cocos2d actions inside the touch callback or written something to do like move sprite etc on touch then you will encounter a crash. Instead try to stop all actions and schedulers on the scene and proceed further. You can stop all actions and schedulers as below:
[self pauseSchedulerAndActions];//This will pause all the scheduler and actions running on current scene. Note this will not unschedule the update().
To stop the update method as well do-
[self unscheduleUpdate]; //This will stop the cocos2d update method
Once you paused this way now you can perform any action on the sprite and once done then you can resume as below:
[self resumeSchedulerAndActions];
[self scheduleUpdate];

Related

determine currently in contact using SprikeKit Contact Delegate

Here is a code snippet I'm using in my didBeginContact delegate method:
- (void)didBeginContact:(SKPhysicsContact *)contact
{
for (Lasers *laser in self.lasers.spawnArray) {
if (contact.bodyB == self.player.physicsBody && [contact.bodyA.node.name isEqual:laser.sprite.name]) {
if ([self.lasers isDoneSpawning]) {
if (!self.player1.isSuperPlayer) {
[self playerDied];
}
}
}
}
}
So, there are red laser beams that will kill the player if the player runs into them. If the player current has the superPlayer power-up activate then the player can run right through these red beams.
The code above works perfectly to achieve this, but when the player has the power-up active and stands right in the middle of the red beam and then the power-up is deactivated then the player will not die. The reason is that this method is only run when the initial contact between the two sprites begins.
I don't think there is a delegate method to determine if the sprites are currently in contact. Does anyone have a solution for this?
Set the contactTestBitMask to not contact the laser at the start of the power-up and then set it back when the power-up ends. That will generate a didEndContact and then a didBeginContact, allowing the appropriate code to come into play.
This is more of a hack but should achieve proper functionality...
Immediately after setting your Super power-up to end, Remove and re-add your laser beams.
This will signal a new didBeginContactEvent and should kill your player.

How to effectively pause a game in spritekit?

This seems like such a basic concept but there is really no good answer.
I currently have a pretty basic setup, in my game's scene, I have an enum gameState which is currently either inGame or gamePaused.
I have my button set up, and I have it so pressing simply toggles gameState.
Then, to make it work, I have two separate update methods: One for when gamePaused, one for when inGame.
This works for 95% of my game, as the updates in my game can be pretty readily started and stopped without any issues. There are some issues I cannot tackle though.
First and foremost are actions. I use SKActions for a bit of my project (some movement, scaling, fading...) and this method does NOT pause them. This can be catastrophic in certain points. Secondly, this doesn't handle particles, physics, and a few other things that are not directly associated to my update methods.
The only clue I have is self.view.paused = YES, but that isn't going to work either.
First off, this DOES fix my actions, particles, and physics problem... kinda. But This question suggests that SKActions aren't actually PAUSED by this, but actually STOPPED completely. Is this true? If it is, then it won't work smoothly either because my actions are thrown out of whack.
Also, pausing the view seems to do what it says: stop everything. EVERYTHING. Newbie question here, but how am I supposed to get any code to run at that point? It's all cut off. Is this where I need subViews? I have never used a subView yet, but it sounds like its what I want in this case.
Simply put, there are a lot of unanswered questions for me, and I don't know how to proceed. I'm hoping there's some 'standard procedure' for pausing in Sprite Kit, but with pausing the view halting actions I truly have no idea where to proceed.
Should I move away from using actions? Is my idea of a pause subView sound?
I don't want to babble, all I want to know is how you go about pausing in your average Sprite Kit project. Additional info provided upon request.
Pausing a node will pause (not stop) the node's actions, and i suppose pause will also be applied to all children of the paused node in the same way. All other unpaused nodes will continue to run their actions to completion.
You should have a "game layer" node with all game nodes in them that you want to pause, and then just pause that "game layer" node. Then have a "pause game menu" node where you add all the pause menu stuff you need which will continue to function normally even if the game layer is paused.
Be very sure you do not use performSelector or NSTimer or GCD or any other scheduling methods besides actions or the scene's update: method because those will not adhere to a node's paused state.
Pausing the view effectively freezes everything in it, pausing the scene according to some will not pause the update: calls - but I have not verified this myself. If this were the case you can always check inside the update method whether a given node that you send a message to is paused and if so, decide not to send certain messages to that node.
I just called a map function on all children of the scene and set the paused property to true
self.children.map{($0 as SKNode).paused = false}
By setting it to true you can easily undo the freeze.
EDIT:
Better use a for-loop to iterate over the children.
for node in self.children as [SKNode] {
node.paused = false
}
I can tell you what I did in my game:
Pause the SKView on -applicationWillResignActive: (or equivalent, using NSNotifications),
Un-pause the SKView on -applicationDidBecomeActive: (or equivalent, using NSNotifications),
For the actual game scene only, I set a boolean flag _isPaused when resigning active, and if it is true I just skip the -update: method (frame updates). (I also show the "paused" menu when the user resumes the app).
...But my game is a simple puzzle game, and there's no long actions that should continue past the pause/resume. I can not confirm right now that all actions are aborted, but I think it's not the case.
I also used self.children.map{($0 as SKNode).paused = false} and it does work if you create a var layer of type SKSpriteNode and add it on top of the scene. But I also have an NSTimer I'm using to spawn sprites on the scene. Everything currently on the scene is paused, but sprites keep appearing and moving across the screen. It is not pausing the spawning.
I tried pausing the NSTimer when I call the above code by setting repeats to false, then setting it to true when I remove the layer but it doesn't work. I also tried self.scene?.view?.paused = true but that freezes everything and the layer I create does not even appear onscreen.
You need to set the delegate for the View!
func PauseGame(){
self.scene.view.paused = true
}
fun playGame(){
self.scene.pause = false
}
This function is work perfectly if you are not using the NSTimer to call a function. If you use NSTimer your view will pause perfectly but when you play the game again by play game functionality you see that all the functions that use the NSTimer function that runs in the back end and your screen full with your sprite-kits. So when you use NSTimer in the function you have to first pause the NSTimer functions also after that this function work perfectly.
//For Pause
func pauseGame() {
scene?.view?.paused = true
}
//For play.
func playGame() {
scene?.view?.paused = false
}

How to restart game play ( layer ) in cocos2d

I have created one level in cocos2d and i have a pause button on screen . clicking on pause screen it will open Menu ( Resume, Restart, Settings) . i want that when i click on restart menu my level will start from start. what i have tried i have remove that layer class and called it again but it didnt work. i tried to replaceScene . it didnt work either. how can i achieve that?
I have tried this and it works.
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] pushScene:[HelloWorldLayer node]];
But is it write that i am calling pushScene over and over.? will it effect my game. coz till now i am now removing that HelloWorldLayer that i want to restart again.
You have to use replaceScene. With pushScene the app will eventually run out of memory as previous scenes are not deallocating.
Be sure to create a new scene (as in your code sample), do not try to call replaceScene with the already running scene, that will fail.
You should also verify that your scene deallocates after replaceScene. Set a breakpoint in the dealloc method. If it does not deallocate, this means the scene is leaking and that can lead to all kinds of strange issues.

touch/drag rapid calls

I'm creating a shooting game where you touch the screen and the player shoots. The problem I have is that when you touch the screen and keep it down dragging it around it shoots rapid fire. What is the best way to deal with this?
I want the player to be able to hold the finger down and shoot at a steady pace, and on finger up stop shooting.
I was thinking of just using a Timer but I don't think that's very efficient... Any other ideas?
Thanks
I would really like to see more details on how you are implementing the shooting, but let me provide you with a way to accomplish this:
In cocos2d, you can use the CCScheduler to schedule a method call, instead of using timers. And this is very efficient. You don't have to worry about performance.
In the scheduled method, you would check whether the user is still touching the screen, and decide to shoot or not accordingly.
Here is some code to get you started:
NOTE: Am using cocos2d v1.0.1 .. in cocos2d v2.0, I think the CCScheduler was moved to the CCDirector.
- (void)init {
// ...
[[CCScheduler sharedScheduler] scheduleSelector:#selector(shoot:) forTarget:self interval:0.2f paused:NO];
// ...
}
- (void)shoot:(ccTime)dt {
if (userIsTouching) {
[player shoot];
}
}

How to wait until the CCMenuItemImage is press in objective C?

I want to pause the program until my button is press
I have pause my scene by Use [[CCDirector sharedDirector] pause]; sure that the screen is pause but my function is continue running until it end function
Do they have any function to wait for the press button action??
If you pause the CCDirector, it will drop the framerate to 4 fps - it does not stop the CCDirector from running game updates, nor will it halt the execution of the current method.
Note that the low framerate will affect touch detection and you may find it hard to activate any CCMenu button while CCDirector is paused.
These are just two reasons why CCDirector's pause method is a poor substitute for a real implementation of pausing your game. In principle, when you bring up the ingame (pause) menu, what should happen is that any game play object is simply not running any updates and pauses any currently running actions. Only the foreground menu remains running normally to receive user input.

Resources