简体   繁体   English

在精灵工具包中,当我在节点上运行一个动作以将其上移到某个y值时,它会逐渐变慢。 为什么?

[英]In sprite kit with swift when i run an action on a node to move it up to a certain y value it gradually slows down. Why?

I didn't setup any physics and I don't use the scene file. 我没有设置任何物理学,也没有使用场景文件。 I just setup scene from code. 我只是从代码设置场景。

that's where i setup and fire up the action: 那是我设置并启动动作的地方:

  let GoUpAction = SKAction.moveTo(CGPoint(x:80,y:size.height-hVar),duration:1);
    if node1.position.y < (size.height/6) + 20 {
        node2.runAction(GoUpAction)
    }

and when it executes, node2 start moving up with normal speed but the higher (y++) it gets the slower it moves. 当它执行时,node2开始以正常速度向上移动,但是(y ++)越高,它移动得越慢。

Funny thing - in general that behaviour suites me, but i'd appreciate the slowdown to start only 1/4 away from the destination. 有趣的事情-通常,这种行为适合我,但我希望能从距离目的地仅1/4的距离开始减速。 Or no slowdown at all. 或根本没有减速。

I didn't set any gravity or anything. 我没有设置任何重力或任何东西。 very basic setup. 非常基本的设置。

this is how nodes are defined: 这是定义节点的方式:

    let node2 = SKSpriteNode(imageNamed: "n2")
    node2.name = "n2"
    node2.position = CGPoint(x: size.width-80, y: size.height-hVar)
    node2.xScale = 0.25
    node2.yScale = 0.25
    node2.zPosition = -1
    addChild(node2)

I didn't set any gravity or anything. 我没有设置任何重力或任何东西。 very basic setup. 非常基本的设置。

The default value is (0.0,-9.8) which may slow down your node in y direction. 默认值为(0.0,-9.8),这可能会在y方向上减慢节点速度。 Try to either set property affectedByGravity of your physic body, 尝试设置affectedByGravity属性affectedByGravity的属性。

node2.physicsBody.affectedByGravity = NO;

or set "global" gravity when creating your scene, 或在创建场景时设置“全局” gravity

self.physicsWorld.gravity = CGVectorMake(0.0f, 0.0f);

to get rid of the gravity. 摆脱重力。

Thank you all for your help! 谢谢大家的帮助!

However the issue was a bit structural. 但是,问题有点结构化。 I made a stupid mistake which I didn't think through well by having a certain function being called from the Update method (runs every frame). 我犯了一个愚蠢的错误,因为从Update方法(运行每一帧)中调用了某个函数,所以我想不通。

This function was actually retriggering the a sprite move action on each frame, but with each frame the distance became less, while time for the action to run was the same, so it produced the slowdown effect:) 该函数实际上是在每个帧上重新触发一个精灵移动动作,但是随着每个帧的距离变短,而动作运行的时间相同,因此产生了减速效果:)

I had to rewrite some code in regards what is called when and got rid of this stupid mistake. 我不得不重写有关何时调用的代码,并摆脱了这个愚蠢的错误。

All now works as expected:) 现在所有工作都按预期进行:)

But a funny trick to simulate the slowdown though:) 但是,这是一个模拟减速的有趣技巧:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM