SpriteKit - 如何使用“挡板”使奖品轮减速?

Posted

技术标签:

【中文标题】SpriteKit - 如何使用“挡板”使奖品轮减速?【英文标题】:SpriteKit - How Do I Make a Prize Wheel Slow Down With A "Flapper"? 【发布时间】:2014-06-02 21:13:14 【问题描述】:

我正在尝试使用精灵套件制作命运之轮风格的游戏。但我被困住了。

好的,我有两个 SKSpriteNode (带有物理实体)。旋转得很好的***和“挡板”(也就是减慢奖品轮盘并指示您中奖的东西。挡板不断被向上推,而不是转动并减慢***的速度。

我的问题是:

A) 我如何防止“挡板”向上移动,而不是在顶部保持静止并减慢车轮速度。

B) 我如何使“挡板弯曲而不是简单地被推动”。

C) 为什么***不会旋转不止一次? (注意:我尝试在每次 touchesBegan 之前添加 [wheel removeAllActions];:

代码:

-(id)initWithSize:(CGSize)size     
if (self = [super initWithSize:size]) 
    /* Setup your scene here */

    self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

    SKLabelNode *myLabel = [SKLabelNode labelNodeWithFontNamed:@"Chalkduster"];

    myLabel.text = @"Hello, World!";
    myLabel.fontSize = 30;
    myLabel.position = CGPointMake(CGRectGetMidX(self.frame),
                                   CGRectGetMidY(self.frame));


    myLabel.text = @"Speed: 0";
    myLabel.fontSize = 20;
    myLabel.position = CGPointMake(CGRectGetMinX(self.frame) + 50,
                                   CGRectGetMinY(self.frame));

    /* WHEEL CREATION */
    /*==============================================================================================*/
    wheel = [SKSpriteNode spriteNodeWithImageNamed:@"wheel.png"];
    self.physicsWorld.gravity = CGVectorMake(0.0, 0.0);

    CGFloat offsetX = wheel.frame.size.width * wheel.anchorPoint.x;
    CGFloat offsetY = wheel.frame.size.height * wheel.anchorPoint.y;

    wheel.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMinY(self.frame) + 170);

    CGMutablePathRef path = CGPathCreateMutable();

    CGPathMoveToPoint(path, NULL, 132 - offsetX, 276 - offsetY);
    CGPathAddLineToPoint(path, NULL, 122 - offsetX, 283 - offsetY);
    CGPathAddLineToPoint(path, NULL, 116 - offsetX, 272 - offsetY);
    CGPathAddLineToPoint(path, NULL, 108 - offsetX, 271 - offsetY);
    CGPathAddLineToPoint(path, NULL, 100 - offsetX, 268 - offsetY);
    CGPathAddLineToPoint(path, NULL, 95 - offsetX, 266 - offsetY);
    CGPathAddLineToPoint(path, NULL, 87 - offsetX, 261 - offsetY);
    CGPathAddLineToPoint(path, NULL, 86 - offsetX, 260 - offsetY);
    CGPathAddLineToPoint(path, NULL, 73 - offsetX, 263 - offsetY);
    CGPathAddLineToPoint(path, NULL, 72 - offsetX, 250 - offsetY);
    CGPathAddLineToPoint(path, NULL, 65 - offsetX, 246 - offsetY);
    CGPathAddLineToPoint(path, NULL, 58 - offsetX, 239 - offsetY);
    CGPathAddLineToPoint(path, NULL, 53 - offsetX, 234 - offsetY);
    CGPathAddLineToPoint(path, NULL, 48 - offsetX, 227 - offsetY);
    CGPathAddLineToPoint(path, NULL, 36 - offsetX, 225 - offsetY);
    CGPathAddLineToPoint(path, NULL, 39 - offsetX, 213 - offsetY);
    CGPathAddLineToPoint(path, NULL, 34 - offsetX, 206 - offsetY);
    CGPathAddLineToPoint(path, NULL, 32 - offsetX, 200 - offsetY);
    CGPathAddLineToPoint(path, NULL, 30 - offsetX, 194 - offsetY);
    CGPathAddLineToPoint(path, NULL, 27 - offsetX, 187 - offsetY);
    CGPathAddLineToPoint(path, NULL, 26 - offsetX, 183 - offsetY);
    CGPathAddLineToPoint(path, NULL, 15 - offsetX, 175 - offsetY);
    CGPathAddLineToPoint(path, NULL, 23 - offsetX, 166 - offsetY);
    CGPathAddLineToPoint(path, NULL, 22 - offsetX, 163 - offsetY);
    CGPathAddLineToPoint(path, NULL, 22 - offsetX, 154 - offsetY);
    CGPathAddLineToPoint(path, NULL, 22 - offsetX, 143 - offsetY);
    CGPathAddLineToPoint(path, NULL, 23 - offsetX, 133 - offsetY);
    CGPathAddLineToPoint(path, NULL, 16 - offsetX, 122 - offsetY);
    CGPathAddLineToPoint(path, NULL, 26 - offsetX, 117 - offsetY);
    CGPathAddLineToPoint(path, NULL, 27 - offsetX, 112 - offsetY);
    CGPathAddLineToPoint(path, NULL, 28 - offsetX, 108 - offsetY);
    CGPathAddLineToPoint(path, NULL, 31 - offsetX, 101 - offsetY);
    CGPathAddLineToPoint(path, NULL, 34 - offsetX, 96 - offsetY);
    CGPathAddLineToPoint(path, NULL, 37 - offsetX, 88 - offsetY);
    CGPathAddLineToPoint(path, NULL, 39 - offsetX, 85 - offsetY);
    CGPathAddLineToPoint(path, NULL, 36 - offsetX, 72 - offsetY);
    CGPathAddLineToPoint(path, NULL, 48 - offsetX, 71 - offsetY);
    CGPathAddLineToPoint(path, NULL, 51 - offsetX, 68 - offsetY);
    CGPathAddLineToPoint(path, NULL, 54 - offsetX, 63 - offsetY);
    CGPathAddLineToPoint(path, NULL, 60 - offsetX, 59 - offsetY);
    CGPathAddLineToPoint(path, NULL, 65 - offsetX, 54 - offsetY);
    CGPathAddLineToPoint(path, NULL, 71 - offsetX, 49 - offsetY);
    CGPathAddLineToPoint(path, NULL, 73 - offsetX, 37 - offsetY);
    CGPathAddLineToPoint(path, NULL, 85 - offsetX, 39 - offsetY);
    CGPathAddLineToPoint(path, NULL, 91 - offsetX, 36 - offsetY);
    CGPathAddLineToPoint(path, NULL, 97 - offsetX, 33 - offsetY);
    CGPathAddLineToPoint(path, NULL, 103 - offsetX, 30 - offsetY);
    CGPathAddLineToPoint(path, NULL, 109 - offsetX, 28 - offsetY);
    CGPathAddLineToPoint(path, NULL, 116 - offsetX, 26 - offsetY);
    CGPathAddLineToPoint(path, NULL, 123 - offsetX, 16 - offsetY);
    CGPathAddLineToPoint(path, NULL, 133 - offsetX, 24 - offsetY);
    CGPathAddLineToPoint(path, NULL, 138 - offsetX, 22 - offsetY);
    CGPathAddLineToPoint(path, NULL, 145 - offsetX, 22 - offsetY);
    CGPathAddLineToPoint(path, NULL, 152 - offsetX, 22 - offsetY);
    CGPathAddLineToPoint(path, NULL, 160 - offsetX, 22 - offsetY);
    CGPathAddLineToPoint(path, NULL, 165 - offsetX, 23 - offsetY);
    CGPathAddLineToPoint(path, NULL, 176 - offsetX, 15 - offsetY);
    CGPathAddLineToPoint(path, NULL, 182 - offsetX, 26 - offsetY);
    CGPathAddLineToPoint(path, NULL, 190 - offsetX, 29 - offsetY);
    CGPathAddLineToPoint(path, NULL, 198 - offsetX, 31 - offsetY);
    CGPathAddLineToPoint(path, NULL, 203 - offsetX, 34 - offsetY);
    CGPathAddLineToPoint(path, NULL, 208 - offsetX, 37 - offsetY);
    CGPathAddLineToPoint(path, NULL, 213 - offsetX, 39 - offsetY);
    CGPathAddLineToPoint(path, NULL, 224 - offsetX, 36 - offsetY);
    CGPathAddLineToPoint(path, NULL, 226 - offsetX, 47 - offsetY);
    CGPathAddLineToPoint(path, NULL, 231 - offsetX, 51 - offsetY);
    CGPathAddLineToPoint(path, NULL, 236 - offsetX, 57 - offsetY);
    CGPathAddLineToPoint(path, NULL, 242 - offsetX, 61 - offsetY);
    CGPathAddLineToPoint(path, NULL, 245 - offsetX, 66 - offsetY);
    CGPathAddLineToPoint(path, NULL, 249 - offsetX, 72 - offsetY);
    CGPathAddLineToPoint(path, NULL, 262 - offsetX, 74 - offsetY);
    CGPathAddLineToPoint(path, NULL, 258 - offsetX, 86 - offsetY);
    CGPathAddLineToPoint(path, NULL, 264 - offsetX, 94 - offsetY);
    CGPathAddLineToPoint(path, NULL, 268 - offsetX, 101 - offsetY);
    CGPathAddLineToPoint(path, NULL, 270 - offsetX, 110 - offsetY);
    CGPathAddLineToPoint(path, NULL, 272 - offsetX, 117 - offsetY);
    CGPathAddLineToPoint(path, NULL, 283 - offsetX, 122 - offsetY);
    CGPathAddLineToPoint(path, NULL, 275 - offsetX, 132 - offsetY);
    CGPathAddLineToPoint(path, NULL, 276 - offsetX, 137 - offsetY);
    CGPathAddLineToPoint(path, NULL, 277 - offsetX, 145 - offsetY);
    CGPathAddLineToPoint(path, NULL, 277 - offsetX, 150 - offsetY);
    CGPathAddLineToPoint(path, NULL, 277 - offsetX, 156 - offsetY);
    CGPathAddLineToPoint(path, NULL, 276 - offsetX, 163 - offsetY);
    CGPathAddLineToPoint(path, NULL, 276 - offsetX, 167 - offsetY);
    CGPathAddLineToPoint(path, NULL, 283 - offsetX, 176 - offsetY);
    CGPathAddLineToPoint(path, NULL, 272 - offsetX, 183 - offsetY);
    CGPathAddLineToPoint(path, NULL, 271 - offsetX, 188 - offsetY);
    CGPathAddLineToPoint(path, NULL, 269 - offsetX, 195 - offsetY);
    CGPathAddLineToPoint(path, NULL, 266 - offsetX, 202 - offsetY);
    CGPathAddLineToPoint(path, NULL, 263 - offsetX, 208 - offsetY);
    CGPathAddLineToPoint(path, NULL, 260 - offsetX, 212 - offsetY);
    CGPathAddLineToPoint(path, NULL, 263 - offsetX, 225 - offsetY);
    CGPathAddLineToPoint(path, NULL, 250 - offsetX, 226 - offsetY);
    CGPathAddLineToPoint(path, NULL, 246 - offsetX, 233 - offsetY);
    CGPathAddLineToPoint(path, NULL, 241 - offsetX, 238 - offsetY);
    CGPathAddLineToPoint(path, NULL, 235 - offsetX, 243 - offsetY);
    CGPathAddLineToPoint(path, NULL, 229 - offsetX, 249 - offsetY);
    CGPathAddLineToPoint(path, NULL, 226 - offsetX, 250 - offsetY);
    CGPathAddLineToPoint(path, NULL, 225 - offsetX, 263 - offsetY);
    CGPathAddLineToPoint(path, NULL, 213 - offsetX, 259 - offsetY);
    CGPathAddLineToPoint(path, NULL, 207 - offsetX, 263 - offsetY);
    CGPathAddLineToPoint(path, NULL, 198 - offsetX, 267 - offsetY);
    CGPathAddLineToPoint(path, NULL, 191 - offsetX, 271 - offsetY);
    CGPathAddLineToPoint(path, NULL, 181 - offsetX, 273 - offsetY);
    CGPathAddLineToPoint(path, NULL, 175 - offsetX, 283 - offsetY);
    CGPathAddLineToPoint(path, NULL, 166 - offsetX, 275 - offsetY);
    CGPathAddLineToPoint(path, NULL, 159 - offsetX, 277 - offsetY);
    CGPathAddLineToPoint(path, NULL, 152 - offsetX, 277 - offsetY);
    CGPathAddLineToPoint(path, NULL, 145 - offsetX, 277 - offsetY);
    CGPathAddLineToPoint(path, NULL, 138 - offsetX, 277 - offsetY);
    CGPathAddLineToPoint(path, NULL, 133 - offsetX, 275 - offsetY);

    CGPathCloseSubpath(path);

    wheel.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:path];
    wheel.physicsBody.allowsRotation = YES;
    wheel.physicsBody.linearDamping = 100;


    /*======================================================================*/


    /* "FLAPPER" CREATION */
    /*==============================================================================================*/
    flapper = [SKSpriteNode spriteNodeWithImageNamed:@"flapper.png"];
    flapper.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMaxY(self.frame) - 92);
    flapper.anchorPoint = CGPointMake(0.5, 1);
    flapper.physicsBody.allowsRotation = YES;
    CGFloat flapperOffsetX = flapper.frame.size.width * flapper.anchorPoint.x;
    CGFloat flapperOffsetY = flapper.frame.size.height * flapper.anchorPoint.y;

    CGMutablePathRef flapperPath = CGPathCreateMutable();

    CGPathMoveToPoint(flapperPath, NULL, 4 - flapperOffsetX, 0 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 5 - flapperOffsetX, 0 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 6 - flapperOffsetX, 2 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 7 - flapperOffsetX, 8 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 7 - flapperOffsetX, 13 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 7 - flapperOffsetX, 17 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 8 - flapperOffsetX, 21 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 8 - flapperOffsetX, 24 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 8 - flapperOffsetX, 29 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 8 - flapperOffsetX, 33 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 8 - flapperOffsetX, 37 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 8 - flapperOffsetX, 38 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 7 - flapperOffsetX, 40 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 6 - flapperOffsetX, 40 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 5 - flapperOffsetX, 40 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 3 - flapperOffsetX, 40 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 1 - flapperOffsetX, 39 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 1 - flapperOffsetX, 35 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 2 - flapperOffsetX, 15 - flapperOffsetY);
    CGPathAddLineToPoint(flapperPath, NULL, 3 - flapperOffsetX, 5 - flapperOffsetY
                         );

    CGPathCloseSubpath(flapperPath);

    flapper.physicsBody = [SKPhysicsBody bodyWithPolygonFromPath:flapperPath];
    flapper.physicsBody.mass = 5;
    flapper.physicsBody.linearDamping = 100;
    flapper.physicsBody.allowsRotation = TRUE;
    flapper.physicsBody.usesPreciseCollisionDetection = TRUE;

            /*==============================================================================================*/

        [self addChild:myLabel];
        [self addChild:wheel];
        [self addChild:flapper];
    

    return self;
    

    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
    /* Called when a touch begins */

    for (UITouch *touch in touches)
    

        CGPoint location = [touch locationInNode:self];

        //SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];

        SKAction *action = [SKAction rotateByAngle:10 duration:1];
        [wheel runAction:[SKAction repeatActionForever:action]];

        SKAction *slow = [SKAction speedBy:0.2 duration:5];
        [wheel runAction:[SKAction repeatAction:slow count:10]];
    

    NSLog(@"reached end of touchesBegan");

【问题讨论】:

【参考方案1】:

老问题,但对于任何有类似问题的人来说。我在 Spritekit 和 swift 中创建了一个开源的旋转奖轮,它使用物理来实现逼真的运动和挡板控制。

它允许用户通过推动***的中心或使用物理力拖动***来产生随机旋转。

https://github.com/hsilived/SpinWheel

【讨论】:

【参考方案2】:

我认为您将不得不“伪造”挡板。

创建几个弯曲程度不同的挡板,并为车轮的给定位置更换正确的挡板。

没有理由让挡板与***进行物理交互 - 您可以使用代码减慢***的速度。事实上,对于如此简单的设置,根本不需要物理体代码。

【讨论】:

嗯,我该如何让它变得现实?有没有办法将挡板的动画与车轮的速度同步?另外,您知道为什么我在第一次旋转后无法再次旋转方向盘吗? 另外,另一个想法,不是最好让他们互动吗?否则,如何防止奖品轮停在中间区域(也就是将奖品分开并与“挡板”相互作用的钉子?【参考方案3】:

如果您只是为您的挡板制作了一个精灵,其中图像可以是几个(10 个左右)不同的“弯曲”位置之一,那可能会更好。那么你需要做的就是得到你的***的角度,然后用你拥有的帧数来修改它,然后乘以 Pi*2。那应该可以正常工作。我希望这是有道理的。

此外,该***创建可以使用 CGPathAddArc 而不是所有那些 CGPathAddLineToPoint 的单行

至于旋转,因为您使用的是精灵套件,所以只需使用您免费获得的物理东西,而不是使用 SKAction。

您应该考虑两种情况。 1.当用户拖动滚轮时,您需要旋转滚轮以立即匹配该移动(无动画) 2. 当用户放手时,您可以通过使用触摸事件的前一点(触摸的一部分)来计算它移动的速度,然后将其转化为对您的滚轮的角力。

【讨论】:

我实际上有一个我不久前制作的精灵套件***,它的工作原理与此类似。我看看能不能给你找到。但这里有一个 UIView 版本可以帮助您入门github.com/nathanfjohnson/SpinWheel 其实我已经找到了!我一直在使用它,同时在功能端寻找适合我需求的东西。如果你找到那个 Sprite Kit ***,请告诉我!

以上是关于SpriteKit - 如何使用“挡板”使奖品轮减速?的主要内容,如果未能解决你的问题,请参考以下文章

如果 spritekit 中正在播放来自另一个应用程序的音乐,如何使 AVAudioPlayer 静音

iOS Swift SpriteKit:如何使子精灵节点的位置和动作与其父精灵节点相同?

关于程序员35岁的坎:年龄不是挡板,图灵学院视频下载

无法使应用程序进入主菜单(Swift 4 SpriteKit)

SpriteKit 如何支持多种屏幕尺寸?

在 SpriteKit 中,如何制作我的英雄精灵的裁剪(带有透明段)图像?