I'm working on a game using xcode and I want to create a super item (like the star in Super Mario) If you touch this super item, you can pass all the obstacles without lose (avoid the game over). But really I don't have an good idea to do this.
I have my current gameover function in my game, If I touch the obstacle area, I die. Obstacles are UIView Objects. If the player touch any obstacle:
[self gameover];
I can try two things: First, I can make the image invisible, But If I use:
player.hidden = YES;
when the player touch some obstacle, the player dies (game over). Obviously is hidden but the object is still there. I need an option to set player invisible to all the obstacles in the game.
Second: I can avoid the gameover if the player is using an special item.
This is what I'm try to do:
if (CGRectIntersectsRect(player.frame, superitem.frame)) {
[avoid gameover];
I really don't know the correct function to do this. Hope to receive some help. Thanks
I finally found the correct code to do what I need. I want to share it, maybe someone need to do this and this code can help.
UIImageView* tempf = [coins];
if (tempf.tag == obstacles)
{
[coins removeObject:tempf];
[tempf removeFromSuperview];
}
Thanks everyone's for your help.
Related
I'm struggling with pause moment in my game.
I want to make button which perform pause logic and transition me to another scene with save action to sceneManager that holds gameScene.
Everything works fine but I can't make pause all actions my scene has. I read quite a lot of themes but still no luck.
The only think that really pauses any movement on my scene is
self.scene.view?.isPaused = true
But it pauses my transition as well.
I have spawning enemies, power-ups, shots and background stuff. So if I translate to other scene with this code:
transition.pausesOutgoingScene = true
transition.pausesIncomingScene = true
I see that objects and actions are not paused as well!
How to get pause logic work?
I added all nodes I want to pause to the same SKNode instance and after that I can just write:
someNodeWithSpritesToPause.isPaused = true
physicsWorld.speed = 0
This code solves my problem.
Thank you!
I'm messing around with Swift for the first time and have a square on the screen. The square starts by moving to the right. When I tap on it I want it to go up. I don't want it to continue to the right at all, I want it to just go straight up. Unfortunately the physics and gravity make it curve to the right some more before it goes up. What can I do to completely stop the gravity pull and acceleration of my object, before setting the gravity to the new value?
This turns the object in the right direction, but I need it to come to an instant stop before doing this.
if(self.gravity.gravityDirection.dx == 1){
self.gravity.gravityDirection = CGVectorMake(-1.0, 0.0)
}
The way to stop an item being affected by a behavior is to remove the item from the behavior or remove the behavior from the animator. For example, here you might remove self from the existing gravity behavior.
I used a CGRectIntersectsRect to detect the collision of a moving image (anvil) and an image (guy).
The "anvil" moves constantly in a CCRepeatForever and the "guy" moves in a ccTouchesBegan method by the control of the user.
Here it is, very simple:
if (CGRectIntersectsRect(guy.boundingBox, anvil1.boundingBox))
{
pancake = [CCSprite spriteWithFile:#"pancake.png"];
pancake.position = ccp(200,200);
[self addChild:pancake];
}
The problem is that the above lines of code do not work in the if((self=[super init])) and only in the ccTouchesBegan, where I have all of the actions for "guy." I do not want to have the image "pancake" appear only if the user taps exactly when the two images collide...if the images EVER collide, regardless of touch events, "pancake" should appear.
Any advice? Thanks in advance!
Not to criticize but your code doesn't make a lot of sense. Why would you be testing for interestion in an init method for two objects that are constantly in motion throughout your scene? Even if you wanted to show the pancake whenever the two intersected, why don't you load the pancake and make it invisible, then switch it to visible whenever the two objects intersect. This could be done simply by putting the condition in an update method and setting pancake's visible property to true if they intersect or false otherwise.
But to answer the question of "why" that code doesn't work in init is simply because at the point of calling this either one or both of your objects doesn't exist, they simply don't intersect, or their bounding box properties have not been set. I bet they simply don't intersect yet. But that doesn't matter much since there is no reason I can see to check for interestion in an init method. Schedule the update method, create the update method, and place this condition inside of it but rather than create the pancake sprite, you'd be setting the already existing pancake sprite's visibility to TRUE.
We are working on a game in cocos2d in which there is a possibility of getting a trivia question. The trivia question is implemented as a new, transparent CCLayer on top of the gameboard, which contains a CCMenu with all of the questions.
Our problem is that we can't seem to get the touches to stop propagating properly. When the trivia menu is up, players should not be able to click on the "roll dice" button on the board layer.
We have tried implementing this by calling dice.isTouchEnabled=NO; right before adding the trivia layer, but we can't figure out how to re-enable the dice button.
We also tried changing ccTouchBegan from NO to YES to always consume all of the touches, but then it stops responding to our menu. It seems that this should be the right way to do it, but why did the menu stop responding then?
Our professor suggested implementing a callback function, which we of course can do, but it seems like it should be easier than that.
Does anyone have any suggestions?
I understand that there are two ways to do this.
Method 1 (method that I am using)
Before trivia question pops up, use the function below to disable menus on the Underlying Scene node. The method is a recursive method so it disables all menus on the node's children too.
When the trivia question is dismissed, send an NSNotification which will be received by the Underlying Scene node and will re-enable menus on the node and its children. You can use the block method of NSNotification to shorten your code.
Docs on addObserverForName:object:queue:usingBlock:
(void) MenuStatus:(BOOL)_enable Node:(id)_node {
for (id result in ((CCNode *)_node).children) {
if ([result isKindOfClass:[CCMenu class]]) {
for (id result1 in ((CCMenu *)result).children) {
if ([result1 isKindOfClass:[CCMenuItem class]]) {
((CCMenuItem *)result1).isEnabled = _enable;
}
}
}
else
[self MenuStatus:_enable Node:result];
}
}
Method 2
Create an invisible layer that will swallow all touches below the Trivia Question layer. Here is a class you can try: https://gist.github.com/christophercotton/1563708
I've found a peculiarity with Cocos2D and I cannot seem to fix it. From the AppDelegate I load into a Menu, which is a CCScene. The scene holds a CCLayer, which itself holds the CCMenu. Everything works find the first time through. After my game has ended, I bring the user to a GameOverScene and prompt them to return to the menu. I am reloading the menu scene and calling [[CCDirector sharedDirector] replaceScene:menu]. After entering this menu, though, not all input functions. I can no longer tap on menu items, but I can pan / multi-touch on them to trigger the item. What gives?
Is there some way on initialization to reset the CCScene to receive input, and if so, will this mess up CCMenu's input receiving?
Did you override any of the on* methods like onEnter, onExit, etc. in any of your classes?
If so, you must call the super implementation (ie [super onEnter]) in each, otherwise some cocos2d functionality like scheduling or input may stop working.
I finally figured it out, and the answer was right under my nose. Earlier in my game development I needed a way to stop KKInput from swallowing gestures. I'm not entirely sure of the ramifications of this action, but I was able to do so like this:
KKInput* input = [KKInput sharedInput];
UITapGestureRecognizer* tapGestureRecognizer;
tapGestureRecognizer = input.tapGestureRecognizer;
tapGestureRecognizer.cancelsTouchesInView = NO
It seems that the default Kobold2D behavior is to swallow all touches, which was preventing the CCMenu from receiving any tap gesture.