【问题标题】:SpriteKit: Just wondering why my script won't work properly?SpriteKit:只是想知道为什么我的脚本不能正常工作?
【发布时间】:2014-05-14 19:13:15
【问题描述】:

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

#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]]];
}

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

【问题讨论】:

  • 你在代码的什么地方调用了 addBalls 方法?
  • 在设置球的位置时交换 x 和 y 值
  • 修复了,谢谢!我还有一个问题,如何将多个节点添加到场景中?使用 [self add child:node] 它只会产生 1.
  • 可以将代码包裹在一个for循环中,加上需要添加的球数
  • 我添加了两个问题的解决方案的答案

标签: ios sprite-kit


【解决方案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]]];
    }
}

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

例如。

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

【讨论】:

  • 救命稻草!非常感谢!既然你有点角色,我知道有人已经给出了一些答案,你认为你可以帮助我吗? stackoverflow.com/questions/23652571/…
  • 我在上面的 initwithsize 代码中收到一个错误。 [自我添加球]; “MyScene”没有可见的@interface 声明选择器“addBalls”。当我不将它添加到 initwithsize 方法时,它们不存在。
  • 需要 15 分,我没有足够的...不过我会的。我如何接受?
  • 那是因为您没有使用 addBalls 方法传递任何参数。你需要传递这样的东西:[self addBalls:10];
  • 解决了我的问题,对不起我的错:D。谢谢。我已经将您写的内容应用到出现在框架中的非动态“块”节点上,以便球节点反弹,是否可以添加一些东西,以便每个块节点可以相隔一定距离 X 和 Y?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-03
  • 2023-02-20
  • 1970-01-01
  • 1970-01-01
  • 2014-10-06
  • 2012-09-11
相关资源
最近更新 更多