改变spritekitnode图像

时间:2014-03-29 03:51:00

标签: xcode image ipad sprite-kit

我正在为Xcode做一些新的sprite工具包教程,但是对于下一部分,我需要更改许多节点的图像。例如,我每次点击一个按钮时都需要更改三个图像。我发现这样做的唯一方法是每次更新时添加addChild,但这会使程序运行得太慢。

@property (nonatomic, strong) SKSpriteNode * button;
@property (nonatomic, strong) JSTileMap *map;
@property (nonatomic, strong) Player *player;
@property (nonatomic, assign) NSTimeInterval previousUpdateTime;
@property (nonatomic, strong) TMXLayer *walls;
@property (nonatomic, strong) TMXLayer *hazards;
@property (nonatomic, assign) BOOL gameOver;
@property (nonatomic, strong) SKSpriteNode *selectedNode;
@property (nonatomic, strong)SKTexture *butt1;
@property (nonatomic, strong)SKTexture *butt2;
@property (nonatomic, strong)SKTexture *butt3;
@end

@implementation GameLevelScene

-(id)initWithSize:(CGSize)size {
   if (self = [super initWithSize:size]) {
_button = [SKSpriteNode spriteNodeWithImageNamed:@"b1.png"];
[_button setName:kAnimalNodeName];
[_button setPosition:CGPointMake(500,200)];
_button.zPosition =15;
_butt1= [SKTexture textureWithImageNamed:@"b1.png"];
_butt2= [SKTexture textureWithImageNamed:@"b2.png"];
_butt3= [SKTexture textureWithImageNamed:@"b3.png"];
[self addChild:_button];

[[SKTAudio sharedInstance] playBackgroundMusic:@"level1.mp3"];
[[SKTAudio sharedInstance] playSoundEffect:@"intro2.mp3"];
self.backgroundColor = [SKColor colorWithRed:.4 green:.4 blue:.95 alpha:1.0];
self.map = [JSTileMap mapNamed:@"cl1.tmx"];
[self addChild:self.map];
self.walls = [self.map layerNamed:@"walls"];
self.hazards = [self.map layerNamed:@"hazards"];
self.player = [[Player alloc] initWithImageNamed:@"cuberta1"];
self.player.position = CGPointMake(100, 250);
self.player.zPosition = 15;
[self.map addChild:self.player];
self.userInteractionEnabled = YES;
   }
 return self;
 }

 - (void)update:(NSTimeInterval)currentTime{
  if (self.gameOver) return;
   if(mode == 1){
NSLog(@"AAAAAAAAA");
_button = [SKSpriteNode spriteNodeWithImageNamed:@"b1.png"];
_button = [SKSpriteNode spriteNodeWithTexture:_butt1];
[_button setName:kAnimalNodeName];
[_button setPosition:CGPointMake(500,200)];
_button.zPosition =15;
[self addChild:_button];
    }else if (mode == 2){
NSLog(@"BBBBBBBBBB");
_button = [SKSpriteNode spriteNodeWithImageNamed:@"b2.png"];
_button = [SKSpriteNode spriteNodeWithTexture:_butt2];
[_button setName:kAnimalNodeName];
[_button setPosition:CGPointMake(500,200)];
_button.zPosition =15;
[self addChild:_button];
   }else if (mode == 3){
NSLog(@"CCCCCCCCC");
_button = [SKSpriteNode spriteNodeWithImageNamed:@"b3.png"];
_button = [SKSpriteNode spriteNodeWithTexture:_butt3];
[_button setName:kAnimalNodeName];
[_button setPosition:CGPointMake(500,200)];
_button.zPosition =15;
[self addChild:_button];
   }



 - (void)selectNodeForTouch:(CGPoint)touchLocation {
     //1  WHICH NODE IS SELECTED
     SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
    if([[touchedNode name] isEqualToString:kAnimalNodeName]) {


  if (mode == 3) {
    NSLog(@"DERP");
    mode = 1;
  }else if(mode == 2){
    NSLog(@"QWE");
    mode = 3;
  }else if (mode == 1) {
    NSLog(@"ASF");
    mode = 2;
  }


}

 }

1 个答案:

答案 0 :(得分:0)

不是每次点击都重新添加一堆图像,而是将所有图像放在前面,正确放置它们,然后使用SKNode属性"隐藏"显示或隐藏它们。

假设您的图像没有动态更改为不同的纹理,这应该比重新创建精灵要快得多。