简体   繁体   中英

SKTextureAtlas and SKAction not getting removing let alone stopping from SKSpriteNode iOS SpriteKit

Basically I'm trying to muck around with SpriteKit, and it took me a while to figure out how to remove a series of SpriteNodes out of an Array. I conquered that, it was an issue of it still being in the ram. I'm still trying to figure out some of these strict flags on obj-c objects. First of all here is my latest stable git commit: https://github.com/kap10g/KathySprite

Right now I'm trying to figure out how to both remove the SKAction from my SKSpriteNode as well as the SKTexture. Setting them to nil does not work as I have been finding. If you download the code you will see that the animation of my walk cycle keeps running as you touch again, the intention is to only have one walk cycle on the screen at a time. The other SKSpriteNodes disappear. I'm guessing this is an issue of these SKAction's and SKTexture's still being in the RAM, but I don't have a clear understanding of how this works. I would love someone to point out the clarity of how this sort of garbage collection works. I will paste my current MyScene.m, as I am working on it now. It is slightly different than the last git update, because I committed it last time I successfully figured something out (looping the SKAction). So if you compare my code to the git you will see where I am at now.

#import "MyScene.h"

@implementation MyScene
@synthesize smoke, holder, Kathyholder, Spliffholder, spliff, spriteAction;

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        holder = [[NSMutableArray alloc] init];
        Kathyholder = [[NSMutableArray alloc] init];
        /* Setup your scene here */

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];



        myLabel.text = @"FART";
        myLabel.fontSize = 30;
        myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                       CGRectGetMidY(self.frame));
        NSString *smokePath = [[NSBundle mainBundle] pathForResource:@"MyParticle" ofType:@"sks"];
        smoke = [NSKeyedUnarchiver unarchiveObjectWithFile:smokePath];
        [self addChild:myLabel];
    }
    return self;
}

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

    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        SKSpriteNode *sprite = [smoke copy];
        SKSpriteNode *kathy = [SKSpriteNode spriteNodeWithImageNamed:@"kathy"];

        sprite.position = location;
        kathy.position = location;

        kathy.xScale = (CGFloat) random()/(CGFloat) RAND_MAX;
        kathy.yScale = (CGFloat) random()/(CGFloat) RAND_MAX;

        spliff = [SKSpriteNode spriteNodeWithTexture:WALKING_TEX_CROP_1_LARGE];



        SKAction *action = [SKAction rotateByAngle:M_PI duration:1];

        [kathy runAction:[SKAction repeatActionForever:action]];

        if ([holder count] > 0)
        {
            NSLog(@"Too long");
            SKSpriteNode *holderSprite = [holder objectAtIndex:0];
            [holder removeObject:holderSprite];
            [holderSprite removeFromParent];
            SKSpriteNode *kathySprite = [Kathyholder objectAtIndex:0];
            [Kathyholder removeObject:kathySprite];
            [kathySprite removeFromParent];
            SKSpriteNode *spliffSprite = [Spliffholder objectAtIndex:0];
            [Spliffholder removeObject:spliffSprite];
            [spliffSprite removeAllActions];
            spriteAction = nil;
            spliffSprite.texture = nil;
            [spliffSprite removeAllChildren];
            [spliffSprite removeFromParent];
        }


        [holder addObject:sprite];
        [self addChild:sprite];
        [Kathyholder addObject:kathy];
        [self addChild:kathy];
        [Spliffholder addObject:spliff];
        [self addChild:spliff];
        CGFloat spliffScale = ((CGFloat) random()/(CGFloat) RAND_MAX) * 0.5;
        spliff.xScale = spliffScale;
        spliff.yScale = spliffScale;
        spliff.position = location;
        spriteAction = [SKAction repeatActionForever:[SKAction animateWithTextures:WALKTHROUGH timePerFrame:0.033]];
        [spliff runAction:spriteAction];
    }
}

-(void)update:(CFTimeInterval)currentTime {
    /* Called before each frame is rendered */
}

@end

I'm just messing with the SpriteKit library for fun, I admit I am still a novice/intermediate to Objective-C and honestly I would probably do better with a loosely typed language like Swift, however the programmer inside of me wants to know the proper way to do things, so I'm going to try and do both and start using swift as soon as possible.

Wow I figured it out. I do not know why though. This block belongs in the init

SKTextureAtlas *walkAtlas = [SKTextureAtlas atlasNamed:@"walking"];
    NSArray *textureNames = [walkAtlas textureNames];
    _walkTextures = [NSMutableArray new];
    for (NSString *name in textureNames) {
        SKTexture *texture = [walkAtlas textureNamed:name];
        [_walkTextures addObject:texture];
    }

This block belongs in the touches area

    SKSpriteNode *walk = [SKSpriteNode spriteNodeWithTexture:[_walkTextures objectAtIndex:0]];
    walk.zPosition = 100;
    walk.scale = spliffScale;
    walk.position = location;

    [self addChild:walk];

    SKAction *walkAction = [SKAction animateWithTextures:_walkTextures timePerFrame:0.03];
    SKAction *remove = [SKAction removeFromParent];
    [walk runAction:[SKAction sequence:@[walkAction, walkAction, remove]]];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM