我有两个节点。玩家和敌人。我希望 Enemy 节点在足够近后跟随 Player 节点,并且 Enemy 节点在与 Player 节点碰撞时会停止。我得到的是 Enemy 节点位于 Player 之上,两个节点都被推送。我曾想过以某种方式阻止 Enemy 节点在与 Player 发生碰撞时移动,但在我看来它应该是一种 cleaner 方式。
(我通过在更新时更改它的位置来移动敌人节点)。
这是我的 GameScene.sks:
-(void)didMoveToViewSKView *)view {
player = [self childNodeWithName"character"];
enemy = [self childNodeWithName"enemy"];
self.physicsWorld.contactDelegate = self;
}
-(void)touchesBeganNSSet *)touches withEventUIEvent *)event {
for (UITouch *touch in touches) {
moveToTouch = [touch locationInNode:self];
SKNode *tile = [self nodeAtPoint:moveToTouch];
if([tile.name isEqualToString"tile"])
{
moveToTouch = tile.position;
}
}
}
-(void)updateCFTimeInterval)currentTime {
[self walk:player to:moveToTouch];
if((SDistanceBetweenPoints(enemy.position, player.position) < 200))
{
[self walk:enemy to:player.position];
}
}
-(void)walkSKNode*)node toCGPoint)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;
}
}
我不知道你是如何设置你的敌人跟随玩家的。 不过你可以试试
使用碰撞检测来了解碰撞的时间。
关于ios - Sprite Kit 中的简单碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33465187/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |