ios - Sprite Kit 中的简单碰撞
<p><p>我有两个节点。玩家和敌人。我希望 Enemy 节点在足够近后跟随 Player 节点,并且 Enemy 节点在与 Player 节点碰撞时会停止。我得到的是 Enemy 节点位于 Player 之上,两个节点都被推送。我曾想过以某种方式阻止 Enemy 节点在与 Player 发生碰撞时移动,但在我看来它应该是一种 <em>cleaner</em> 方式。</p>
<p>(我通过在更新时更改它的位置来移动敌人节点)。</p>
<p>这是我的 GameScene.sks:</p>
<p> <a href="/image/EJoaG.png" rel="noreferrer noopener nofollow"><img src="/image/EJoaG.png" alt="character and enemy options from GameScene"/></a> </p>
<pre><code>-(void)didMoveToView:(SKView *)view {
player = ;
enemy= ;
self.physicsWorld.contactDelegate = self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
for (UITouch *touch in touches) {
moveToTouch = ;
SKNode *tile = ;
if()
{
moveToTouch = tile.position;
}
}
}
-(void)update:(CFTimeInterval)currentTime {
;
if((SDistanceBetweenPoints(enemy.position, player.position) < 200))
{
;
}
}
-(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;
}
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我不知道你是如何设置你的敌人跟随玩家的。
不过你可以试试</p>
<ul>
<li>将敌人动态设置为 false</li>
<li>或将速度设为 0</li>
</ul>
<p>使用碰撞检测来了解碰撞的时间。</p></p>
<p style="font-size: 20px;">关于ios - Sprite Kit 中的简单碰撞,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/33465187/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/33465187/
</a>
</p>
页:
[1]