我正在为iOS7开发一款Spritekit游戏,我想让一系列硬币从上到下从屏幕上掉下来。我熟悉这样一个事实,即拥有一系列项目,通常首选数组,但我不熟悉数组。为了使硬币不断下降,这就是我设置的:
-(void)testCoins {
testCoin = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:@"catCoin.png"] size:CGSizeMake(26.05, 25.5)];
testCoin.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:12];
testCoin.physicsBody.affectedByGravity = NO;
testCoin.physicsBody.categoryBitMask = CollisionDebris;
testCoin.name = @"coin";
RandomPosition = arc4random() %260*DoubleIfIpad;
RandomPosition = RandomPosition + 20*DoubleIfIpad;
testCoin.position = CGPointMake (RandomPosition, self.size.height + 40*DoubleIfIpad);
[self addChild:testCoin];
SKAction *moveDown = [SKAction moveByX:0 y:-self.size.height+100 duration:10.0];
[testCoin runAction:moveDown];
[self runAction:[SKAction sequence:@[
[SKAction waitForDuration:2.0],
[SKAction performSelector:@selector(testCoins) onTarget:self],
]]];
if (testCoin.position.y < CGRectGetMinY(self.frame)) {
[testCoin removeFromParent];
}
}
一切正常,硬币正确生成并正确地沿着屏幕流下来。问题实际上在于联系人,因为我用于联系的if语句告诉程序删除硬币,然后添加一个点。这样做,但它总是在屏幕上保留一个硬币并且不会将其移除,但是如果玩家继续点击可见硬币,它将添加点,并从屏幕上移除其他硬币。我认为这是因为我正在使用performSelector
调用来继续重复该方法,因此它会在屏幕上保留一枚硬币。虽然我错了,但我完全糊涂了。
有更好的方法吗?也许额外的方法额外的硬币???
如果有人做了类似的事情,请你指导我正确的方向,无论是添加阵列等。
提前致谢,如果需要任何额外信息,请告知我们。
答案 0 :(得分:1)
它简单的sprite工具包为我们提供了一种独特的方法来搜索节点我的名字“enumerateChildNodesWithName”
请查看下面的示例,我认为它适用于您,而您不需要数组
#import "milkhuntMyScene.h"
@implementation milkhuntMyScene
{
int balloonId;
int aniId;
int removeId;
}
-(id)initWithSize:(CGSize)size {
if (self = [super initWithSize:size]) {
/* Setup your scene here */
[self FallBalloons:20];
}
return self;
}
-(void)FallBalloons:(int)total
{
for(int i=0;i<total;i++)
{
balloonId+=1;
SKSpriteNode *balloon=[SKSpriteNode spriteNodeWithImageNamed:@"balloon1.png"];
[self addChild:balloon];
balloon.position=CGPointMake(50+(i*balloon.size.width), 700);
balloon.name=[NSString stringWithFormat:@"Balloon%d", balloonId];
}
SKAction *aniTime= [SKAction sequence:@[
[SKAction waitForDuration:0.2],
[SKAction performSelector:@selector(aniBalloon)
onTarget:self]
]];
[self runAction:[SKAction repeatAction:aniTime count:total]];
}
-(void)aniBalloon
{
aniId+=1;
[self enumerateChildNodesWithName:[NSString stringWithFormat:@"Balloon%d", aniId] usingBlock:^(SKNode *node, BOOL *stop) {
[node runAction:[SKAction moveToY:50 duration:1]];
SKAction *removeTime= [SKAction sequence:@[
[SKAction waitForDuration:1.1],
[SKAction performSelector:@selector(removeBalloon)
onTarget:self]
]];
[self runAction:removeTime];
}];
}
-(void)removeBalloon
{
removeId+=1;
[self enumerateChildNodesWithName:[NSString stringWithFormat:@"Balloon%d", removeId] usingBlock:^(SKNode *node, BOOL *stop) {
[node removeFromParent];
}];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
/* Called when a touch begins */
}
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
}
@end