3

I have a custom class "LinkWithNumber" with three sprites and on the GameLayer in update I am trying to test CGRectContainsRect for collisions but I am having trouble trying to access the sprite in the class file (I don't have much experience so most likely I am messing up :P)

I have tried the following:

LinkWithNumber.h

@interface LinkWithNumber : SKSpriteNode <SKPhysicsContactDelegate>
{
SKSpriteNode *collide;
}

LinkWithNumber.m

@synthesize collide;

   //add collision object to the class
    collide = [[SKSpriteNode alloc]initWithColor:[SKColor blueColor]
                            ...blah blah as normal
   [self addChild:collide];
   collide.name = @"collide";

GameLayer.h

@class LinkWithNumber;
@interface GameScene : SKScene <SKPhysicsContactDelegate>
{
LinkWithNumber* twoSpritesWithParticlesBridge;
}
@property (nonatomic, strong)LinkWithNumber* twoSpritesWithParticlesBridge;

GameLayer.m

@synthesize twoSpritesWithParticlesBridge;

    -(void)addStaticLinkedSpriteWithParticles
{
    twoSpritesWithParticlesBridge =
    [[LinkWithNumber alloc]initWithlinkSpriteA:@"RoseMine06"
                                       spriteB:@"RoseMine06"
                             andPlistAnimation:@"need to create animations"
                                   distbetween:300
                                  hasParticles:YES
                                ParticlesNamed:@"Fire"];
      [self addChild:self->twoSpritesWithParticlesBridge];


    twoSpritesWithParticlesBridge.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize: twoSpritesWithParticlesBridge.frame.size];



}

-(void)update:(CFTimeInterval)currentTime {

LinkWithNumber *currentSprite = 
(LinkWithNumber*)[self  childNodeWithName:@"collide"];

 //NSLog(@"currentSprite Name @%@", currentSprite); //gets nil

if (CGRectContainsRect(myShip02.frame,currentSprite.frame)) {
NSLog(@"Hit barrier can pass");
        }
}

Any help would be appreciated :)

How to locate your class object... The Solution, with thanks to 0x141E!

 //name it on setup inside your customCLass 
 //eg yourObject.name = @"collide";

   //Now in Gamelayer locate your object by recursive search
   //it will look for any object named @"//collide"
   //without the slashes it will only look on the game layer
   //but since we need to dig a bit further we need them!

 LinkWithNumber *currentSprite =
(LinkWithNumber*)[self  childNodeWithName:@"//collide"];
NSLog(@"LinkWithNumber is %@",NSStringFromClass([currentSprite class]));


    //do something with your object
    if (currentSprite.position.y >0 ) {
        NSLog(@"currentSprite Position %f",currentSprite.position.y);
    }

extras

Sknode Class ref for other search functions

How to enumerate all nodes

Community
  • 1
  • 1
StackBuddy
  • 577
  • 5
  • 17

1 Answers1

3

I see two issues...

  1. You are searching only the root node (in this case the scene) for the node named "collide", but that node is a child of LinkWithNumber node not the scene. To recursively search the entire node tree, use @"//collide"
  2. You are casting the result of the search to a LinkWithNumber pointer, but collide is an SKSpriteNode not a LinkWithNumber.
0x141E
  • 12,613
  • 2
  • 41
  • 54
  • 0x141E thanks I will try in test in the weekend. (I'll come back with results - and mark it as correct once I've tested it) To be sure that I do understand it properly and havent missed anything. thanks again and have good weekend! – StackBuddy Aug 14 '15 at 07:56