2014-02-24 2 views
0

스프라이트 키트를 사용하여 공항 시뮬레이션 게임을 제작 중입니다. SKPhysics 본문을 추가하기 전에 게임 상태에 대한 내 레이아웃을 그대로 유지하고 일단 SKPhysicsbody가 노드로 설정되면 내 스프라이트 노드는주의를 기울입니다.동일한 SKPhysics 본문 개체가있는 여러 스프라이트

이것은 SKPhysicsBody가없는 씬에 추가하는 것입니다. 많은 게이트가있는 공항과 게이트 옆에 서있는 항공편을 상상해보십시오. 그것이 내가 아래 코드로 달성하고자하는 것입니다. 이제 모든 fine.Please 첨부 참조 보인다 틸

-(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; 
} 

: 방법에 대한

@implementation의 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; 
} 

코드 [X 좌표 자기 setupFlightAtPoint] 위의 코드에서 볼 수있는 장면을 보려면 scene1이라는 이미지를 사용하십시오. enter image description here

이제 내 몸체를 내 비행기 스프라이트에 설정하려고 할 때 여기에서 내 문제가 시작됩니다. 내 "newPlane"방법은, 내가 Physicsbodies를 설정 한 후 코드

-(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; 
} 

아래에 추가하고, 내 장면은 하나 개의 평면 스프라이트와 내가 지금 내 장면에서 볼이

enter image description here

처럼 보인다 이유를 알아낼 수 없습니까?

답변

1

시도 초기화 및 아이로 스프라이트를 추가하기 전에 물리 몸을 할당 : 나는 또한 점 표기법을 사용하도록 코드를 변환

-(instancetype) newPlane 
{ 
    [self setScale:0.10]; 
    SKSpriteNode *spriteNode = [SKSpriteNode spriteNodeWithImageNamed:@"plane.png"]; 
    spriteNode.position = CGPointMake(100, 200); 

    SKPhysicsBody *planePhysics = [SKPhysicsBody bodyWithRectangleOfSize:spriteNode.frame.size]; 
    spriteNode.physicsBody = planePhysics; 
    spriteNode.physicsBody.affectedByGravity = NO; 

    [self addChild:spriteNode]; 

    return self; 
} 

, 내가 입력하고 읽기이 쉽게 찾을 수 있습니다.

+0

응답 해 주셔서 감사합니다. 나는 옵션을 시도했지만 여전히 같은 문제가있다. – slysid

+0

내 답변을 편집 했으므로 추가하거나 물리학 자 본문을 만들기 전에 비행기의 위치를 ​​설정해야 할 수 있습니다. – LearnCocos2D

+0

문제가 해결되었습니다. [planePhysics setDynamic : NO]를 추가하는 중입니다. 내 문제가 해결되었습니다. – slysid

관련 문제