SpriteKit Block定位

时间:2014-04-05 15:15:41

标签: sprite-kit game-physics

我正在尝试使用SpriteKit在Xcode 5中进行跳转游戏。 首先,我想在随机位置生成块,但是在跳跃的游戏中可以找到“人类”。 我有一个1. Block,它在一个固定位置产卵。从这个位置,应该产生一个2.块,可以是+或 - 80更高或更低,并且< 80以外。现在问题是,我不知道如何在Xcode中编码。到目前为止我的代码:

         SKTexture * BlockTexture1 = [SKTexture textureWithImageNamed:@"Block"];
    BlockTexture1.filteringMode = SKTextureFilteringNearest;
    SKTexture * BlockTexture2 = [SKTexture textureWithImageNamed:@"Block"];
    BlockTexture2.filteringMode = SKTextureFilteringNearest;

    SKSpriteNode * Block1 = [SKSpriteNode spriteNodeWithTexture:BlockTexture1];
    [Block1 setScale:1];
    Block1.position = CGPointMake( 100 , 150 );
    Block1.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Block1.size];
    Block1.physicsBody.dynamic = NO;
    Block1.physicsBody.usesPreciseCollisionDetection = YES;
    Block1.zPosition = 3;
    [self addChild:Block1];

    SKSpriteNode * Block2 = [SKSpriteNode spriteNodeWithTexture:BlockTexture2];
    [Block2 setScale:1];
    Block2.position = CGPointMake( **Block1 + or - 80, Block 1 < 80** );
    Block2.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:Block2.size];
    Block2.physicsBody.dynamic = NO;
    Block2.physicsBody.usesPreciseCollisionDetection = YES;
    Block2.zPosition = 3;
    [self addChild:Block2];

1 个答案:

答案 0 :(得分:0)

为了让你开始,也许这样:

int block1x = 100;
int block1y = 150;

Block1.position = CGPointMake(block1x, block1y);

int block2x = 0;
int block2y = 0;

int r1 = arc4random() % 2; // produces random numbers between 0 and 1
if(r1 == 0)
   block2x = block1x + 80;
if(r1 == 1)
   block2x = block1x - 80;

int r2 = arc4random() % 2;
if(r2 == 0)
   block2y = block1y + 80;
if(r2 == 1)
   block2y = block1y - 80;

Block2.position = CGPointMake(block2x, block2y);