Sprite position not working in Objective-C, Xcode

2019-09-04 23:08发布

It has been a year or so since I've used Objective-C and I must have forgot something because this is stumping me. I didn't use a for loop because I wanted a delay, but I logged all these variables, r is a float, j is an integer. r and j were initialized to 0.

-(void)fire{
    r = j;
    if (j < 80) {

        j = j + 1;
        bullet.position = CGPointMake(2*r, 15);

        NSLog(@"r is %f", r);
        NSLog(@"j is %i", j);
        NSLog(@"position is %f, %f",bullet.position.x, bullet.position.y);

        NSLog(@"x is, %f", r/8);
        [self performSelector:@selector(fire) withObject:nil afterDelay:.05];
    }
}

I get 80 readouts in the log and this is one of them, arbitrarily when r is 70:

r is 70.000000
j is 71
position is 0.000000, 0.000000
x is, 8.750000

So why is the position always (0,0) when I am clearly telling it to be (2*r, 15)?

By the way, here is the code I initialize the bullet with

bullet = [SKSpriteNode spriteNodeWithImageNamed:@"missile"];
bullet.xScale = 1;
bullet.yScale = 1;
bullet.position = CGPointMake(67, 148);
bullet.zRotation = M_PI;

[self addChild:bullet];

0条回答
登录 后发表回答