SpriteKit输送带 [英] SpriteKit conveyor belt

查看:78
本文介绍了SpriteKit输送带的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用SpriteKit创建传送带效果,如此

I am trying to create a conveyor belt effect using SpriteKit like so

我的第一反应是创建一个大于屏幕的传送带图像,然后通过动作反复移动它。但这似乎不合适,因为它取决于屏幕尺寸。

MY first reflex would be to create a conveyor belt image bigger than the screen and then move it repeatedly forever with actions. But this does not seem ok because it is dependent on the screen size.

有没有更好的方法呢?

Is there any better way to do this ?

同样显然我想把东西(它会独立移动)放在传送带上,这样节点就是一个带有子精灵节点的SKNode。

Also obviously I want to put things (which would move independently) on the conveyor belt so the node is an SKNode with a the child sprite node that is moving.

更新:我希望传送带视觉上移动;所以线条朝着给人以运动印象的方向移动。

Update : I would like the conveyor belt to move "visually"; so the lines move in a direction giving the impression of movement.

推荐答案

physicsBody 应用于您需要继续前进的所有精灵传送带并将 affectedByGravity 属性设置为NO。

Apply physicsBody to all those sprites which you need to move on the conveyor belt and set the affectedByGravity property as NO.

在此示例中,我假设spriteNode表示您的传送带称为传送带。此外,需要移动的所有精灵节点都具有字符串moveable作为其 name 属性。

In this example, I am assuming that the spriteNode representing your conveyor belt is called conveyor. Also, all the sprite nodes which need to be moved are have the string "moveable" as their name property.

然后,在你的-update:方法中,

Then, in your -update: method,

-(void)update:(CFTimeInterval)currentTime
{
    [self enumerateChildNodesWithName:@"moveable" usingBlock:^(SKNode *node, BOOL *stop{ 
        if ([node intersectsNode:conveyor])
        {
            [node.physicsBody applyForce:CGVectorMake(-1, 0)];
            //edit the vector to get the right force. This will be too fast.
        }
    }];
}

在此之后,只需在正确的位置添加所需的精灵,您就会看到它们自行移动。

After this, just add the desired sprites on the correct positions and you will see them moving by themselves.

对于动画,最好使用一个纹理数组,你可以在精灵上循环。

For the animation, it would be better to use an array of textures which you can loop on the sprite.

或者,您可以添加和删除一系列带有截面图像的小精灵,并像移动它们一样移动它们在输送机上行驶的精灵。

Alternatively, you can add and remove a series of small sprites with a sectional image and move them like you do the sprites which are travelling on the conveyor.

这篇关于SpriteKit输送带的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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