Spritekit and OpenGL: smooth smoke trail

。_饼干妹妹 提交于 2019-12-03 15:23:47

Your question did not include a key issue which is the type of movement to be used. My answer is based on touching the screen for a destination point but another alternative is to use core motion. Regardless of which method is used the basic code principals remain the same. Only the implementation would change.

I used a rectangle tail image in my example because I wanted for you to be able to copy and run the example code. You should replace the rect with a circle image/texture to give the tail smoother sides.

Modifying the fadeOutDuration value will result in a longer or shorter lasting tail.

Modifying the stepsDivider will result in a more or less nodes in the tail.

#import "GameScene.h"

@implementation GameScene {
    SKSpriteNode *playerNode;
    CGPoint destinationPoint;
    NSMutableArray *myArray;
    NSMutableArray *myDiscardArray;
    BOOL working;
    int numberOfSteps;
    float xIncrement;
    float yIncrement;
    float fadeOutDuration;
    int stepsDivider;
}

-(void)didMoveToView:(SKView *)view {
    self.backgroundColor = [SKColor blackColor];

    playerNode = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(30, 30)];
    playerNode.position = CGPointMake(200, 200);
    [self addChild:playerNode];

    myArray = [[NSMutableArray alloc] init];
    myDiscardArray = [[NSMutableArray alloc] init];

    working = false;
    fadeOutDuration = 0.5;
    stepsDivider = 10;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (UITouch *touch in touches) {
        CGPoint location = [touch locationInNode:self];

        if(working == false) {
            destinationPoint = location;

            if(fabsf(location.x - playerNode.position.x) > fabsf(location.y - playerNode.position.y)) {
                numberOfSteps = fabsf(location.x - playerNode.position.x) / 10;
            } else {
                numberOfSteps = fabsf(location.y - playerNode.position.y) / 10;
            }

            xIncrement = (location.x - playerNode.position.x) / numberOfSteps;
            yIncrement = (location.y - playerNode.position.y) / numberOfSteps;

            working = true;
        }
    }
}

-(void)update:(CFTimeInterval)currentTime {

    if (working == true) {

        // create trail node at current player's position
        SKSpriteNode *myNode = [SKSpriteNode spriteNodeWithColor:[SKColor whiteColor] size:CGSizeMake(30, 30)];
        myNode.position = playerNode.position;
        [self addChild:myNode];
        [myArray addObject:myNode];
        [myNode runAction:[SKAction fadeOutWithDuration:fadeOutDuration]];

        // check array for any nodes with zero alpha
        for(SKSpriteNode *object in myArray) {
            if(object.alpha == 0) {
                [myDiscardArray addObject:object];
            }
        }

        // remove zero alpha nodes
        if([myDiscardArray count] > 0) {
            [myArray removeObjectsInArray:myDiscardArray];
            [myDiscardArray removeAllObjects];
        }

        // update player's new position
        playerNode.position = CGPointMake(playerNode.position.x+xIncrement, playerNode.position.y+yIncrement);

        // check if player has arrived at destination
        if(((int)playerNode.position.x == (int)destinationPoint.x) && ((int)playerNode.position.y == (int)destinationPoint.y)) {
            working = false;
        }
    }
}

@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!