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