gpt4 book ai didi

ios - Sprite Kit 中的简单碰撞

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:40 26 4
gpt4 key购买 nike

我有两个节点。玩家和敌人。我希望 Enemy 节点在足够近时跟随 Player 节点,并且 Enemy 节点在与 Player 节点碰撞时会停止。我得到的是 Enemy 节点位于 Player 之上并且两个节点都被推送。我想过以某种方式阻止 Enemy 节点在与 Player 发生碰撞时移动,但在我看来这应该是一种更清洁的方式。

(我通过在更新时改变它的位置来移动敌人节点)。

这是我的 GameScene.sks:

character and enemy options from GameScene

-(void)didMoveToView:(SKView *)view {

player = [self childNodeWithName:@"character"];
enemy = [self childNodeWithName:@"enemy"];
self.physicsWorld.contactDelegate = self;
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

for (UITouch *touch in touches) {
moveToTouch = [touch locationInNode:self];
SKNode *tile = [self nodeAtPoint:moveToTouch];
if([tile.name isEqualToString:@"tile"])
{
moveToTouch = tile.position;
}
}
}

-(void)update:(CFTimeInterval)currentTime {

[self walk:player to:moveToTouch];

if((SDistanceBetweenPoints(enemy.position, player.position) < 200))
{
[self walk:enemy to:player.position];
}


}

-(void)walk:(SKNode*)node to:(CGPoint)moveTo
{
if (CGPointEqualToPoint(moveTo, CGPointZero))
{
return;
}


if(round(moveTo.y) != round(node.position.y))
{

if(moveTo.y > node.position.y)
{
node.position = CGPointMake(node.position.x,node.position.y+2);
}
else if (moveTo.y < node.position.y)
{
node.position = CGPointMake(node.position.x,node.position.y-2);
}

}else if(round(moveTo.x) != round(node.position.x))
{

if(moveTo.x > node.position.x)
{
node.position = CGPointMake(node.position.x+2,node.position.y);
}
else if (moveTo.x < node.position.x)
{
node.position = CGPointMake(node.position.x-2,node.position.y);
}

}

float distance = SDistanceBetweenPoints(node.position, moveTo);
if (distance < 1.0){
moveToTouch = CGPointZero;
}

}

最佳答案

我不知道你是如何设置你的敌人跟随玩家的。但你可以尝试

  • 将敌人动态设置为 false
  • 或将速度设置为 0

使用碰撞检测来了解碰撞的时间。

关于ios - Sprite Kit 中的简单碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465187/

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