What is the best symbol for SKNode or SKSpriteNode?

enter image description here Current, my class looks something like

@interface Character : SKNode @property (nonatomic, assign) CGFloat walkingSpeed; 

@implementation Character

 +(Character*)characterWithLevel:(BaseLevel *)level { return [[self alloc] initWithLevel:level]; } -(id)initWithLevel:(BaseLevel*)level { if (self = [super init]) { _sprite = [SKSpriteNode spriteNodeWithImageNamed:level.heroTexture]; _sprite.position = level.heroStartingPosition; [self addChild:_sprite]; [level addChild:self]; } return self; } 

The problem is that when you perform actions with the node symbol at the level, it simply won’t do anything because SpriteNode is animated. I wonder how you guys approach these issues?

Action example:

In touch event:

 CGPoint location = [[touches anyObject] locationInNode:self]; [character moveToPosition:location]; 

Inside a character class

 -(void)move { [self runAction:[SKAction repeatActionForever:[SKAction animateWithTextures:[self walkFrames] timePerFrame:0.1f resize:NO restore:YES]] withKey:@"moving"]; return; } 

It does nothing. The only way to make it work if the character was a subclass of SKSpriteNode. Performing an action against self.sprite also seems hacked and the node itself will not move.

Any suggestions?

+6
source share
1 answer

Why does using self.sprite seem to be hacked? This is absolutely true.

In fact, from the point of view of OOP, SKNode, combining other visible nodes, is a more universal approach.

For example, this allows you to hide the sprite and instead make the particle emitter visible if the player picks up the powerup, which makes it invisible (or very transparent) with just a few bright bits around it.

To achieve this, you will have a radiator as a child of the node symbol. If the node symbol is where the sprite player is, you cannot turn only the invisible sprite, because this will also affect the child emitter.

Therefore, in the end, it is better to aggregate views (visible nodes) rather than subclassing them. Think of the SKNode symbol as a view controller for one or more views (its children, such as sprites, emitters, labels, etc.).

+8
source

Source: https://habr.com/ru/post/957628/


All Articles