1
votes

i will explain a part of my code. I have Spritenodes (images) who are moving down on the screen.

SKTexture* Squaretexture = [SKTexture textureWithImageNamed:@"squaregreen"];
SquareTexture.filteringMode = SKTextureFilteringNearest;
Square = [SKSpriteNode spriteNodeWithTexture:SquareTexture];
Square.name = @"square";
.
.
.

[_objects addChild:Square];

_objects is a SKNode and Square is a SKSpriteNode. Now there is my code: every one second there is one square, who came from "over the screen" and is moving to the bottom. (Also there are more then one squares on the screen).

Now I want this: When I touch a square it should be "deleted" or hidden, but only the one who i touch. With my code, when i touch all squares are deleted or nothing. I tried with removefromparent and removechild, but i couldn't solve it.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    /* Called when a touch begins */

    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInNode: self];
    SKNode *node = [self nodeAtPoint:location];

    NSLog(@"Point in myView: (%f,%f)", location.x, location.y);
    if ([node.name isEqualToString:@"Square"]) {
        [Square removeFromParent];
        [Square removeAllChildren];
    }    
}

Do you have a suggestion how can I do it? Thanks for Answers. Mehmet

1
What Quadrat carries?, isn't it should be [node removeFromParent]; ??BaSha
I corrected to square. I tried it with removeFromParent, but it isn't working..Mehmet63
ok.. i feel issue is, you are adding new Square every second to same Square so, by [Square removeFromParent]; only last assigned node to Square is getting removed, which may not one you tapped..BaSha
yes, every second i add a new SpriteNode (Square) to Node (objects)Mehmet63

1 Answers

1
votes

You almost had it right. The trick is that you need to have a unique identifier for each object (sprite) that you create and then store those objects in an array for later use.

The code below creates 5 sprites and gives them unique names: Sprite-1, Sprite-2, etc...

Whenever a touch is registered, it extracts the touched node's name, searches the array for the matching object, removes the object from the view and lastly removes the object from the array.

Note that my sample code is based on landscape view.

#import "MyScene.h"

@implementation MyScene
{
    NSMutableArray *spriteArray;
    int nextObjectID;
}

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        spriteArray = [[NSMutableArray alloc] init];
        nextObjectID = 0;

        // create 5 sprites
        for (int i=0; i<5; i++)
        {
             SKSpriteNode *mySprite = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(30, 30)];
             nextObjectID ++; // increase counter by 1
             mySprite.name = [NSString stringWithFormat:@"Sprite-%i",nextObjectID]; // add unique name to new sprite
             mySprite.position = CGPointMake(50+(i*70), 200);
            [spriteArray addObject:mySprite];
            [self addChild:mySprite];
        }
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     UITouch *touch = [touches anyObject];
     CGPoint location = [touch locationInNode: self];
     SKNode *node = [self nodeAtPoint:location];

     NSLog(@"touched node name: %@",node.name);
     NSLog(@"objects in spriteArray: %lu",(unsigned long)[spriteArray count]);

     NSMutableArray *discardedItems = [NSMutableArray array];
     for(SKNode *object in spriteArray)
     {
        if([object.name isEqualToString:node.name])
        {
           [object removeFromParent];
           [discardedItems addObject:object];
        }
    }
    [spriteArray removeObjectsInArray:discardedItems];

    NSLog(@"objects in spriteArray: %lu",(unsigned long)[spriteArray count]);

}

-(void)update:(CFTimeInterval)currentTime
{
    //
}

@end