Reputation: 235
I have node named "sprite" and when i touch screen it shows up for 1 sec on specific x axis, after that "sprite" node disappear, i want to make that on screen can be only two "sprite" nodes at the same time and they can't be on same spot.. Because for now i can add "sprite" nodes as much as my fingers can tap on screen. here what i got for now:
-(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];
}
}
Edited
@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];
};
Upvotes: 0
Views: 58
Reputation: 16837
You could add an array like @kaizoku has pointed out, but that is just a variable that is floating out there making the code possibly unmanageable. A better approach would be to use an SKNode to create a group, then check the group count.
(My objective c is a bit rusty, so please bear with me)
...
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];
}
}
}
The reason you want to do it this way, is because it makes your code more readable. You have a scene, with a group of sprites, that contains sprites. Now if for some reason you want to remove all the sprites from the screen. You just remove it from the group, you do not need to worry about removing it from the scene AND removing it from an array.
Upvotes: 2
Reputation: 797
Create an Array of SKSpriteNode type.
var yourArray = [SKSpriteNode]
When you touch, add the new node in this array.
yourArray.append(sprire)
When your touch function is executed, check if the array size is more than two. If it is, do not do anything.
if yourArray.count < 2 { //Your code }
Upvotes: 1