简体   繁体   English

SKTextureAtlas和SKAction并未删除,更不用说从SKSpriteNode iOS SpriteKit停止了

[英]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. 基本上,我试图弄乱SpriteKit,花了我一段时间才弄清楚如何从数组中删除一系列SpriteNode。 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. 我仍在尝试找出obj-c对象上的一些严格标志。 First of all here is my latest stable git commit: https://github.com/kap10g/KathySprite 首先,这是我最新的稳定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. 现在,我正在尝试找出如何从我的SKSpriteNode和SKTexture中删除SKAction。 Setting them to nil does not work as I have been finding. 如我所知,将它们设置为nil无效。 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. 其他SKSpriteNodes消失。 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. 我猜这是因为这些SKAction和SKTexture仍在内存中的问题,但是我对它的工作原理尚不明确。 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. 现在,我将粘贴当前的MyScene.m。 It is slightly different than the last git update, because I committed it last time I successfully figured something out (looping the SKAction). 它与上次git更新略有不同,因为我上次成功解决某个问题(循环SKAction)后便提交了它。 So if you compare my code to the git you will see where I am at now. 因此,如果将我的代码与git进行比较,您将看到我现在的位置。

#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. 我只是把SpriteKit库弄得很有趣,我承认我还是Objective-C的新手/中级,老实说,我可能会更好地使用像Swift这样的松散类型的语言,但是我内心的程序员想知道正确的做事方式,所以我将尝试同时做这两种事情,并尽快开始使用swift。

Wow I figured it out. 哇,我想通了。 I do not know why though. 我不知道为什么。 This block belongs in the init 该块属于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 该块属于touches区域

    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]]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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