gpt4 book ai didi

opengl-es - Spritekit 和 OpenGL : smooth smoke trail

转载 作者:行者123 更新时间:2023-12-02 22:39:30 30 4
gpt4 key购买 nike

我想在我的 Spritekit 游戏中实现这种效果,其中角色后面有一条平滑轨迹。

在喷气背包兜风中查看硬币背后的踪迹: enter image description here

木星跳跃中英雄背后的这条踪迹: enter image description here

或者《Ski Safari》中英雄身后的这条 super 平滑的小路: enter image description here

这似乎是其他游戏引擎的标准功能?我认为 spritekit 粒子发射器只会提供 block 状/标记的轨迹,而不是平滑的轨迹。我应该使用某种 Sprite 自定义着色器吗?还有其他创意吗?

最佳答案

您的问题没有包括要使用的运动类型这一关键问题。我的答案是基于触摸屏幕到达目的地,但另一种选择是使用核心运动。无论使用哪种方法,基本代码原理都是相同的。只有实现会改变。

我在示例中使用了矩形尾部图像,因为我希望您能够复制并运行示例代码。您应该用圆形图像/纹理替换矩形,以使尾部的侧面更平滑。

修改 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

关于opengl-es - Spritekit 和 OpenGL : smooth smoke trail,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29137947/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com