-1

I'm new in Objective-C developing and I'm working on project that uses SpriteKit. I'm trying to create a scene that contains an arena with corners as controlPoints.

my code is as below:

MyScene.m:

-(id)initWithSize:(CGSize)size {

CGPointArray=[[NSMutableArray alloc] init];         // Kugan: cette instruction ne sert à rien dans initWithSize d'après moi.

if (self = [super initWithSize:size]) {

    // Setting up the scene background
    SKSpriteNode *background = [SKSpriteNode spriteNodeWithImageNamed:@"Background"];
    [background setName: @"background"];
    background.anchorPoint = CGPointZero;

    [self addChild:background];

    // creating the unique instance of the menu at the right side of the scene
    ref=[ElementsMenuSingleton MenuChoice];


    // Creating the points that will represente the borders of the scene
    CGPoint leftBottomPoint = CGPointMake(50, 150);
    CGPoint centerBottomPoint = CGPointMake(350, 150);
    CGPoint rightBottomPoint = CGPointMake(650, 150);
    CGPoint rightCenterPoint = CGPointMake(650, 300);
    CGPoint rightTopPoint = CGPointMake(650, 450);
    CGPoint centerTopPoint = CGPointMake(350, 450);
    CGPoint leftTopPoint = CGPointMake(50, 450);
    CGPoint leftCenterPoint = CGPointMake(50, 300);

    CGPoint positions[] = {
        CGPointMake(leftBottomPoint.x , leftBottomPoint.y),
        CGPointMake(centerBottomPoint.x, centerBottomPoint.y),
        CGPointMake(rightBottomPoint.x, rightBottomPoint.y),
        CGPointMake(rightCenterPoint.x, rightCenterPoint.y),
        CGPointMake(rightTopPoint.x, rightTopPoint.y),
        CGPointMake(centerTopPoint.x, centerTopPoint.y),
        CGPointMake(leftTopPoint.x, leftTopPoint.y),
        CGPointMake(leftCenterPoint.x, leftCenterPoint.y),
    };

    // This array will contain all the selected nodes
    _selectedNodesVector = [[NSMutableArray alloc] init];

    // adding the points to the vector that contains the controlPoints
    for (int i = 0; i < 8; i++) {
        ControlPointNode* controlPointTmp = [[ControlPointNode alloc] init:positions[i] :i];
        [_controlPointsVector addObject:controlPointTmp];
    }
    // create arena with the controlPointVector
    _arena = [[ArenaNode alloc ] init:_controlPointsVector ];

    [self addChild:_arena];

}
return self;

}

and MyArena.m

-(id) init:(NSMutableArray *)controlPointVector{

if (self = [super init]) {

    controlPointsVector =[[NSMutableArray alloc]init];

    controlPointsVector = controlPointVector;

    self.name = @"arena";

    // set physical aspect of the arena
    self.frictionForce = 0.0;
    self.bouncingForce = 1.0;
    self.acceleration = 1.0;

    // draw the shape of the arena
    [self drawArena];

    for(ControlPointNode* controlPointTmp in controlPointsVector){

        [self addChild:controlPointTmp];
    }

    [self addChild:_shape];

}
return self;

} -(void) drawArena{

_pathToDraw = CGPathCreateMutable();


CGPathMoveToPoint (_pathToDraw, NULL, [[controlPointsVector objectAtIndex:0] position].x,
                   [[controlPointsVector objectAtIndex:0] position].y );

for(int i=1; i<8; i++){

    CGPathAddLineToPoint(_pathToDraw, NULL, [[controlPointsVector objectAtIndex:i] position].x ,
                         [[controlPointsVector objectAtIndex:i] position].y );
}

CGPathCloseSubpath(_pathToDraw);

_shape = [[SKShapeNode alloc]init];
_shape.path = CGPathCreateCopy(_pathToDraw);
_shape.fillColor = [UIColor colorWithRed:1.0 green:0.8 blue:0.4 alpha:1.0];

//the following instruction allows the physics bodies that hit the borders of the scene to bounce
// From: Documentation - SKPhysicsBody Class Reference

_shape.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromPath:_shape.path];
[self updatePhysicsBody:_shape.physicsBody :@"ArenaNode"];

}

The problem is that when I try to run/build, the arena does not appear. and when I debug I found out that controlPointVector remain always nil.

Can anyone help with this matter please.

N.B: 1- I used NSMutableArray because I need to move the controlPoints and I have to keep track of any change that happen in the scene.

2- The arena is not static, it has to be dynamic to allow user to change it.

Thanks in advance.

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Guizmoo03
  • 660
  • 1
  • 7
  • 23

1 Answers1

1

Actually, it was beginner mistake I did not initialized the vector in the right place

controlPointsVector =[[NSMutableArray alloc]init];

Should be before

 // adding the points to the vector that contains the controlPoints
for (int i = 0; i < 8; i++) {
    ControlPointNode* controlPointTmp = [[ControlPointNode alloc] init:positions[i] :i];
    [_controlPointsVector addObject:controlPointTmp];
}

in MyScene.m instead of being in MyArena.m. Now I have the arena in my scene.

Guizmoo03
  • 660
  • 1
  • 7
  • 23