【问题标题】:Scale a CCSprite from the side and not from the center从侧面而不是从中心缩放 CCSprite
【发布时间】:2012-09-09 09:33:19
【问题描述】:

我正在尝试制作自定义进度条。我想要做的是让“barMask.png”有它的 X 比例取决于数字的百分比。我试过这样做:

barBack = CCSprite::create( "barBack.png" );
this -> addChild( barBack );

barMask = CCSprite::create( "barMask.png" );
barMask -> setPosition( barBack -> getPosition( ) );
this -> addChild( barMask );

然后关于更新方法

// Scale the width of barMask depending on the percentage of the progress.
barMask -> setScaleX( CURRENT_AMOUNT / TOTAL_AMOUNT );

然而,精灵是这样缩放的:

Frame 1: [ |||||||||| ]
Frame 2: [  ||||||||  ]
Frame 3: [   ||||||   ]

它缩小到中间。我该怎么做才能让它向左/向右缩小?像这样:

Frame 1: [ |||||||||| ]
Frame 2: [ |||||||||  ]
Frame 3: [ ||||||||   ]
Frame 4: [ |||||||    ]

我知道 CCProgressTimer,但我想为进度条使用纯精灵。

【问题讨论】:

    标签: c++ xcode cocos2d-iphone cocos2d-x


    【解决方案1】:

    将 spriet 的 anchorPoint 属性更改为 (0.f, 0.5f)。所有的转换都是相对于节点的锚点进行的。默认情况下,精灵的锚点是 (0.5f, 0.5f)。这就是为什么你的缩放默认是相对于你的精灵的中心的。

    【讨论】:

    • 谢谢。我之前查过 Anchor Points,但文档没有足够的信息来说明它的使用情况。
    【解决方案2】:

    另一种可能实现的方法是使用 draw 方法和一点 openGL。我在我的太空射击应用程序中将它用于敌人的健康条,根据敌人剩余的生命值进行缩放。它改编自 Ray Wenderlich 的教程之一。无论如何,这是我的绘制方法,它遍历屏幕上的每个老板“怪物”并绘制他们的健康条。这个可以修改,进度条用的方法...

    - (void)draw {
    
    for (CCSprite *target in _targets) {
        Monster *monster = (Monster *)target;
    
        if (monster.monsterisboss == YES) {
    
            static int lineBuffer = 5;
            int lineHeight = 7;
            int startY = monster.position.x - (monster.contentSize.width/2);
            int endY = monster.position.x + (monster.contentSize.width/2) - lineBuffer;
            int actualX = monster.position.y + (monster.contentSize.height/2) + lineHeight*2;
    
            static int maxColor = 200;
            static int colorBuffer = 55;
            float percentage = ((float) monster.hp) / ((float) monster.maxHp);
            int actualY = ((endY-startY) * percentage) + startY;
            int amtRed = ((1.0f-percentage)*maxColor)+colorBuffer;
            int amtGreen = (percentage*maxColor)+colorBuffer;
    
            glEnable(GL_LINE_SMOOTH);
    
            glLineWidth(lineHeight);        
            glColor4ub(amtRed,amtGreen,0,255);
            ccDrawLine(ccp(startY, actualX), ccp(actualY, actualX));
        }
    }
    
    [super draw];
    

    }

    edit 看起来我的代码块遗漏了最后一个右花括号 - 如果您想尝试上面的绘制方法,请不要忘记!

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 1970-01-01
      • 2018-03-20
      • 2015-11-22
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多