Spritekit和OpenGL:顺畅的烟雾轨迹 [英] Spritekit and OpenGL: smooth smoke trail
问题描述
我想在我的Spritekit游戏中实现这种效果,该游戏的角色后面有平滑痕迹.
I want to achieve this effect in my Spritekit game where there is a smooth trail behind the character.
查看jetpack joyride中硬币背后的踪迹:
See the trail behind the coin in jetpack joyride:
这是木星跳跃中英雄背后的足迹:
And this trail behind the hero in Jupiter Jump:
或者在Ski Safari中落后于英雄的超级顺畅步道:
Or this super smooth trail behind the hero in Ski Safari:
这似乎是其他游戏引擎的标准功能吗?我认为一个spritekit粒子发射器只会提供一个块状/带标记的轨迹,而不是一个平滑的轨迹.我应该使用某种精灵自定义着色器吗?还有其他创造性的想法吗?
This appears to be a standard feature in other game engines maybe? I think a spritekit particle emitter will only provide a blocky/stamped trail, not a smooth trail. Should I be using some kind of sprite custom shader? Any other inventive thoughts?
推荐答案
您的问题不包括关键问题,后者是要使用的机芯类型.我的答案是基于触摸屏幕上的目标点,但另一种选择是使用核心运动.无论使用哪种方法,基本代码主体均保持不变.只有实现会改变.
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.
我在示例中使用了矩形尾部图像,因为我希望您能够复制并运行示例代码.您应将rect替换为圆形图像/纹理,以使尾部的侧面更光滑.
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.
修改fadeOutDuration值将导致更长或更短的持久尾巴.
Modifying the fadeOutDuration value will result in a longer or shorter lasting tail.
修改stepsDivider将导致尾部或多或少的节点.
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
这篇关于Spritekit和OpenGL:顺畅的烟雾轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!