Spe*_*pgh 8 ios sprite-kit skaction skspritenode
在IOS SpriteKit中玩SKSprites,我基本上想让一个精灵随机移动某个方向一定距离,然后选择一个新的随机方向和距离.很简单,创建一个生成随机数然后创建的方法动画和动画的完成块让它回调相同的例程.
这确实有效,但它也可以防止动画以相同的速度移动,因为动画都是基于持续时间的.如果对象必须移动100,它移动的速度是它移动的速度的1/2,如果下一个随机告诉它移动200 ......那么我怎么能以一致的速度移动?
And*_*yOS 13
@Noah Witherspoon在上面是正确的 - 使用Pythagorus来获得平稳的速度:
//the node you want to move
SKNode *node = [self childNodeWithName:@"spaceShipNode"];
//get the distance between the destination position and the node's position
double distance = sqrt(pow((destination.x - node.position.x), 2.0) + pow((destination.y - node.position.y), 2.0));
//calculate your new duration based on the distance
float moveDuration = 0.001*distance;
//move the node
SKAction *move = [SKAction moveTo:CGPointMake(destination.x,destination.y) duration: moveDuration];
[node runAction: move];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5291 次 |
| 最近记录: |