我正在创建一个应用程序,其中的对象从屏幕顶部掉落。这些对象是spriteNodes,使用方法- (void)generateNodes
生成。当您点击屏幕时,会创建一个touchPointer节点并检测您是否触摸了spriteNode。这是通过didBeginContact方法完成的。我有触摸的检测,但是,当我尝试向对象添加死亡动画时,存在问题。如果我点击一个对象并开始它的死亡动画,那么我点击另一个新生成的对象。之前的死亡动画继续使用新生成的对象。因此,不是启动新生成的对象自己的死亡动画 - 基本上淡出,它将继续从该对象完成后的前一个对象的死亡动画。
示例:
点击0.5时, firstObject
alpha - > 0
==第一个对象被点击,而firstObject仍在运行死亡动画==
点击时, secondObject
alpha为0.2 - > 0
这是因为当我点击secondObject时,它会继续第一个对象的动画。我希望它能够成为secondObject开始自己动画的地方,它应该从0.5
的alpha开始代码didBeginContact
if([contact.bodyA.node.name isEqualToString:@"object"] && [contact.bodyB.node.name isEqualToString:@"touchPointer"]){
//Object Death Animation Actions
int randObjectMovement = (arc4random()%2) + 1;
touchedObject = [objects objectAtIndex:0];
touchedObject.alpha = 0.5;
if(randObjectMovement == 1){
touchedObject.physicsBody.velocity = CGVectorMake(0, 0);
[touchedObject.physicsBody applyImpulse:CGVectorMake(-10, 18)];
NSLog(@"Impluse completed");
}else{
touchedObject.physicsBody.velocity = CGVectorMake(0, 0);
[touchedObject.physicsBody applyImpulse:CGVectorMake(10, 18)];
NSLog(@"Impluse completed");
}
objectFadeOut = [SKAction fadeAlphaTo:0 duration:0.75];
objectRemove = [SKAction removeFromParent];
objectDeathRotation = [SKAction rotateByAngle:M_PI*2 duration:3];
[touchedObject runAction:objectDeathRotation completion:^{
NSLog(@"objectDeathRotation completed");
}];
[touchedObject runAction:objectFadeOut completion:^{
NSLog(@"objectFadeOut and Removal completed");
[touchedObject runAction:objectRemove];
}];
}
代码generateObjects
- (void)generateObject{
//Object Texture delcaration
if ([self generateObjectType] <= 100 && [self generateObjectType] > 20){
objectTexture = [SKTexture textureWithImageNamed:@"Object_N"];
}else if([self generateObjectType] <= 20 && [self generateObjectType] > 5){
objectTexture = [SKTexture textureWithImageNamed:@"Object_B"];
}else{
objectTexture = [SKTexture textureWithImageNamed:@"Object_G"];
}
objectTexture.filteringMode = SKTextureFilteringNearest;
//Object Actions
objectRotation = [SKAction rotateByAngle:M_PI*2 duration:1];
//Object Sprite declaration
objectSprite = [SKSpriteNode spriteNodeWithTexture:objectTexture];
objectSprite.position = [self generateObjectPosition];
objectSprite.zPosition = 3;
objectSprite.size = CGSizeMake(30, 30);
[objectSprite runAction:objectRotation];
objectSprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:objectSprite.size];
objectSprite.name = @"object";
objectSprite.physicsBody.categoryBitMask = objectCategory;
objectSprite.physicsBody.collisionBitMask = groundCategory;
objectSprite.physicsBody.contactTestBitMask = groundCategory | touchCategory;
[objects insertObject:objectSprite atIndex:0];
[self addChild:objectSprite];
}
顺便说一句,对象是一个NSMutableArray。
请帮忙!
答案 0 :(得分:0)
为什么不使用联系人中的节点:
SKSpriteNode *touchedObject = (SKSpriteNode*)contact.bodyA.node;
这会使节点参与碰撞,您可以对其执行操作,不需要阵列。
<强>更新强>
使用操作的本地实例:
SKAction *objectFadeOutLocal = [SKAction fadeAlphaTo:0 duration:0.75];
SKAction *objectRemoveLocal = [SKAction removeFromParent];
SKAction *objectDeathRotationLocal = [SKAction rotateByAngle:M_PI*2 duration:3];