[英]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.