基本上我正在尝试使用SpriteKit,我花了一些时间来弄清楚如何从数组中删除一系列SpriteNodes。我征服了它,这是一个仍然在公羊的问题。我还在试图找出obj-c对象上的一些严格标志。首先,这是我最新的稳定git提交:https://github.com/kap10g/KathySprite
现在我正试图弄清楚如何从我的SKSpriteNode以及SKTexture中删除SKAction。将它们设置为nil不起作用,因为我一直在寻找。如果您下载代码,您将看到我的步行周期的动画在您再次触摸时保持运行,目的是一次只在屏幕上有一个步行周期。其他SKSpriteNodes消失。我猜这是SKAction的一个问题,SKTexture仍然在RAM中,但我不清楚它是如何工作的。我希望有人能指出这种垃圾收集的清晰度。我将粘贴当前的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。
答案 0 :(得分:0)
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];
}
此块属于触摸区域
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]]];