SKTexture get image name - ios

Is there possible to get current image name from SKTexture from SKSpriteNode?
I am a new in SpriteKit and I'm writing a little game. I need to detect when ninja will hit enemy. Something like this
I am doing SKAction like
- (void)setUpHit
{
SKTextureAtlas *hitAtlas = [SKTextureAtlas atlasNamed:#"Ninja_hit"];
SKTexture *hit1 = [hitAtlas textureNamed:#"Ninja_hit_1"];
SKTexture *hit2 = [hitAtlas textureNamed:#"Ninja_hit_2"];
SKTexture *hit3 = [hitAtlas textureNamed:#"Ninja_hit_3"];
SKTexture *hit4 = [hitAtlas textureNamed:#"Ninja_hit_4"];
SKAction *hitAnimation = [SKAction animateWithTextures:#[hit1, hit2, hit3, hit4]
timePerFrame:0.1];
SKAction *wait = [SKAction waitForDuration:0.3];
SKAction *goBack = [SKAction animateWithTextures:#[hit1, [SKTexture textureWithImageNamed:#"Ninja"]]
timePerFrame:0.1];
self.hitAction = [SKAction sequence:#[hitAnimation, wait, goBack]];
}
But hit is going only on images Ninja_hit_2.png or Ninja_hit_3.png.
So I need to detect current texture image name when i am doing intersectsNode ninja with enemy.
Now I am doing something like
if ([ninja intersectsNode:node] && !self.isKilled)
{
SKTexture *currentTexture = [ninja texture];
if ([self isHit:currentTexture])
{
//kill enemy here
}
}
where
- (BOOL)isHit:(SKTexture *)texture
{
NSString *description = [texture description];
NSRange range = [description rangeOfString:#"'"];
NSString *textureName = [description substringFromIndex:NSMaxRange(range)];
range = [textureName rangeOfString:#"'"];
textureName = [textureName substringToIndex:NSMaxRange(range) - 1];
if ([textureName isEqualToString:#"Ninja_hit_2.png"] ||
[textureName isEqualToString:#"Ninja_hit_3.png"])
{
return YES;
}
return NO;
}
I know that is not correct, but I can't find how to take current texture name or doing it correctly. Could you help me?

Hmm my strategy would be this:
On your ninja class have a property for your textures. Keep them around
#property (nonatomic, strong) NSArray * hitTextures;
Then store the hit textures (note this is a lazy loading getter method)
-(NSArray *)hitTextures{
if (_hitTextures == nil){
SKTexture *hit1 = [SKTexture textureWithImageNamed:#"Ninja_hit_1"];
SKTexture *hit2 = [SKTexture textureWithImageNamed:#"Ninja_hit_2"];
SKTexture *hit3 = [SKTexture textureWithImageNamed:#"Ninja_hit_3"];
SKTexture *hit4 = [SKTexture textureWithImageNamed:#"Ninja_hit_4"];
_hitTextures = #[hit1, hit2, hit3, hit4];
}
return _hitTextures;
}
Note that we don't need to make the SKTextureAtlas object explicitly:
When loading the texture data, Sprite Kit searches the app bundle for an image file with the specified filename. If a matching image file cannot be found, Sprite Kit searches for the texture in any texture atlases stored in the app bundle. If the specified image does not exist anywhere in the bundle, Sprite Kit creates a placeholder texture image.
Use this texture array to fill in your SKAction
SKAction *hitAnimation = [SKAction animateWithTextures:self.hitTextures
timePerFrame:0.1];
This allows you do change your -isHit method like so:
- (BOOL)isHit:(SKTexture *)texture
{
NSUInteger index = [self.hitTextures indexOfObject:texture];
if (index == 1 ||
index == 2)
{
return YES;
}
return NO;
}
This way you don't rely on an implementation detail of the -description method that is subject to change.

to detect the current name out of a SKAtlas Animation, this is not the best way but it works for me.
var tempstr = yoursprite.texture!.description
if(tempstr.rangeOfString("currentFrameName", options: NSStringCompareOptions.LiteralSearch, range: nil, locale: nil) != nil){
// do something
}

Look at the texture description of the SKSpriteNode and you should be able to see the name of the image
for node in self.children {
if node is SKSpriteNode && node.physicsBody != nil {
let spriteNode:SKSpriteNode = node as! SKSpriteNode
print("sprite node image name \(spriteNode.texture!.description)")
}
}
an example of this will print out on debug
sprite node image name 'Clear10x10' (10 x 10)
You can then RegX the third field for the image name, 'Clear10x10' in this example. Note that Apple may change this description format.

Related

Sprite Kit device lags on first collision

I have a problem in which my Sprite Kit game lags only when the first collision occurs between the main character and any other sprite. After the first collision occurs, every other collision is smooth and runs at 60.0 fps. The odd thing is that when the first collision, the fps only drops to 49-51, but the actual game freezes for a half second. This is also not an issue of setup lag as this occurs no matter how long I wait to start. Does anyone know what the issue is?
-(void)checkForCollisions {
if (_invincible) return;
[self enumerateChildNodesWithName:#"enemy"
usingBlock:^(SKNode *node, BOOL *stop){
SKSpriteNode *enemy = (SKSpriteNode *)node;
CGRect smallerFrame = CGRectInset(enemy.frame, 22, 22);
if (CGRectIntersectsRect(smallerFrame, _sloth.frame)) {
[enemy removeFromParent];
[self runAction:[SKAction playSoundFileNamed:#"eagle.wav" waitForCompletion:NO]];
NSString *burstPath =
[[NSBundle mainBundle] pathForResource:#"ExplosionParticle" ofType:#"sks"];
SKEmitterNode *burstEmitter =
[NSKeyedUnarchiver unarchiveObjectWithFile:burstPath];
burstEmitter.position = _sloth.position;
[self addChild:burstEmitter];
[self changeInLives:-1];
_invincible = YES;
float blinkTimes = 10;
float blinkDuration = 3.0;
SKAction *blinkAction =
[SKAction customActionWithDuration:blinkDuration
actionBlock:
^(SKNode *node, CGFloat elapsedTime) {
float slice = blinkDuration / blinkTimes;
float remainder = fmodf(elapsedTime, slice);
node.hidden = remainder > slice / 2;
}];
SKAction *sequence = [SKAction sequence:#[blinkAction, [SKAction runBlock:^{
_sloth.hidden = NO;
_invincible = NO;
}]]];
[_sloth runAction:sequence];
}
}];
}
The lag is not linked to the emitter node as the game still lags whenever it is commented out.
Let me know if you need any additional information. Thanks in advance!
Here is a link for my Instruments's trace file: https://www.dropbox.com/sh/xvd1xdti37d76au/ySL4UaHuOS
If you look at the trace file, note that the collision occurs when the Time Profiler reaches 118%.
As Cocos mentioned, it is probably the initial loading of your sound file which is causing the one time delay.
In your implementation add the sound action:
SKAction *eagleSound;
In your init method add this:
eagleSound = [SKAction playSoundFileNamed:#"eagle.wav" waitForCompletion:NO];
Then whenever you need to play the sound, use this:
[self runAction:eagleSound];

How to generate SKTexture from a modified SKSpriteNode?

So I have an SKSpriteNode which has undergone two SKActions which modify it's shape and coloring however I want to return the modified SKSpriteNode as an SKTexture which I can apply to another SKSpriteNode. This is what I'm currently using.
1) Generate the "modified" laserNode and store it as a property
-(void)generateLaserSprite
{
SKSpriteNode *laserNode = [SKSpriteNode spriteNodeWithTexture:[SKTexture textureWithImageNamed:#"Laser.png"]];
[laserNode runAction:[SKAction group:#[[SKAction colorizeWithColor:[SKColor redColor] colorBlendFactor:100 duration:0.0],
[SKAction scaleXBy:0.4 y:0.7 duration:0.0],
]]];
_laserSprite = laserNode
}
2) Then a method from the SKScene calls this method to retrieve a copy of the property
-(SKSpriteNode*)retrieveLaserSprite
{
SKSpriteNode *laserNode = [_laserSprite copy];
laserNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(laserNode.size.width*0.8, laserNode.size.height*0.8)];
//other laserNode.physicsBody modifications
return laserNode;
The problem with this currently is that when I call the second method (retrieveLaserSprite), the returned LaserNode then shows up on screen as the original image (pre-SKAction), and then you can visibly see the SKActions take place on-screen.
It should actually be possible to create a texture from a sprite whose rendering properties have been modified using the -(SKTexture *)textureFromNode: method of the SKView class.
Your code would go like this:
-(SKSpriteNode*)retrieveLaserSprite {
SKTexture *tempTexture = [self.view textureFromNode:_laserSprite];
SKSpriteNode *laserNode = [SKSpriteNode spriteNodeWithTexture:tempTexture];
laserNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(laserNode.size.width*0.8, laserNode.size.height*0.8)];
//other laserNode.physicsBody modifications
return laserNode;
}
You should be able to accomplish this task using something like:
-(SKSpriteNode*)retrieveLaserSprite
{
SKTexture *texture = [self.view textureFromNode:[_laserSprite copy]];
SKSpriteNode *laserNode = [SKSpriteNode spriteNodeWithTexture:texture];
laserNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:CGSizeMake(laserNode.size.width*0.8, laserNode.size.height*0.8)];
//other laserNode.physicsBody modifications
return laserNode;
}

How to store position in spritekit

I need to change the character appearance during the game. My character define by a node:
SKSpriteNode* _character;
....
SKTexture* tmp = [SKTexture textureWithImageNamed:string];
tmp.filteringMode = SKTextureFilteringNearest;
[textureArray addObject:tmp];
_character = [SKSpriteNode spriteNodeWithTexture:tmp];
Then under a certain condition in the game, if I check it satisfied, I will change the appearance by:
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if (condition_satisfied) {
...
_store.position = _character.position;
[_character removeFromParent];
...
//load new character texture here, I won't post the code, for short. Then
....
_character.position = _store.position;
...
[self addChild:_character];
}
I use a store node to store the position, because my character's position will change during the game by human touch. But in the game, it will always cause crash, collision... So I guess that is not the right way to restore the right position of character.
How should I do here ?
To change a texture just set SKSpriteNode's texture property:
_character.texture = [SKTexture textureWithImageNamed:#"image"];
Or use SKAction, if you want to add some fading effect when texture is changing:
SKAction *fadeOut = [SKAction fadeOutByDuration:0.3f];
SKAction *fadeIn = [SKAction fadeInByDuration:0.3f];
SKAction *changeTexture = [SKTexture setTexture:[SKTexture textureWithImageNamed:#"image"]];
[_character runAction:[SKAction sequence:#[fadeOut,changeTexture,fadeIn]]];
There is no need to removeFromParent the node.
If you need to store node's position, it's better to store it in #property:
#interface GameScene()
#property (nonatomic) CGPoint characterPosition;
#end
#implementation GameScene
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if (condition_satisfied) {
...
self.characterPosition = _character.position;
[_character removeFromParent];
...
//load new character texture here, I won't post the code, for short. Then
....
_character.position = self.characterPosition;
...
[self addChild:_character];
}
#end
Instead of creating a SKSpriteNode to store the position of the node, you can simply do so in a CGPoint variable.
-(void)update:(CFTimeInterval)currentTime {
/* Called before each frame is rendered */
if (condition_satisfied) {
...
CGPoint characterPosition = _character.position;
[_character removeFromParent];
...
//load new character texture here, I won't post the code, for short. Then
....
_character.position = characterPosition;
...
[self addChild:_character];
}

Stop SKAction that RepeatsForever - Sprite Kit

I want to run two animations on my spriteNode depending on its rotation. If the value is negative run one of the animations, if it's positive run the other. And I managed to do that (kind of) but I have a problem. If Animation1 is running, and zRotation changes to positive, they both run because they are repeating forever. So I did this :
NSMutableArray *walkingTextures = [NSMutableArray arrayWithCapacity:14];
for (int i = 1; i < 15; i++) {
NSString *textureName =
[NSString stringWithFormat:#"character%d", i];
SKTexture *texture =
[SKTexture textureWithImageNamed:textureName];
[walkingTextures addObject:texture];
}
SKAction *spriteAnimation = [SKAction animateWithTextures:Textures timePerFrame:0.04];
repeatWalkAnimation = [SKAction repeatActionForever:spriteAnimation];
[sprite runAction:repeatWalkAnimation withKey:#"animation1"];
and then when I want it to stop :
[self removeActionForKey:#"animation1"];
but it keeps running the action, how can I stop the action, then? Thank you!
The method is supposed to be called on the node which the SKAction is running on.
Change
[self removeActionForKey:#"animation1"];
to
[sprite removeActionForKey:#"animation1"];

SKTexture from UIImage looks different

I'm playing around with SpriteKit and I'm creating a SKTexture from an UIImage, then using SKSpriteNode to add the child to my SKScene (As a background), everything works fine, except that the UIImage looks very different from the original image, I tried to recreate the image in photoshop and the issue still remains, tested on Simulator and real device and different image colors, no changes.
The image format is PNG, and I added through Images.xcassets in Xcode 5
Image, results:
Different image, results:
I'm using the following code in my SKScene subclass:
- (id)initWithSize:(CGSize)size
{
if (self = [super initWithSize:size]) {
SKTexture *textureGradient = [SKTexture textureWithImage:[UIImage imageNamed:#"Image"]];
SKSpriteNode* spriteGradient = [SKSpriteNode spriteNodeWithTexture:textureGradient];
[self addChild:spriteGradient];
}
return self;
}
What I'm doing wrong?
Any help would be appreciated. :)
Solution:
It was my fault, the image position was wrong, so I changed the position property:
SKTexture *textureGradient = [SKTexture textureWithImage:[UIImage imageNamed:#"Image"]];
SKSpriteNode* spriteGradient = [SKSpriteNode spriteNodeWithTexture:textureGradient];
spriteGradient.position = CGPointMake(CGRectGetMidX(self.frame),CGRectGetMidY(self.frame));
[self addChild:spriteGradient];

Resources