CCNode Hud, not acting as Hud but "scrolling with scene" - ios

Attempting to simply have a scene where I have the gameplay, and the hud. Since I have it positioning the scene (which contains both layers), I assume this is why my Menu button is moving even though it's in the Hud Layer.
To fix it I would think that I would maybe have to change this line
[self setCenterOfScreen: mainChar.position];
to something like this:
[gameLayer setCenterOfScreen: mainChar.position];
But then I get this:
No visible #interface for CCNode declares the selector
'setCenterOfScreen:'
Here is the
GameScene (commented where I think issue is):
+ (GameScene *)scene
{
return [[self alloc] init];
}
// -----------------------------------------------------------------------
- (id)init
{
self = [super init];
if (!self) return(nil);
CGSize winSize = [CCDirector sharedDirector].viewSize;
self.userInteractionEnabled = YES;
self.theMap = [CCTiledMap tiledMapWithFile:#"AftermathRpg.tmx"];
self.contentSize = theMap.contentSize;
CCNode *gameLayer = [CCNode node];
gameLayer.userInteractionEnabled = YES;
gameLayer.contentSize = self.contentSize;
[self addChild:gameLayer];
[gameLayer addChild:theMap z:-1];
self.mainChar = [CCSprite spriteWithImageNamed:#"mainChar.png"];
mainChar.position = ccp(200,300);
[gameLayer addChild:mainChar];
// POSSIBLY WHY BOTH LAYERS ARE SHIFTING, RATHER THEN HUD STANDING STILL,
// I essentially want the gameLayer to be centered on screen, not both.
// I tried [gameLayer setCenterOfScreen: mainChar.position] but got error posted above
[self setCenterOfScreen: mainChar.position];
CCNode *hudLayer = [CCNode node];
hudLayer.userInteractionEnabled = YES;
hudLayer.position = ccp(winSize.width * 0.9, winSize.height * 0.9);
[self addChild:hudLayer];
CCButton *backButton = [CCButton buttonWithTitle:#"[ Menu ]" fontName:#"Verdana-Bold" fontSize:18.0f];
backButton.positionType = CCPositionTypeNormalized;
backButton.position = ccp(0.85f, 0.95f); // Top Right of screen
[backButton setTarget:self selector:#selector(onBackClicked:)];
[hudLayer addChild:backButton];
return self;
}
but then I just get:
Just confused on how I would set up a scene, to initially have 2 layers - one that keeps position in say the top right as a HUD, and the other that scrolls with scene for gameplay when telling it to.

This is related to the other question you asked here (Adding a Hud Layer to Scene Cocos2d-3) which I posted the answer to. In the future it is better to modify your original OP in your first question rather than create a new question that is almost the same.
Hope this helped.

Related

Anti-gravity physics in SceneKit

I am trying to make a simple app where if you tap the screen a box is switched from floating to affected by gravity. I can't seem to find a way to make the box float in the air though.
This code here takes care of half the problem:
boxNode.physicsBody = [SCNPhysicsBody dynamicBody];
This causes the box to drop out of the air and hit a floor I created. Is there anything in SCNPhysicsBody that would do the opposite of this? Say, perhaps, cause objects to float or just sail off toward a ceiling?
Also, I've written this code:
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
if (myBool == false) {
myBool = true;
NSLog(#"true");
} else {
myBool = false;
NSLog(#"false");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// touch recognizer
UITapGestureRecognizer *screenTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
[self.view addGestureRecognizer:screenTap];
// create box
SCNBox *myBox = [SCNBox boxWithWidth:1.0 height:1.0 length:1.0 chamferRadius:0.1];
SCNNode *boxNode = [SCNNode nodeWithGeometry:myBox];
boxNode.position = SCNVector3Make(0.0, 0.0, 4.0);
[myScene.rootNode addChildNode:boxNode];
while (myBool == true) {
boxNode.physicsBody = [SCNPhysicsBody dynamicBody];
}
}
I'm not sure why the while loop doesn't work though. I was thinking it would detect that myBool has been changed and alter the physics of the boxNode, but it doesn't.
The viewDidLoad method is only called once when the view is loaded. If your app is initialised with myBool = false, then the while loop will never be run. However in this case if myBool was true the while loop would never stop executing, preventing the view from loading, and therefore preventing the user from tapping the view to trigger your gesture recogniser.
I haven't tested the below, but it should at least give you a starting point. The scene is created in the viewDidLoad as per your code, importantly the scene's physicsWorld has its gravity set to zero (this is -9.8 by default). Later on when the user taps the view we reset the gravity to its default value, this should cause the box to fall.
The header file GameViewController.h
#import <UIKit/UIKit.h>
#import <SceneKit/SceneKit.h>
#interface GameViewController : UIViewController {
SCNScene *myScene;
}
#end
and GameViewController.m
#import "GameViewController.h"
#implementation GameViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// create a new scene
myScene = [SCNScene scene];
// create box
SCNBox *myBox = [SCNBox boxWithWidth:1.0 height:1.0 length:1.0 chamferRadius:0.1];
SCNNode *boxNode = [SCNNode nodeWithGeometry:myBox];
boxNode.position = SCNVector3Make(0.0, 0.0, 4.0);
[myScene.rootNode addChildNode:boxNode];
boxNode.physicsBody = [SCNPhysicsBody dynamicBody];
//'disable' scene gravity
myScene.physicsWorld.gravity = SCNVector3Make(0, 0, 0);
SCNView *scnView = (SCNView *)self.view;
scnView.scene = myScene;
scnView.allowsCameraControl = YES;
scnView.autoenablesDefaultLighting = YES;
scnView.backgroundColor = [UIColor blackColor];
// add a tap gesture recognizer
UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleSingleTap:)];
NSMutableArray *gestureRecognizers = [NSMutableArray array];
[gestureRecognizers addObject:tapGesture];
[gestureRecognizers addObjectsFromArray:scnView.gestureRecognizers];
scnView.gestureRecognizers = gestureRecognizers;
}
- (void)handleSingleTap:(UITapGestureRecognizer *)recognizer {
myScene.physicsWorld.gravity = SCNVector3Make(0, -9.8, 0);
}
#end

Objective-C. How is your first SKScene initialised?

This method sets up the initial scene and is called as soon as the application opens:
#import "GameScene.h"
#import "WarScene.h"
#import "Math.h"
#implementation GameScene
{
SKNode *map;
float oldY;
float oldX;
BOOL buttonClicked;
int thisButton;
float oldMapPosition;
float midScreenX;
float midScreenY;
int separation;
float sendY;
BOOL interacting;
CGVector speed;
float oldPosition;
float newPosition;
BOOL isReleased;
int iterations;
float diff;
BOOL comeToStop;
BOOL actualStop;
BOOL clicked;
}
int numberOfLevels = 20;
-(void)didMoveToView:(SKView *)view {
/* Setup your scene here */
map = [SKNode node];
separation = [UIScreen mainScreen].bounds.size.height;
if (UIDeviceOrientationIsLandscape((UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation]))
{
//landscape mode
midScreenX = ([[UIScreen mainScreen]bounds].size.width>[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
midScreenY = ([[UIScreen mainScreen]bounds].size.width<[[UIScreen mainScreen]bounds].size.height?[[UIScreen mainScreen]bounds].size.width:[[UIScreen mainScreen]bounds].size.height)/2;
}
else
{
//portrait mode
midScreenX = [[UIScreen mainScreen]bounds].size.width/2;
midScreenY = [[UIScreen mainScreen]bounds].size.height/2;
}
NSLog(#"Initial Width: %f Height: %f", midScreenX*2, midScreenY*2);
map.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: CGSizeMake([UIScreen mainScreen].bounds.size.width, separation*(numberOfLevels+1))];
map.physicsBody.affectedByGravity = NO;
map.physicsBody.allowsRotation = NO;
clicked = NO;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(handleTap:)];
tap.numberOfTapsRequired = 1;
[self.view addGestureRecognizer:tap];
self.backgroundColor = [SKColor blackColor];
for (int i = 0; i<numberOfLevels; i += 1) {
SKLabelNode *title;
{
title = [SKLabelNode labelNodeWithFontNamed:#"Cochin"];
title.text = [NSString stringWithFormat: #"Level %d", 1+i];
title.fontSize = 60;
title.position = CGPointMake(CGRectGetMidX(self.frame),
((i+1)*separation));
title.fontColor = [UIColor whiteColor];
title.name = [NSString stringWithFormat: #"Level %d", 1+i];
[map addChild:title];
}
}
[self addChild:map];
}
This works exactly as I intended it to, however, when I call this from another class:
-(void)continueMethod:(UIButton*)button{
NSLog(#"InitwithSize to GameScene Width: %f Height: %f", midScreenX*2, midScreenY*2);
GameScene *map = [[GameScene alloc] initWithSize: CGSizeMake(midScreenX*2 ,midScreenY*2)];
SKTransition *reveal = [SKTransition revealWithDirection:SKTransitionDirectionDown duration:1.0];
SKView * skView = (SKView *)self.view;
UIView *viewToRemove = [self.view viewWithTag:3];
[viewToRemove removeFromSuperview];
[skView presentScene:map transition:reveal];
}
The scene which should be set up doesn't appear as I intended it to, or indeed how it used to look the last time that it was initialised. The separation variable appears bigger, the text appears bigger and everything is wider. I've verified that the UIScreen that I have initialised is exactly the same throughout.
This led me to question how the initial scene of a SKSprite application comes to be and whether it is different to "initWithSize:" however, looking through all the code that comes in the game template I can't ever see my first scene get called. The closest that I can find to initWithSize: is the following from the GameViewController-
GameScene *scene = [GameScene unarchiveFromFile:#"GameScene"];
scene.scaleMode = SKSceneScaleModeAspectFill;
Firstly, is this what is initialising my initial scene? Secondly is it in anyway different to initWithSize: ? And finally if the answer to the previous tow questions is yes, should it be used instead of initWithsize: ?
Yes, this is the initialization of the initial scene! What you are seeing in the View Controller is a slightly different initialization. You are using the sks file, first of all, and initWithCoder: is being called when the view controller unarchives it. However, this isn't very different from initWithSize if you specify all of the same dimensions and properties.
That being said, the scene's scaleMode in your initialization and the view controller's scaleMode is different. As you can see, the view controller specifies:
scene.scaleMode = SKSceneScaleModeAspectFill;
The AspectFill scale mode according to Apple:
The scaling factor of each dimension is calculated and the smaller of the two is chosen. Each axis of the scene is scaled by the same scaling factor. This guarantees that the entire scene is visible but may require letterboxing in the view.
When you initialize your scene, on the other hand, you leave the default scaleMode, which happens to be SKSceneScaleModeFill. This maps each axis of the scene to the view, so you get a distorted scene. What you need to do is simply state:
map.scaleMode = SKSceneScaleModeAspectFill;
This will use the same scale mode as in the original scene and the distortion that you see now will be gone.
Hope this helps, let me know if you have questions.

Using CCScrollView in Cocos2d V3

I'm wondering if anyone can help me with this. I want to add 4 CCScrollViews, that will scroll horizontally, on a CCNode. The CCNode, positioned and held on the device in portrait mode, will fill the entire screen with a normalized contentSize set to cover the entire screen.
So in essence the scroll views will pile on top of each other with the 4th being at the bottom of the screen and the 1st being at the top. Now, I have managed to add the 4 CCScrollViews but only one responds to touches. The others are flat an unmovable. It's almost as though the last CCScrollView added to the node is overlaying the other three and it is the only thing responding to touch requests. All 4 CCScrollViews have their delegate property set to self.
I'm someone coming at Cocos2d with a fair bit of UIKit experience. So, i'm trying to apply my UIScrollView mode of thinking to all of this. Having had a good Google about and coming up with little, I'm wondering if SO can help. I've even been considering winding UIScrollView into Cocos2d.
I guess my main issues here are two-fold. One, I have an issue with touch response. Two, I have an issue with the paging aspect and control of content-size. Via trial and error, I'm sort of getting along but if someone could perhaps write up a bit of a best-practive guide to CCScrollView implementation, specifically where one does not set the contentSize of the CCNode or CCspriteFrame that's added to larger contentView does not fill the entire width of the screen.
Thanks in advance.
#import "CCToolKit.h"
#import "GameShip.h"
typedef enum ShipPart
{
ShipPartHead,
ShipPartBody,
ShipPartWings,
ShipPartBoosters
} ShipPart;
#implementation GameShip
-(instancetype)init {
if (self = [super init]) {
[self addBackground];
[self addScrollTo:ShipPartHead forQtyOfParts:3];
[self addScrollTo:ShipPartBody forQtyOfParts:4];
[self addScrollTo:ShipPartWings forQtyOfParts:3];
[self addScrollTo:ShipPartBoosters forQtyOfParts:3];
}
return self;
}
-(void)addBackground {
CCSprite *bg = [CCSprite spriteWithImageNamed:kGameMainBackGround];
bg.positionType = CCPositionTypeNormalized;
bg.position = ccp(0.5f,0.5f);
[self addChild:bg];
}
-(void)addScrollTo:(ShipPart)shipPart forQtyOfParts:(int)partQty {
NSString *imageFileNameSegment;
switch (shipPart) {
case ShipPartHead:
imageFileNameSegment = #"head";
break;
case ShipPartBody:
imageFileNameSegment = #"body";
break;
case ShipPartWings:
imageFileNameSegment = #"wings";
break;
case ShipPartBoosters:
imageFileNameSegment = #"boosters";
break;
default:
break;
}
CCNode *scrollViewContents = [CCNode node];
scrollViewContents.contentSizeType = CCSizeTypeNormalized;
scrollViewContents.contentSize = CGSizeMake(partQty * 0.65, 0.25f);
NSLog(#"scrollView,height %f", scrollViewContents.boundingBox.size.height);
for (int i = 1; i <= partQty; i++) {
NSString *imageFileName = [NSString stringWithFormat:#"%#%d.png", imageFileNameSegment, i];
CCSprite *shipPartSprite = [CCSprite spriteWithImageNamed:imageFileName];
shipPartSprite.positionType = CCPositionTypeNormalized;
shipPartSprite.position = ccp((i + 0.5f) / partQty, 0.5f);
[scrollViewContents addChild:shipPartSprite];
}
CCScrollView *scrollView = [[CCScrollView alloc] initWithContentNode:scrollViewContents];
scrollView.pagingEnabled = YES;
scrollView.horizontalScrollEnabled = YES;
scrollView.verticalScrollEnabled = NO;
scrollView.color = [CCColor redColor];
scrollView.contentSize = CGSizeMake(0.5f, 0.25f);
scrollView.positionType = CCPositionTypeNormalized;
scrollView.position = ccp(-1.0f, ((shipPart * 0.25f) -0.1f));
scrollView.delegate = self;
//scrollView.description = [NSString stringWithFormat:#"%d", shipPart];
[self addChild:scrollView];
//[scrollView setHorizontalPage:1];
}

Tilemap is not filling up entire scene

I'm using cocos2d with a viewcontroller in Xcode 5. I create a CCDirector as a child viewcontroller, and then add this to a UIView that is in the storyboard.
myViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
CCDirector* director = (CCDirector *)[self childViewControllers][0];
director.view.frame = CGRectMake(0, 0, 320, 176);
[_sceneView addSubview:[director view]];
}
The tilemap is created in IntroLayer.m.
-(id) initWithViewContoller:(MyCocosViewController *)viewController
{
if( (self=[super init])) {
_tileMap = [CCTMXTiledMap tiledMapWithTMXFile:#"apartmentWithWall.tmx"];
[self addChild:_tileMap z:-1];
_tileMap.scale = 1;
_bgLayer = [_tileMap layerNamed:#"Floorplan"];
self.wall = [_tileMap layerNamed:#"Walls"];
_wall.visible = NO;
}
And the scene is made in IntroLayer.m also
+(CCScene *) sceneWithViewController:(MyCocosViewController *)viewController
{
CCScene *scene = [CCScene node];
IntroLayer *layer = [[IntroLayer node] initWithViewContoller:viewController];
// add layer as a child to scene
[scene addChild: layer];
return scene;
}
This code successfully creates the tilemap and puts it into the view. However, the tilemap does not take up the whole scene - it seems to be only half sized. It is placed on the bottom left of the view, and the rest of the view is just black. If you touch on it, then the pixels are not scaled correctly - you can touch on the black part, and it still thinks this is part of the tilemap. This is what it looks like: http://i.imgur.com/hb0gMFI.png
The white and black floorplan on the bottom left should take up the entire scene.
So why isn't it just naturally scaled correctly? The tutorials I've looked at don't seem to have this problem.

Touch accuracy of ccmenuitem is lower when it is placed adjacent to screen edge

When I tried to build and run my game(based on Cocos2d 1.0.1, built in Xcode 4.5 with iOS 6.0 SDK) on iTouch 5, I found the CCMenuItems do not behave normally: when the menuitem is adjacent to screen edge, the edge border seems not so easy to be tapped inside to respond the touch event(Sorry for my poor expression).
To demonstrate the problem, I wrote a demo app with Xcode 4.3 using Cocos2d template and just modify the HelloWorldLayer's init method, and the phenomenon still happens. The code is below:
-(void) init
{
// always call "super" init
// Apple recommends to re-assign "self" with the "super" return value
if( (self=[super init])) {
CCLayerColor *cl = [CCLayerColor layerWithColor:ccc4(ccWHITE.r, ccWHITE.g, ccWHITE.b, 255)];
[self addChild:cl];
// create and initialize a Label
CCLabelTTF *label = [CCLabelTTF labelWithString:#"Hello World" fontName:#"Marker Felt" fontSize:64];
// ask director the the window size
CGSize size = [[CCDirector sharedDirector] winSize];
// position the label on the center of the screen
label.position = ccp( size.width /2 , size.height/2 );
// add the label as a child to this Layer
[self addChild: label];
float width = 160;
CCSprite *sp1 = [CCSprite node];
[sp1 setContentSize:CGSizeMake(width, width)];
[sp1 setTextureRect:CGRectMake(0, 0, width, width)];
[sp1 setColor:ccc3(0xff, 0xff, 0)];
CCSprite *sp2 = [CCSprite node];
[sp2 setContentSize:CGSizeMake(width, width)];
[sp2 setTextureRect:CGRectMake(0, 0, width, width)];
[sp2 setColor:ccc3(0, 0, 0xff)];
CCMenuItemSprite *button = [CCMenuItemSprite itemFromNormalSprite:sp1 selectedSprite:sp2 target:nil selector:nil];
CCMenu *menu = [CCMenu menuWithItems:button, nil];
[self addChild:menu];
menu.position = ccp(0, 0);
button.anchorPoint = ccp(1, 1);
button.position = ccp([[CCDirector sharedDirector] winSize].width,
[[CCDirector sharedDirector] winSize].height);
}
return self;
}
I looked all over the internet and had no luck, wonder someone can help me with that. Many Thanks!
Just a few hunches:
Refrain from setting the sprites contentSizes. They should be set automatically, and may be used by CCMenu.
Make sure you don't change position or anchorPoint of CCMenuItem. Don't change anchorPoint of CCMenu either. This will mess up touch detection. Only use position property of CCMenu.
Make sure no other touch code elsewhere in your project interferes and may be swallowing touches. Gesture Recognizers can also cause CCMenu to misbehave.
If you want to freely position your menu items, make sure to wrap each in a CCMenu node. Then you can position the item via the menu.

Resources