精灵产生的位置不正确

时间:2015-01-30 23:04:31

标签: ios objective-c sprite-kit

我有一排5个精灵,它们每4秒产生一次并沿着Y轴向下移动。精灵每次都会产生正确的数量,我在屏幕上有5个相等的间距,它们都是完美的中心。

然而,当调用performSelector动作时,当它生成它们时,它们不会到达正确的位置。到目前为止向左移动我只能在屏幕左侧看到半个精灵。在测试之后,它们似乎都在错误的位置堆叠在一起。

有什么想法吗?我很感激任何帮助。这是我在场景中使用的所有代码。

-(void) addBricks:(CGSize)size {
for (int i = 0; i < 5; i++) {
    //create brick sprite from image
    SKSpriteNode *brick = [SKSpriteNode spriteNodeWithImageNamed:@"brick"];
    //resize bricks
    brick.size = CGSizeMake(60, 30);
    //psoition bricks
    int xPos = size.width/7.5 * (i+.5);
    int yPos = 450;
    brick.position = CGPointMake(xPos, yPos);

    //add move action
    SKAction *wait = [SKAction waitForDuration:3];
    SKAction *move = [SKAction moveByX:0 y:-36.9 duration:1];
    SKAction *sequence = [SKAction sequence:@[wait, move]];
    SKAction *repeatMove = [SKAction repeatActionForever:sequence];


    [brick runAction:repeatMove];
    [self addChild:brick];

}
}

-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
    self.backgroundColor = [SKColor colorWithRed:(243.0f/255) green:(228.0f/255) blue:(165.0f/255) alpha:1.0];

    //add action to spawn bricks
    SKAction *spawn = [SKAction performSelector:@selector(addBricks:) onTarget:(self)];
    SKAction *delay = [SKAction waitForDuration:4];
    SKAction *delayThenSpawn = [SKAction sequence:@[delay, spawn]];

    [self runAction:[SKAction repeatActionForever:delayThenSpawn]];

    [self addBricks:size];


}
return self;
}

1 个答案:

答案 0 :(得分:2)

作为上一个问题中提到的LearnCocos2D ..当你将参数传递给方法时,你不能使用选择器。

addBricks期待size ..你没有将大小传递给它。

将您的生成动作更改为

SKAction *spawn = [SKAction runBlock:^{
    // make this whatever size you want, i'm just using the scene's size
    [self addBricks:self.size];
}];

应该让你开始