SpriteKit - 如何使用“挡板”使奖励轮速度减慢?

时间:2014-06-02 21:13:15

标签: ios sprite-kit skspritenode

我正在尝试使用精灵套件制作一个财富风格的游戏。但是我被困住了。

好的,我有两个SKSpriteNode(带有物理实体)。轮子旋转得很好,还有“挡板”(也就是减速轮下来的东西,表明你赢了多少奖品。挡板不断向上推,而不是转动和放慢车轮。

我的问题是:

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

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

C)为什么车轮不会旋转多次? (注意:我尝试添加[wheel removeAllActions];在每次触摸之前添加:

CODE:

-(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");
}

3 个答案:

答案 0 :(得分:1)

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

创建几个以不同数量弯曲的挡板,并在车轮的给定位置交换正确的挡板。

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

答案 1 :(得分:1)

老问题,但对于那些用类似问题看这个问题的人。我用Spritekit和swift创建了一个开源旋转奖轮,它使用物理学来实现真实的动作和挡板控制。

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

https://github.com/hsilived/SpinWheel

enter image description here

答案 2 :(得分:0)

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

此外,该轮创建可以与CGPathAddArc一行而不是所有那些CGPathAddLineToPoint

至于旋转,因为你使用精灵套件,只需使用你免费获得的物理内容而不是使用SKAction。

你应该考虑两种情况。  1.当用户拖动车轮时,您需要旋转车轮以立即匹配该动作(无动画)  2.当用户放开时,您可以使用触摸事件的上一个点(触摸的一部分)计算它移动的速度,然后将其转换为对您的车轮施加的角度力。