我有一个名为“sprite”的节点,当我触摸屏幕时它在特定的 x 轴上显示 1 秒,在“sprite”节点消失之后,我想让屏幕上只有两个“sprite”节点同时,他们不能在同一个地方.. 因为现在我可以添加“sprite”节点,就像我的手指可以在屏幕上点击一样多。这是我现在得到的:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 60);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[self addChild:sprite];
}
}
已编辑
@implementation GameScene
SKNode *groupSprite;
-(void)didMoveToView:(SKView *)view{
groupSprite = [[SKNode alloc]init];
[self addChild:groupSprite];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
if (groupSprite.children.count < 2) {
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 75);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.physicsBody.categoryBitMask = boardCategory;
sprite.physicsBody.contactTestBitMask = ballCategory;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
};
最佳答案
您可以像@kaizoku 指出的那样添加一个数组,但这只是一个 float 的变量,使代码可能难以管理。更好的方法是使用 SKNode 创建一个组,然后检查组数。
(我的 objective-c 有点生疏,所以请多多包涵)
...
SKNode *groupSprite = [[SKNode alloc] init];
...
-(void)didMoveToView:(SKView *)view
{
self.addChild(groupSprite);
}
...
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches)
{
if(groupSprite.children.count < 2)
{
CGPoint touchlocation = [touch locationInNode:self];
CGPoint location = CGPointMake(touchlocation.x, 60);
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"board_ipad@2x"];
sprite.xScale = 0.4;
sprite.yScale = 0.6;
sprite.position = location;
sprite.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:sprite.frame.size];
sprite.physicsBody.dynamic = NO;
sprite.name = boardCategoryName;
SKAction *delay = [SKAction waitForDuration:1];
SKAction *remove = [SKAction removeFromParent];
SKAction *actionSequence = [SKAction sequence:@[delay,remove]];
[sprite runAction:actionSequence];
[groupSprite addChild:sprite];
}
}
}
之所以要这样做,是因为它使您的代码更具可读性。你有一个场景,有一组 Sprite ,其中包含 Sprite 。现在,如果出于某种原因你想从屏幕上删除所有 Sprite 。您只需将其从组中移除,无需担心将其从场景中移除以及从数组中移除。
关于ios - 如何限制触摸时的节点外观,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34797665/