ios - SpriteKit : Just wondering why my script won't work properly?

标签 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]]];
}

只是想让它让节点从顶部水平边缘的任何地方随机落下并落下页面。没有从垂直边缘被砍掉一半。它当前显示白屏

最佳答案

首先需要交换球节点位置的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]]];
    }
}

然后,只需传递方法中需要的球数即可。

例如。

[self addBalls:10]; //Will add 10 balls to the scene

关于ios - SpriteKit : Just wondering why my script won't work properly?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23645958/

相关文章:

ios - 如何使用 Swift 以编程方式使用交替条纹图案填充 UIView?

iphone - 一个 View 中的两个 NSURLConnections

ios - NSTimer 不重复

ios - SpriteKit 按钮

swift - 在 swift 中将带有参数的函数分配给变量

ios - 有没有办法以编程方式将 View 从一个按钮居中到另一个按钮?

android - 如何在离线模式下在 iOS 中实现自动续订?

ios - 我在哪里可以找到 AdMob 的测试设备 ID

sprite-kit - 如何在击中某些敌人后删除敌人?

android - 适用于 Spritekit