我是 Objective-C
开发的新人,我正致力于使用 {{1 }} 的。
我试图创建一个包含角落作为controlPoints的竞技场的场景。
我的代码如下:
MyScene.m:
SpriteKit
}
和MyArena.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;
} - (void)drawArena {
-(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;
}
问题在于,当我尝试运行/构建时,竞技场不会出现。当我调试
我发现 _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"];
始终 controlPointVector
。
请有人帮忙解决这个问题。
N.B:1-我使用了NSMutableArray,因为我需要移动controlPoints,我必须跟踪场景中发生的任何变化。
2-竞技场不是静态的,它必须是动态的,以允许用户更改它。
提前致谢。
答案 0 :(得分:1)
实际上,这是初学者错误我没有在正确的位置初始化矢量
controlPointsVector =[[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];
}
MyScene.m
,而不是 MyArena.m
。
现在我的场景中有竞技场。