大家好,我没有将物理应用到我的阵列中的图像。 我想要做的就是随机选择一堆图像在随机位置弹出现场,我所做的就是首先将它们声明为SKSpriteNode,给它们精灵然后再使用它们的物理,然后使用它们的标识符#34 ; iamge1 ... 6"在数组中。我这样做的唯一原因是因为我需要有几个不同的精灵或敌人我应该说要在场景中弹出但是我需要能够正确地射击它们,我需要物理学应用于此。我在这里采取正确的方法吗?
我的代码:
- (void) itemsONScene {
image1 = [SKSpriteNode spriteNodeWithImageNamed:@"b1"];
image2 = [SKSpriteNode spriteNodeWithImageNamed:@"b2"];
image3 = [SKSpriteNode spriteNodeWithImageNamed:@"b3"];
image4 = [SKSpriteNode spriteNodeWithImageNamed:@"b4"];
image5 = [SKSpriteNode spriteNodeWithImageNamed:@"b5"];
image6 = [SKSpriteNode spriteNodeWithImageNamed:@"b6"];
image7 = [SKSpriteNode spriteNodeWithImageNamed:@"b7"];
image1.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image1.frame.size.width/2];
image1.physicsBody.usesPreciseCollisionDetection = YES;
image1.physicsBody.dynamic = YES;
image1.physicsBody.categoryBitMask = CNPhysicsimage1Category;
image1.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image1 attachDebugRectWithSize:image1.size];
//////
image2.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image2.frame.size.width/2];
image2.physicsBody.usesPreciseCollisionDetection = YES;
image2.physicsBody.dynamic = YES;
image2.physicsBody.categoryBitMask = CNPhysicsimage2Category;
image2.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image2 attachDebugRectWithSize:image2.size];
//////
image3.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image3.frame.size.width/2];
image3.physicsBody.usesPreciseCollisionDetection = YES;
image3.physicsBody.dynamic = YES;
image3.physicsBody.categoryBitMask = CNPhysicsimage3Category;
image3.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image3 attachDebugRectWithSize:image3.size];
//////
image4.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image4.frame.size.width/2];
image4.physicsBody.usesPreciseCollisionDetection = YES;
image4.physicsBody.dynamic = YES;
image4.physicsBody.categoryBitMask = CNPhysicsimage4Category;
image4.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image4 attachDebugRectWithSize:image4.size];
//////
image5.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image5.frame.size.width/2];
image5.physicsBody.usesPreciseCollisionDetection = YES;
image5.physicsBody.dynamic = YES;
image5.physicsBody.categoryBitMask = CNPhysicsimage5Category;
image5.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image5 attachDebugRectWithSize:image5.size];
/////
image6.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image6.frame.size.width/2];
image6.physicsBody.usesPreciseCollisionDetection = YES;
image6.physicsBody.dynamic = YES;
image6.physicsBody.categoryBitMask = CNPhysicsimage6Category;
image6.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image6 attachDebugRectWithSize:image6.size];
/////
image7.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:image7.frame.size.width/2];
image7.physicsBody.usesPreciseCollisionDetection = YES;
image7.physicsBody.dynamic = YES;
image7.physicsBody.categoryBitMask = CNPhysicsimage7Category;
image7.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[image7 attachDebugRectWithSize:image7.size];
/////
myArray = [[NSMutableArray alloc] initWithObjects:@"b1", @"b2", @"b3", @"b4", @"b5",
@"b6", @"b7", nil];
NSUInteger arraypicker = arc4random() % [myArray count];
NSUInteger i = arraypicker;
ds = [myArray objectAtIndex:i];
sprites = [SKSpriteNode spriteNodeWithImageNamed:ds];
sprites.name = @"items";
sprites.position = CGPointMake(ScalarRandomRange(325, 525),
ScalarRandomRange(self.size.height/1.20, self.size.height/12));
sprites.xScale = 0.79;
sprites.yScale = 0.79;
[self addChild:sprites];
}
- (void) OneItemONLY {
SKAction *appear = [SKAction scaleTo:0.79 duration:0.05];
SKAction *waiter = [SKAction waitForDuration:2.5];
SKAction *scaleDown = [SKAction scaleTo:0.0 duration:2.0];
SKAction *remover = [SKAction removeFromParent];
[sprites runAction:[SKAction sequence:@[appear, waiter, scaleDown, remover]]];
[sprites runAction:appear];
}
答案 0 :(得分:0)
你能不能先为每张图片设置这样的所有属性吗?
-(NSMustableArray*)createMutableArrayOfImagesWithAmount:(int)amount andImageNamed:(NSString*)string{
amount -= 1;
NSMutableArray *mutableArray = [[NSMutableArray alloc] init];
SKSpriteNode *node;
for (int i = 0, i <= amount, i++){
node = [SKSpriteNode spriteNodeWithImageNamed:[NSString stringWithFormat:@"%@%i", string, i]];
node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:node.frame.size.width/2];
node.physicsBody.usesPreciseCollisionDetection = YES;
node.physicsBody.dynamic = YES;
node.physicsBody.categoryBitMask = CNPhysicsimage1Category;
node.physicsBody.contactTestBitMask = CNPhysicsbulletCategory;
[node attachDebugRectWithSize:node.size];
[mutableArray addObject:node];
}
return mutableArray;
}
似乎更合适,然后七次重新输入相同的东西