Control Sprite in another scene from HUD layer - iphone

I have a sprite in scene1.m and I have a button in HUD layer hud.m. Now a button appears in Scene1.
How can I get control of scene1 sprite from HUD layer. Any ideas?

With cocos2d, you can assign a tag to your sprite (or button, or any class derived from CCNode), and to each of its hierarchy of parents (see CCNode class, setTag() ). You can then use these tags to retrieve the corresponding nodes from anywhere in your code, with CCNode getChildByTag().

Related

ARKit – How to load a GIF animation in ARSession?

I am working on an application using ARKit and SceneKit frameworks. I am able to render my 3D object, create overlay etc. I want to load a GIF animation like a overlay in ARSession on tap of one of my child nodes. Is this possible?
I tried adding SKScene as a overlay to my SCNScene like below, and it looks good.
informationOverlay = InformationOverlayScene(size: self.sceneView.frame.size)
self.sceneView.overlaySKScene = informationOverlay
But how to load this GIF file in SKScene or SCNNode?
The following SO question answers how to display an animation inside an SKNode:
Simple Gif like animation with Spritekit
You could use projectPoint to project the location of the SCNNode (of your 3D model) to a 2D location on screen, and then use that for the location of the SKNode containing the animation. That way the animation will always face the screen.
Alternatively, you could assign a Core Animation layer containing the animation to the .diffuse.contents property of a SCNMaterial and assign that to a SCNPlane which you can add to the scene and position above the 3D model. If you want that plane to face the camera, simply assign the .pointOfView.orientantion property of your SCNView to the SCNNode that holds the SCNPlane.

How to place sprite kit elements overtop of regular UIKit elements

I'm trying to geive the user the ability to drag a skspritenode over a UIImageView and UIButton, but whenever I create the scene it appears behind the UI elements. How do I fix this?
I am afraid that is just not possible. All the sprites and any other elements are rendered frame by frame on the SKScene node and cannot be "dragged" or placed anywhere else. These are not UIKit elements.
In this case, your view is acting as the SKScene and hence all the sprites are being rendered onto that.
SKView is a UIView/NSView in itself. All the Sprite Kit nodes (the scene is also a node) are constituents of the SKView and contained within it. You can only change the draw order of views but not their contents.
To give an example: what you're trying to do is the equivalent of trying to place a UIImageView on top of a UIButton's background but below the UIButton's label.
While technically you could achieve this for plain UIView elements with some trickery, the same will never work for Sprite Kit nodes. They aren't views to begin with but drawn onto an OpenGL framebuffer represented/managed by the SKView. From the perspective of Cocoa an SKView is a single view with no subviews in it.
Simply put: nodes are not views, and nodes are technically incompatible to views.
The UIImageView and its UIButton and whatever other content you want in there must be in a UIView that's sitting lower than the SKView you have the Sprite Kit contents in.
And you'll need to ensure the SKView is transparent (at least its background) and that it passes touches through to the UIView underneath if you need them down there sometimes.

Cocos2d CCFollow when there are multiple layers

Essentially, when I have one CCLayer for background and platform sprites and another for player sprite and HUD, how should I go about calling CCFollow if I want my camera to always center at the player with the HUD in the same layer moving accordingly as well?
Right now the background layer is a subclass of CCLayer that has a CCSpriteBatchNode for drawing objects in the environment. It contains an instance variable CCLayer *HudLayer that has a CCMenu and another CCSpriteBatchNode for the player animation sprites. They are both direct children of a CCScene, meaning HudLayer is not a children of BackgroundLayer. I'm now calling the CCFollow as [self runAction:[CCFollow actionWithTarget:playerSprite worldBoundary:worldRect]] where self is the BackgroundLayer.
The camera follows the box2d physics body of the player correctly, and the menu moves with it too, but whenever the camera moves, let's say to the right, the player sprite moves twice as fast to the right, leaving its physics body behind. So after moving to the right for a bit the playerSprite is outside of screen. playerSprite.pos and playerSprite.position seem to have nothing wrong though.
I've tried calling the CCFollow in different places but either the HUD refuses to move or the camera won't move or the player moves weirdly. What's wrong with it?
I FIGURED IT OUT! The key here is to put the player sprite in some layer other than the HUD layer. I added my CCSpriteBatchNode for the player sprites as a child of backgroundLayer instead of the HudLayer without changing anything else and it worked.
So basically everything that needs to always stay on screen as the HUD should be in one layer, while the target node for CCFollow should be in a separate layer (in the background in my case). The caller layer of CCFollow should contain the target node as a direct child. This tutorial helped me a great deal: http://www.raywenderlich.com/4666/how-to-create-a-hud-layer-with-cocos2d

Cocos2D iPhone - adding a CCLayer on top of another

I am newbie to Cocos2d. I have this layer that represents my main game scene. Lets talk in terms of Cocos2D default template. In this case, my main game scene would be HelloWorldLayer.
Now I want to present a menu. I have create the menu as an individual subclass of CClayer.
How do I make the menu appear using some kind of transition on top of the main scene?
If I use something like
CCScene *menu = [Menu scene];
[[CCDirector sharedDirector] replaceScene:
[CCTransitionCrossFade transitionWithDuration:0.5f scene:menu]];
I would be using the menu as a scene and replacing the main scene with it. This is not what I want. I want to make the menu appear on top of the main scene, using some kind of transition and if possible fading the main scene to 50% or whatever.
How do I do that?
thanks.
Instead of replacing the scene, simply make your Menu class a CCLayer and add it existing scene. You can set opacity and add masking sprites as needed to fade out the bottom layer if you need to.
CCLayer* newLayer = [Menu layer];
[self addChild: newLayer];
You will need to handle much of the details on which layer captures input, but this is the basic idea.

Is it possible to add a CCLabel to a CCSprite in a CCSpriteBatchNode?

I have a CCSpriteBatchNode that is added to my Scene.
I have a CCSprite that is created with "initWithSpriteFrameName," then I add it to my batch node.
The sprite displays correctly. However, after that I add a CCLabelTTF as a child to the CCSprite, and the app crashes with the following error:
"CCSprite is not using the same texture id"
Is there any way to add the Label as a child node of the sprite?
You can add the label only if you don't add the CCSprite to a CCSpriteBatchNode. CCSpriteBatchNode only allows CCSprite as children, and that also extends to children of children.
If that's not an option, simply add the label to the scene hierarchy elsewhere and then update the position of the label to the sprite's position using an update method, in order to synchronize the label's and sprite's positions.