![](/img/trans.png)
[英]iOS, SpriteKit convert SKTextureAtlas/SKTexture into UIImage
[英]SKTextureAtlas and SKAction not getting removing let alone stopping from SKSpriteNode iOS SpriteKit
基本上,我试图弄乱SpriteKit,花了我一段时间才弄清楚如何从数组中删除一系列SpriteNode。 我征服了,这仍然是公羊的问题。 我仍在尝试找出obj-c对象上的一些严格标志。 首先,这是我最新的稳定git commit: https : //github.com/kap10g/KathySprite
现在,我正在尝试找出如何从我的SKSpriteNode和SKTexture中删除SKAction。 如我所知,将它们设置为nil无效。 如果下载代码,您将看到我的步行周期的动画在您再次触摸时保持运行,目的是一次仅在屏幕上显示一个步行周期。 其他SKSpriteNodes消失。 我猜这是因为这些SKAction和SKTexture仍在内存中的问题,但是我对它的工作原理尚不明确。 我希望有人指出这种垃圾收集是如何工作的。 现在,我将粘贴当前的MyScene.m。 它与上次git更新略有不同,因为我上次成功解决某个问题(循环SKAction)后便提交了它。 因此,如果将我的代码与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
我只是把SpriteKit库弄得很有趣,我承认我还是Objective-C的新手/中级,老实说,我可能会更好地使用像Swift这样的松散类型的语言,但是我内心的程序员想知道正确的做事方式,所以我将尝试同时做这两种事情,并尽快开始使用swift。
哇,我想通了。 我不知道为什么。 该块属于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];
}
该块属于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.