スプライトキットを使って空港シミュレーションゲームを作っています。SKPhysics ボディを追加する前のゲーム状態のレイアウトはそのままで、SKPhysicsbody がノードに設定されると、スプライト ノードが警戒します。
これは、SKPhysicsBody なしでシーンに追加しているものです。多くのゲートがあり、ゲートの隣にフライトが立っている空港を想像してみてください。それが、以下のコードで達成しようとしていることです。
@実装 MyScene
-(id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size])
{
allPlanes = [[NSMutableArray alloc]init];
// setting gravity to 0
[[self physicsWorld] setGravity:CGVectorMake(0, 0)];
[[self physicsWorld] setContactDelegate:self];
[self setBackgroundColor:[UIColor whiteColor]];
// Below method will provide runway and other static objects that can be seen in an airport
[self setupAirport];
/* This is where I am setting up by plane sprites.This method will provide a node which is added to the scene. As you can notice, the sprites are added at specific coordinates until they fill the screen. Imagine three or four gates with flights standing by those gates. That is what I am trying to achieve with below while loop */
int xval = 20;
while (xval < kScreenWidth)
{
[self setupFlightAtPoint:xval];
xval = xval + 60;
}
}
return self;
}
メソッドのコード [self setupFlightAtPoint:xPos]
-(void) setupFlightAtPoint:(CGFloat ) xPos
{
// Below code will provide a static gate like object.gateNode is of type Gate class which is a subclass of SKNode
gateNode = [[Gate node] newGate];
[gateNode setPosition:CGPointMake(xPos, kScreenHeight * 0.37)];
[self addChild:gateNode];
// Below code provide plane node and positions it near the gate object.Plane is subclass of SKNode
Plane *plane = [[Plane alloc]init];
imageNode = [plane newPlane];
imageNode.planeIdentifier = xPos;
[imageNode setPosition:CGPointMake(gateNode.frame.origin.x + 12, gateNode.frame.origin.y+15)];
[allPlanes addObject:imageNode];
[self addChild:imageNode];
}
平面オブジェクト法
-(instancetype) newPlane
{
[self setScale:0.10];
SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"plane.png"];
[self addChild:spriteNode];
return self;
}
これまでのところ、すべて問題ないように見えます。添付の Scene1 という画像を参照して、上記のコードで何が表示されるかを確認してください。
物理ボディをプレーンスプライトに設定しようとすると、ここから問題が始まります。私の「newPlane」メソッドでは、以下のコードを追加しています
-(instancetype) newPlane
{
[self setScale:0.10];
SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"plane.png"];
[self addChild:spriteNode];
SKPhysicsBody *planePhysics = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.frame.size];
[spriteNode setPhysicsBody:planePhysics];
[[spriteNode physicsBody] setAffectedByGravity:NO];
return self;
}
Physicsbodies を設定した後、私のシーンは次のようになります
現在、シーンに平面スプライトが 1 つしか表示されていませんが、その理由がわかりません。