ios - 我怎样才能让这个小小的抽搐消失?
<p><p>所以我在我的<strong>SpriteKit游戏项目</strong>中有一个更新方法,其中根据领导者跟随的方向决定其他跟随角色的位置:</p>
<pre><code>-(void) update {
if (_followingEnabled == YES || _isLeader == YES) {
switch (currentDirection) {
case up:
self.position = CGPointMake(self.position.x, self.position.y + speed);
// making a line of characters
if (self.position.x < _idealX && _isLeader == NO) {
self.position = CGPointMake(self.position.x + 1, self.position.y);
} else if (self.position.x > _idealX && _isLeader == NO) {
self.position = CGPointMake(self.position.x - 1, self.position.y);
}
break;
case down:
self.position = CGPointMake(self.position.x, self.position.y - speed);
// making a line of characters
if (self.position.x < _idealX && _isLeader == NO) {
self.position = CGPointMake(self.position.x + 1, self.position.y);
} else if (self.position.x > _idealX && _isLeader == NO) {
self.position = CGPointMake(self.position.x - 1, self.position.y);
}
break;
case left:
self.position = CGPointMake(self.position.x - speed, self.position.y);
// making a line of characters
if (self.position.y < _idealY && _isLeader == NO) {
self.position = CGPointMake(self.position.x, self.position.y + 1);
} else if (self.position.y < _idealY && _isLeader == NO) {
self.position = CGPointMake(self.position.x, self.position.y - 1);
}
break;
case right:
self.position = CGPointMake(self.position.x + speed, self.position.y);
// making a line of characters
if (self.position.y < _idealY && _isLeader == NO) {
self.position = CGPointMake(self.position.x, self.position.y + 1);
} else if (self.position.y < _idealY && _isLeader == NO) {
self.position = CGPointMake(self.position.x, self.position.y - 1);
}
break;
case noDirection:
// in case you want to do something for noDirection
break;
default:
break;
} // switch (currentDirection) {
} // if (_followingEnabled == YES && _isLeader == YES) {
}
</code></pre>
<p><code>_idealX</code> 和 <code>_idealY</code> 是从领导者位置传递的整数(我确实将它们设置为属性..),而跟随他的其他角色的位置是 float (如果我错了,请纠正我)</p>
<p><strong>我的理论:</strong></p>
<p>因此,当我加或减 1 时,我的字符(float)的位置永远不会等于 <code>_idealX/_idealY(int)</code> 并且语句将一遍又一遍地运行,这导致了这个Y/X 轴上的小“抽搐”或“抖动”(加减 1)。</p>
<p>所以你会更好地理解和想象这个,我做了一个截图(他身后的两个角色(领导者)在移动时抽搐..):</p>
<p> <img src="/image/a6CF7.png" alt="enter image description here"/> </p>
<p><strong>我该如何解决这个问题,这样追随者才不会在领导者身后“抽搐”或“摇晃”???</strong></p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>我不记得确切的计算了,但一般的方法是尝试并确保事物与像素网格对齐以进行显示。</p>
<pre><code>x = floorf(x+0.5)
</code></pre>
<p>您将需要一个实际位置和一个绘图位置,因为如果您使用底值,则在低速时事情将无法按预期工作。</p></p>
<p style="font-size: 20px;">关于ios - 我怎样才能让这个小小的抽搐消失?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/21488897/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/21488897/
</a>
</p>
页:
[1]