0

I'm very new to the spriteBuilder and cocos2d, which means I'm fairly certain I am doing something wrong... I just don't know what.

I've added a CCNode into a "level-scene" that I load as a child scene to my "gameplay scene", but I can't seem to figure out how to reference the CCNode that is in the level scene.

Kind of like this:

Gameplay scene (CCScene) Level1 scene (CCScene) Target (CCNode)

And then in my code I do:

gameplay = [CCBReader loadAsScene:@"Levels/Level1"];

And then I want to get the position of my target which I've placed inside the Level. How would I do that? It has a "doc root var" but I can only access that inside a custom "target"-class. How do I access that inside gameplay? This bit of code gives me {0, 0} when run from gameplay but {393, 193} when run inside the target class.

NSLog(@"Target pos = %@", NSStringFromCGPoint(_target.position));

I am sorry if this is confusing, I'll try to make it clearer. And sorry if it's a stupid question, I am just getting started.

Thanks for any and all help.

Edit: Logging [self children] on gameplay gives:

("<CCSprite = 0x993e670 | Rect = (0.00,0.00,500.00,281.50) | tag =  | atlasIndex = -1>",
"<CCSprite = 0x9c9a350 | Rect = (68.00,0.50,32.00,25.50) | tag =  | atlasIndex = -1>",
"<CCPhysicsNode = 0xba3a220 | Name = >",
"<CCButton = 0x9c9db20 | Name = >",
"<CCButton = 0x9ca33e0 | Name = >",
"<CCLabelTTF = 0x9ca5320 | FontSize = 50.0>",
"<CCButton = 0x9ca6010 | Name = >",
"<CCButton = 0x9ca7f50 | Name = >",
"<CCSlider = 0x9caa060 | Name = >",
"<CCLabelTTF = 0x9caeed0 | FontSize = 16.0>",
"<CCLabelTTF = 0x9cafa10 | FontSize = 15.0>",
"<CCSprite = 0x9cb02f0 | Rect = (107.50,29.00,75.00,64.50) | tag =  | atlasIndex = -1>",
"<CCSprite = 0x9cb08c0 | Rect = (107.50,29.00,75.00,64.50) | tag =  | atlasIndex = -1>",
"<CCSprite = 0x9cb0e90 | Rect = (107.50,29.00,75.00,64.50) | tag =  | atlasIndex = -1>",
"<CCLabelTTF = 0x9cb1460 | FontSize = 50.0>")
James Webster
  • 31,873
  • 11
  • 70
  • 114
Jens Hendar
  • 174
  • 1
  • 2
  • 10
  • There's no such thing as a stupid question my friend ! :), first thing to check is if your `_target` is loaded correctly ? is it nil or is it the actual sprite you want ? you can try to `NSLog(@"%@", _target)` and tell us what type of CCNode you see ? – Mostafa Berg Feb 14 '14 at 12:47
  • Of course! In my "target.h"-class it gives "" and in my gameplay.h it gives "(null)". (In spritebuilder my sprite in target.ccb has the custom class "Target" and doc root var "_target") – Jens Hendar Feb 14 '14 at 12:55
  • So that's the problem, `_target` is `null`... in `gameplay` how do you initialise `_target` ? probably that's where the error is ! can you add a snippet from the `gameplay` implementation ? – Mostafa Berg Feb 14 '14 at 13:03
  • That's probably it. I thought that if I included my Target.h-class and initalized it there that I could reach it from the parent aswell. In target.h i do "CCNode* _target;" and then just "NSLog(@"%@", _target);" in the .m-file. Can I add it as a child in gameplay at the same position where it is already in? – Jens Hendar Feb 14 '14 at 13:10
  • Yeah of course you can, you just need a way to find it by using `[self children]` in the `gameplay` class, but that's why custom classes were made in the first place, to avoid the need of doing this, it's hard to see where the error is now without seeing more code, but what's the output of `NSLog(@"%@", [self children]);` in gameplay ? – Mostafa Berg Feb 14 '14 at 13:33
  • The whole point is that it's already added, but for some reason the class isn't being initialised, you're probably missing something really simple.. – Mostafa Berg Feb 14 '14 at 13:35
  • Added the output of logging children on gameplay to the question. :) Thanks for helping me btw. Edit: But my other code-snippet from my "target"-class ("self.physicsBody.collisionType = @"Target";") works since my action to check for collision recognizes "Target". – Jens Hendar Feb 14 '14 at 13:37
  • I understand, i guess the only choice left is to set a tag for the item in builder, then get the child with that tag and assign it to `_target`, but this is a hack not a solution.. – Mostafa Berg Feb 14 '14 at 15:50

1 Answers1

0

I solved it. It seemed that since I had a hierarchy like this: gamePlay->levelNode->level1

I had to do this to set my target:

_target = [[levelNode getChildByName:@"level1" recursively:false] getChildByName:@"target" recursively:false];

Seems to have solved it for now atleast. :)

Jens Hendar
  • 174
  • 1
  • 2
  • 10