OStack程序员社区-中国程序员成长平台

标题: ios - SpriteKit - 如何使用 "Flapper"使奖品轮盘减速? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:15
标题: ios - SpriteKit - 如何使用 "Flapper"使奖品轮盘减速?

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

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

我的问题是:

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

B) 如何使“挡板弯曲而不是简单地被插入”。

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

代码:

-(id)initWithSizeCGSize)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)touchesBeganNSSet *)touches withEventUIEvent *)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");
}



Best Answer-推荐答案


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

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

https://github.com/hsilived/SpinWheel

enter image description here

关于ios - SpriteKit - 如何使用 "Flapper"使奖品轮盘减速?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24003394/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4