【问题标题】:How to slice/cut sprites in iOS (Core Graphics)如何在 iOS 中切片/剪切精灵(核心图形)
【发布时间】:2012-08-27 09:50:00
【问题描述】:

我正在开发一款游戏,我想在其中添加一个适当的切片功能。所以当一个精灵切片时,应该创建 2 个新的精灵。请检查here

目前,我只是在缩小尺寸并复制精灵.. 像这样的东西.. 在此先感谢..

 - (BOOL) sliceSprite: (Sprite *) sprite withPath: (UIBezierPath *) slicePath


 {

            CGSize size = sprite.size;
            size.width /= 2; 
            size.height /=2;
            sprite.size = size;
            sprite.sliced = YES;

            Sprite *newSprite = [[Sprite alloc] initWithImage: sprite.image];

            newSprite.position = sprite.position;
            newSprite.size = size;
            newSprite.sliced = YES;
            newSprite.inView = YES;
            newSprite.xVelocity = SLICE_SPEEDUP * sprite.yVelocity;
            newSprite.yVelocity = SLICE_SPEEDUP * sprite.xVelocity;
            newSprite.angularVelocity = -SLICE_REVUP * sprite.angularVelocity;

            [sprites addObject: newSprite];
            [newSprite release];

        sprite.angularVelocity = SLICE_REVUP * sprite.angularVelocity;
            sprite.xVelocity = -SLICE_SPEEDUP * sprite.xVelocity;
            sprite.yVelocity = -SLICE_SPEEDUP * sprite.yVelocity;

            return YES;
    }

- (void) sliceSpritesInSwipePath
{
    CGRect swipeRect = [swipePath bounds];


        for (NSUInteger i = 0; i < [sprites count]; i++)
        {
                Sprite *sprite = [sprites objectAtIndex: i];

                if ([sprite intersectsWithPathInArray: swipePoints
                                               inRect: swipeRect])
                        if ([self sliceSprite: sprite withPath: swipePath])
                        {

                                [self resetSwipe];

                                if (![sliceSound isPlaying])
                                        [sliceSound play];

                break;
                        }
        }

}

【问题讨论】:

  • 如何修改我现有的代码,让它像水果忍者一样?
  • 类似 raywenderlich.com/14393/… 但我没有使用 cocos2d..
  • sprite.image UIImage?

标签: objective-c ios core-graphics sprite slice


【解决方案1】:

需要具体的分割线吗?水果忍者只生成两半水果,就像从中间裂开一样,这很容易做到:

  • 创建两个宽度为原始精灵一半的精灵
  • 将它们放置在原始精灵水平中心线的 1/4 和 3/4 处
  • 添加旋转/加速等
  • 修改纹理坐标,使左侧精灵具有左侧纹理,右侧精灵具有右侧纹理。

【讨论】:

    【解决方案2】:

    既然您在这里使用 CoreGraphics,为什么不在绘制精灵时简单地使用剪切路径?

    复制要切片的精灵,然后应用简单的多边形来掩盖两半作为它们各自的剪切路径。您需要的函数称为CGContextClip,可以在here 找到简短教程。

    编辑:教程列出了这个例子:

    CGContextBeginPath (context);
    CGContextAddArc (context, w/2, h/2, ((w>h) ? h : w)/2, 0, 2*PI, 0);
    CGContextClosePath (context);
    CGContextClip (context);
    

    这会将当前路径设置为圆形,然后将当前路径应用为剪切区域。

    【讨论】:

    • 你能详细说明一下这项技术吗?
    猜你喜欢
    • 2010-11-07
    • 2012-12-06
    • 2013-08-01
    • 2016-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多