+bodyWithRectangleOfSize:
INDEX>Sprite Kit> SKPhysicsBody
重力の影響を受ける矩形の物理ボディを作成して返します
+ (SKPhysicsBody *)bodyWithRectangleOfSize:(CGSize)s
解説
重力の影響を受ける矩形の物理ボディを作成して返します。
返り値
(SKPhysicsBody *)
引数
(CGSize)s
サイズ
フレームワーク
Sprite Kit
クラス
SKPhysicsBody
使用可能
iOS 7.0以降
定義
SKPhysicsBody.h
参照
例文
-(id)initWithSize:(CGSize)size { if (self = [super initWithSize:size]) { /* Setup your scene here */ //シーンのバックグラウンドカラー設定 self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0]; //ラベルノード作成 SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"]; //ラベルノードの設定 myLabel.text = @"Hello, World!"; myLabel.fontSize = 30; myLabel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame)); //シーンにラベルノードを追加 [self addChild:myLabel]; //物理枠を設定 self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:(CGRect){CGPointZero, size}]; } return self; } -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { /* Called when a touch begins */ for (UITouch *touch in touches) { //位置設定 CGPoint location = [touch locationInNode:self]; //スプライトノード作成 SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"]; //スプライトノードの初期位置を設定 sprite.position = location; //スプライトノードの CGSize aSize = CGSizeMake(100.0f, 100.0f); sprite.size = aSize ; SKPhysicsBody *pbody = [SKPhysicsBody bodyWithRectangleOfSize: aSize ]; //スプライトの物理ボディをセット sprite.physicsBody = pbody ; [self addChild:sprite]; } }