如何使SKSpriteNode重新出现在其SKScene的另一侧? [英] How to make SKSpriteNode reappear on the opposite side of its SKScene?

查看:90
本文介绍了如何使SKSpriteNode重新出现在其SKScene的另一侧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试验SpriteKit已经有一段时间了,我很欣赏一些关于我的SKSpriteNode在其SKScene范围内移动的最佳实践输入。



使用加速度计,我在SKSpriteNode的PhysicsBody属性上施加力,以便以类似大理石的方式在屏幕周围移动节点。目前,我使用 self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopF​​romRect:self.frame] 设置我的场景的PhysicsBody,以便大理石简单地从我的场景边缘反弹。



我不希望从我的场景边缘反弹,而是希望一旦大理石移动到我的场景边界(1),它将以相同的速度重新出现在我的场景的另一侧(2)。我也希望这个边界包裹立即发生,并且当大理石的任何部分移动到我的场景边界(3)之外时,该部分将在场景的另一侧(4)变得可见。 / p>

使用单个SKSpriteNode并保留该节点的PhysicsBody是否可以实现此目的?



提前致谢!



解决方案

不。如果精灵可以在任何方向离开屏幕,你需要4个精灵节点,以避免精灵在切换边时只是弹出进出。您必须通过选择4个精灵中的一个作为主精灵来同步它们的位置和速度。



其他三个只是用来直观地表示精灵沿着屏幕边缘以及拾取物理接触(这可能是棘手的,因为每个节点都可以接触,并且所有4个可以同时进行相同的接触)。 Otoh甚至可能没有必要根据其他身体要碰撞的其他三个物理机构。只是因为如果主人也会碰撞,奴隶可能总是碰撞,但只有当其他机构使用相同的机制或在屏幕边界附近从未发生碰撞时才会发生这种情况。



您可以将3个副本偏移+场景大小的+宽度,+高度和+宽度+高度,并根据主精灵位置每帧重新计算此偏移量。当实际离开屏幕的一侧时,您将不得不更改主精灵位置。例如,当主精灵离开屏幕的右侧时,您将从x位置减去(未设置!)场景大小的宽度。


I have been experimenting with SpriteKit for some time now, and I would appreciate some "best practice" input regarding the movement of my SKSpriteNode within its SKScene’s bounds.

Using the accelerometer, I am applying forces on an SKSpriteNode's PhysicsBody property in order to move the node around the screen in a marble-like fashion. Currently, I set the PhysicsBody of my scene using self.physicsBody = [SKPhysicsBody bodyWithEdgeLoopFromRect:self.frame] so that the marble simply bounces off of my scene's edges.

Instead of bouncing off of my scene's edges, I was hoping that once the marble moved beyond my scene’s bounds (1) it would reappear with the same velocity on the opposite side of my scene (2). I also want this bounds wrapping to occur immediately and in such a way that when any portion of the marble has moved outside of my scene’s bounds (3), that portion will have become visible on the opposite side of the scene (4).

Is it possible to accomplish this while using a single SKSpriteNode and preserving that node’s PhysicsBody?

Thanks in advance!

解决方案

Nope. You need 4 sprite nodes if the sprite can leave the screen in any direction to avoid the sprite from merely "popping" in and out when switching sides. You will have to synchronize their positions and velocities by choosing one of the 4 sprites to be the "master" sprite.

The other three are merely there to represent the sprite visually along the screen edges as well as picking up physics contacts (this may be tricky because each one of the nodes can contact, and all 4 may make the same contact at once). Otoh it may not even be necessary to give the other three physics bodies depending on what other bodies there are to collide with. Simply because the "slaves" might always collide if the master would collide too, but this is only true if other bodies use the same mechanism or collisions never occur near the screen borders.

You would offset the 3 copies by +width, +height and +width+height of the scene size and recalculate this offset every frame based on the master sprite position. And you will have to change the master sprite position when it actually leaves one side of the screen. For instance when the master sprite leaves the right side of the screen you would subtract (not set!) the width of the scene size from its x position.

这篇关于如何使SKSpriteNode重新出现在其SKScene的另一侧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆