【问题标题】:Set frame of CCSprite?设置 CCSprite 的框架?
【发布时间】:2023-04-02 00:15:01
【问题描述】:

我有一张图片,我想用 CCSprite 显示不同的尺寸。我通常知道 CCSprite 只添加图像宽度和高度的图像,但我想知道是否有任何方法,所以我不必先手动缩放图像,如下所示,然后将图像提供给 ccpsrite?? p>

-(UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize image:(UIImage*)sourceImage
{
    UIImage *newImage = sourceImage;
CGSize imageSize = sourceImage.size;

/// Source image is of desired size or desired size 0x0, no change is done
if (!CGSizeEqualToSize(targetSize, CGSizeZero) && !CGSizeEqualToSize(imageSize, targetSize))
{
    CGFloat aspectRatio = imageSize.width / imageSize.height;
    CGFloat newAspectRatio = targetSize.width / targetSize.height;

    CGSize tempSize = targetSize;
    if (newAspectRatio < aspectRatio)
    {
        tempSize.width = targetSize.width * aspectRatio / newAspectRatio;
    }
    else
    {
        tempSize.height = targetSize.height * newAspectRatio / aspectRatio;
    }
    UIGraphicsBeginImageContext(targetSize);
    [sourceImage drawInRect:CGRectMake((targetSize.width - tempSize.width) / 2.0, (targetSize.height - tempSize.height) / 2.0, tempSize.width, tempSize.height)];
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}
return newImage;
}

【问题讨论】:

    标签: iphone ios cocos2d-iphone ccsprite


    【解决方案1】:

    您可以扩展您的CCSpriteCCSpriteCCNode 的子类,CCNode 具有以下属性:

    /** The scale factor of the node. 1.0 is the default scale factor. It modifies the X and Y scale at the same time. */
    @property(nonatomic,readwrite,assign) float scale;
    /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the X scale factor. */
    @property(nonatomic,readwrite,assign) float scaleX;
    /** The scale factor of the node. 1.0 is the default scale factor. It only modifies the Y scale factor. */
    @property(nonatomic,readwrite,assign) float scaleY;
    

    【讨论】:

      【解决方案2】:

      您可以添加一个自定义类,比如 FlexSprite 并执行此操作

      在 FlexSprite.h 中

      @interface FlexSprite : CCSprite
      
      +(FlexSprite*)spriteWithFile:(NSString *)imageName frame:(CGSize )size;
      -(id)initWithFile:(NSString *)imageName frame:(CGSize )size;
      @end
      

      在 FlexSprite.m 中

      #import "FlexSprite.h"
      @implementation FlexSprite
      -(id)initWithFile:(NSString *)imageName frame:(CGSize )size{
          self = [super initWithFile:imageName];
          if (self) {
              self.scaleX = size.width/self.contentSize.width;
              self.scaleY = size.height/self.contentSize.height;
          }
          return self;
      }
      
      +(FlexSprite *)spriteWithFile:(NSString *)imageName frame:(CGSize )size{
          return [[[self alloc]initWithFile:imageName frame:size]autorelease];
      }
      
      @end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-22
        • 2014-06-29
        相关资源
        最近更新 更多