我想在我的 Spritekit 游戏中实现这种效果,其中有一个smooth角色后面的踪迹。
See the trail behind the coin in jetpack joyride:
And this trail behind the hero in Jupiter Jump:
Or this super smooth trail behind the hero in Ski Safari:
这似乎是其他游戏引擎的标准功能?我认为 spritekit 粒子发射器只会提供块状/标记的轨迹,而不是平滑的轨迹。我应该使用某种精灵自定义着色器吗?还有其他创意吗?
您的问题没有包括一个关键问题,即要使用的运动类型。我的答案是基于触摸屏幕到达目的地,但另一种选择是使用核心运动。无论使用哪种方法,基本代码原理都是相同的。只有实施会改变。
我在示例中使用了矩形尾部图像,因为我希望您能够复制并运行示例代码。您应该用圆形图像/纹理替换矩形,以使尾部侧面更平滑。
修改 fadeOutDuration 值将导致更长或更短的持续尾部。
修改stepsDivider会导致尾部的节点增多或减少。
#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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)