objective-c - 如何在 iOS 中切片/剪切 Sprite (Core Graphics)

标签 objective-c ios core-graphics sprite slice

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

目前,我只是缩小尺寸并复制 Sprite 。类似这样的东西。提前致谢。

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

最佳答案

是否需要具体的分割线? Fruit Ninja 只会生成两半的水果,就好像它被从中间切开一样,这很容易做到:

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

关于objective-c - 如何在 iOS 中切片/剪切 Sprite (Core Graphics),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12234626/

相关文章:

ios - 将 RACCommand 与异步网络操作结合使用

ios - SecItemAdd 和 SecItemCopyMatching 返回错误代码 -34018 (errSecMissingEntitlement)

ios 5 : Calling a block in iPhone twice for different values

ios - 如何在 iOS 中以编程方式绘制到显示屏?

ios - 如何计算真实世界的对象大小?

objective-c - 可以在 Objective-c 中访问的协议(protocol)的扩展上定义 Swift 方法吗

外部库中的 iOS Core Foundation 内存泄漏

iphone - 如何在prepareForSegue之前进行内部润色

ios - 为什么我的委托(delegate)变为零?

objective-c - 在 Core Graphics 中倾斜路径/形状