1
votes

Hey I am trying to move some sprites at the same time, they are all the same type of sprite.So I just used a for loop to add the sprite in the game but when I try to move all of the sprites only one moves.

I am trying to simulate running , when the user taps on the Stone the stone will move down and another stone will appear at the top there will be 4 stones so when one goes down another will come on the top and they rest of the stones will move down. I am haveing trouble moveing the stone that comes on the top of the screen that stone doesn't move like the other stones .

Here is some of the Code:

self.Stone = [Stone spriteNodeWithColor:[SKColor blackColor] size:CGSizeMake(80, 142)];


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


self.node =[self nodeAtPoint:location];

if ([self.node.name isEqualToString:@"stone"]) {
    SKAction *hey = [SKAction moveToY:self.Stone.position.y - 142 duration:1];
    [self runAction:hey];
}

They are basically going to be 4 stones on the screen and each time you tap the stones move down.

1
Hi, welcome to Stack Overflow, it helps if you provide a code example so that we can give you advice specific to your situation.mason
I still don't understand what you are trying to do. You have 4 objects (stones) at the top of the view. If you tap 1 object, all 4 are suppose to move down and out of the view and then 4 new stones appear at the top?sangony
not at the top of the view ok think of it like this there is a 4 by 4 grid on screen 1 stone is at 1 location in each row the second column is where you tap on the stones when you tap the stone all the stones including the one you tapped move down a row the one at the last row gets removed and a new stone is added to the scene at the top row. This would continue untill the player taps the background instead of the stonenmwood

1 Answers

0
votes

The name of the sprite has no real bearing on identification in regards to movement. You can either make one sprite the parent and attach all other sprites as children. Doing this will make all children move when you move the parent or you can dump all the sprites into an array and move each one separately in a loop.

Updated to include parent/child example

Below is an example of parent/child. When you tap the screen, the parent sprite is move +10 on x and y. The child will move along with the parent.

Keep in mind that the child position is in relation to the parent and not the view.

#import "MyScene.h"

@implementation MyScene
{
    SKSpriteNode *mainSprite;
}

-(id)initWithSize:(CGSize)size
{
    if (self = [super initWithSize:size])
    {
        self.physicsWorld.contactDelegate = self;

        mainSprite = [SKSpriteNode spriteNodeWithColor:[SKColor redColor] size:CGSizeMake(50, 50)];
        mainSprite.position = CGPointMake(100, 100);
        [self addChild:mainSprite];

         SKSpriteNode *child1 = [SKSpriteNode spriteNodeWithColor:[SKColor blueColor] size:CGSizeMake(20, 20)];
         child1.position = CGPointMake(40, 40);
        [mainSprite addChild:child1];
    }
    return self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
     mainSprite.position = CGPointMake(mainSprite.position.x+10, mainSprite.position.y+10);
}

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

@end