0

Im creating an spark affect for a bomb that rotates and scrolls across the screen using SKEmitterNode. I've come to a sticking point as i can't correctly position the spark effect. I can get the SKEmitterNode and spriteNode to move across the screen together with the spark affect positioned in the centre of the bomb while the bomb rotates. The effect i wish to achieve is the spark positioned correctly at the end of the wick on the bomb while the bomb rotates.

The first piece of code is from myScene:

MCBomb *_multiUp = [MCBomb spriteNodeWithImageNamed:@"MultiShotPowerUp"];    
[_mainLayer addChild:_multiUp];

// Create spark.
NSString *bombSparkPath = [[NSBundle mainBundle] pathForResource:@"BombSpark" ofType:@"sks"];
SKEmitterNode *_bombSpark = [NSKeyedUnarchiver unarchiveObjectWithFile:bombSparkPath];
_bombSpark.targetNode = _mainLayer;
[_mainLayer addChild:_bombSpark];
_multiUp.spark = _bombSpark;

The next two pieces of code are from the class .m

-(void)updateSpark {
if (self.spark) {
    self.spark.position = self.position;
}

and .h

@interface MCBomb : SKSpriteNode
@property (nonatomic) SKEmitterNode *spark;
-(void)updateSpark;

As soon as i add CGPointMake into the mix e.g. self.spark.position = CGPointMake(0.9, 0.9);

when called the spriteNode floats across the screen and the SKEmitterNode appear bottom left corner static.

Or if i do something like:

self.spark.position = CGPointMake (self.size.width +50 , self.size.height + 400);

The SKEmitterNode is static in its new position and the spriteNode floats across the screen. Or if i do this:

self.spark.position = CGPointMake (self.size.width +50 , self.size.height + 400);
    self.position = self.spark.position;

I get both spriteNode and EmitterNode static in there new position.

As you can see, In my frustration i'm clutching at straws! The positioning is extreme just for affect, I'm still learning and could do with some advice on how to get the correct affect.

Below is the complete class, please see new comment:

-(void)spawnMultiShotPowerUp {
// Setting up Texture Atlas.
SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"Parts"];

MCBomb *_multiUp = [MCBomb spriteNodeWithImageNamed:@"MultiShotPowerUp"];
_multiUp.name = @"multiUp";
_multiUp.position = CGPointMake(-_multiUp.size.width, randomInRange(150, self.size.height -100));
_multiUp.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:16.0];
_multiUp.physicsBody.categoryBitMask = mCMultiUpCategory;
_multiUp.physicsBody.collisionBitMask = 0;
_multiUp.physicsBody.velocity = CGVectorMake(10, randomInRange(-40, 40));
_multiUp.physicsBody.angularVelocity = M_PI;
_multiUp.physicsBody.linearDamping = 0.0;
_multiUp.physicsBody.angularDamping = 0.0;
[_mainLayer addChild:_multiUp];

// Create spark.
NSString *bombSparkPath = [[NSBundle mainBundle] pathForResource:@"BombSpark" ofType:@"sks"];
SKEmitterNode *_bombSpark = [NSKeyedUnarchiver unarchiveObjectWithFile:bombSparkPath];
_bombSpark.targetNode = _mainLayer;
_bombSpark.position = CGPointMake(20, 0);
_multiUp.spark = _bombSpark;
[_multiUp addChild:_bombSpark];

}

Coxy
  • 3
  • 3
  • You shouldn't edit an answer to expand on your question or ask an additional question. If my answer provided an answer to your question, check it as be correct. If you wish to expand your question, within its original scope, edit your question. If you have another question you should ask another question and not edit your current one to include it. – sangony Apr 26 '15 at 22:22
  • The spark affect behaviour has change dramatically, The spark now circles the bomb like the moon around the earth! I'm unable to place the node in the correct position, when the bomb randomly enters the scene if it's moving up, the distance between the 2 nodes increases, down and it decreases, so much so as the bomb node is moving off the bottom of the page the spark is virtually in the exact correct spot? I have edited my original question an added the complete class. Please explain this bizarre behaviour. – Coxy Apr 27 '15 at 14:00
  • Try taking out the line _bombSpark.targetNode = _mainLayer; and see what happens. – sangony Apr 27 '15 at 14:16
  • Yes, after you initial answer I did try this but the behaviour is still the same the only thing thats altered is the spark is denser like its duplicated, one on top of the other. – Coxy Apr 27 '15 at 16:32

1 Answers1

0

You have these two nodes you are creating:

MCBomb *_multiUp = [MCBomb spriteNodeWithImageNamed:@"MultiShotPowerUp"];    
[_mainLayer addChild:_multiUp];

// Create spark.
NSString *bombSparkPath = [[NSBundle mainBundle] pathForResource:@"BombSpark" ofType:@"sks"];
SKEmitterNode *_bombSpark = [NSKeyedUnarchiver unarchiveObjectWithFile:bombSparkPath];
_bombSpark.targetNode = _mainLayer;
[_mainLayer addChild:_bombSpark];

but you are adding your spark to the mainLayer which is not correct for what you are trying to do. You need to add the spark as a child to your bomb instead of the mainLayer.

[_multiUp addChild:_bombSpark];

Keep in mind that the _bombSpark positioning has now changed because it is a child of _multiUp. That means _bombSpark's position 0,0 equals the center position of the _multiUp node.

Add a position to _bombSpark like this:

_bombSpark.position = CGPointMake(20,0);

That puts the spark 20 points up from the center of _multiUp. Play around with the exact x,y coordinate values until you get it to where it has to be.

sangony
  • 11,636
  • 4
  • 39
  • 55