【发布时间】: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