SpriteKit:只是想知道为什么我的脚本无法正常工作?

时间:2014-05-14 05:03:08

标签: ios sprite-kit

只是希望它使节点从顶部水平边缘的任何位置随机掉落并落到页面上。没有从垂直边缘半切掉。它目前显示白屏

#import "MyScene.h"

@interface MyScene () <SKPhysicsContactDelegate>
@end

@implementation MyScene

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */

    self.backgroundColor = [SKColor whiteColor];


    //physics world
    self.physicsWorld.gravity = CGVectorMake(0,-6);
    self.physicsWorld.contactDelegate = self;
}
return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

}



-(void) addBalls {
    //create ball sprite
    SKSpriteNode *balls = [SKSpriteNode spriteNodeWithImageNamed:@"Ball.png"];
    balls.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(balls.size.width/2)];
    balls.physicsBody.dynamic = YES;

    //determine where to spawn balls
    int minX = balls.size.height/2;
    int maxX = self.frame.size.height - balls.size.height/2;
    int rangeX = maxX - minX;
    int actualX = (arc4random() % rangeX) + minX;

    //place ball slightly off shot, and along a random position on top edge
    balls.position = CGPointMake(self.frame.size.height + balls.size.width/2, actualX);
    [self addChild:balls];

    //speed of balls
    int minDuration = 2.0;
    int maxDuration = 4.0;
    int rangeDuraton = maxDuration - minDuration;
    int actualDuration = (arc4random() & rangeDuraton) + minDuration;

    //create the actions
    SKAction *moveAction = [SKAction moveTo:CGPointMake(-balls.size.height/2, actualX) duration:actualDuration];
    SKAction *actionMovedone = [SKAction removeFromParent];

    [balls runAction:[SKAction sequence:@[moveAction, actionMovedone]]];
}

只是希望它使节点从顶部水平边缘的任何位置随机掉落并落到页面上。没有从垂直边缘半切掉。它目前显示白屏

1 个答案:

答案 0 :(得分:1)

首先,您需要在球节点的位置交换x和y值。

其次,要添加多个球,您可以按如下方式修改代码:

-(void) addBalls:(int)count
{
    for (int i = 0; i < count; i++)
    {
        //create ball sprite
        SKSpriteNode *balls = [SKSpriteNode spriteNodeWithImageNamed:@"Ball.png"];
        balls.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:(balls.size.width/2)];
        balls.physicsBody.dynamic = YES;

        //determine where to spawn balls
        int minX = balls.size.height/2;
        int maxX = self.frame.size.height - balls.size.height/2;
        int rangeX = maxX - minX;
        int actualX = (arc4random() % rangeX) + minX;

        //place ball slightly off shot, and along a random position on top edge
        balls.position = CGPointMake(actualX, self.frame.size.height + balls.size.width/2);
        [self addChild:balls];

        //speed of balls
        int minDuration = 2.0;
        int maxDuration = 4.0;
        int rangeDuraton = maxDuration - minDuration;
        int actualDuration = (arc4random() & rangeDuraton) + minDuration;

        //create the actions
        SKAction *moveAction = [SKAction moveTo:CGPointMake(-balls.size.height/2, actualX) duration:actualDuration];
        SKAction *actionMovedone = [SKAction removeFromParent];

        [balls runAction:[SKAction sequence:@[moveAction, actionMovedone]]];
    }
}

然后,只需传递方法中所需的球数。

EG。

[self addBalls:10]; //Will add 10 balls to the scene
相关问题