Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top