附加到静态主体时更改 CCSprite 锚点
我正在花栗鼠中创建一个静态形状(使用 SpaceManager)并将 cpCCSprite 附加到它。
但是,我需要精灵的锚点偏离中心,但是当我更改精灵的锚点时,形状和形状会发生变化。精灵不再对齐。
因此,如果我像这样更改锚点,
[sprite setAnchorPoint:ccp(0.5, 0.3)];
精灵就会相应地绘制,但我希望形状会随之“移动”。这是我的意思的图片。
大炮的外观 http://www.tomelders.com/bin/cannon.png
左边是正确对齐的形状和精灵。我没有改变锚点。
右侧是锚点为 ccp(0.5, 0.3)
的精灵,
我还在每帧重新哈希静态形状。
这是它的创建方式
// create the sensor
sensor = [spaceMgr addRectAt:pPoint mass:STATIC_MASS width:53 height:81 rotation:0];
sensor->sensor = YES;
sensor->collision_type = 2;
//Create the sprite
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bownce-sprites-comic-sized.png"];
barrel = [[cpCCSprite node] initWithShape:sensor texture:texture rect:CGRectMake(3, 428, 53, 82)];
[self addChild:barrel];
// set the ancor point
[barrel setAnchorPoint:ccp(0.5, 0.3)];
[barrel setPosition:pPoint];
I'm creating a static shape in chipmunk (using SpaceManager) and attaching a cpCCSprite to it.
However, I need the anchor point of the sprite to be off center, but when I change the anchor point of the sprite, the shape & sprite no longer align.
So if I change the anchor point like this
[sprite setAnchorPoint:ccp(0.5, 0.3)];
The sprite is drawn accordingly, but I expected the shape to "move" with it. Here's a picture of what I mean.
how the cannon should look http://www.tomelders.com/bin/cannon.png
On the left is the shape and the sprite aligned properly. I've not changed the anchor point.
On the right is the sprite with an anchor point of ccp(0.5, 0.3)
I am also rehashing the static shape every frame.
Here's how it's created
// create the sensor
sensor = [spaceMgr addRectAt:pPoint mass:STATIC_MASS width:53 height:81 rotation:0];
sensor->sensor = YES;
sensor->collision_type = 2;
//Create the sprite
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"bownce-sprites-comic-sized.png"];
barrel = [[cpCCSprite node] initWithShape:sensor texture:texture rect:CGRectMake(3, 428, 53, 82)];
[self addChild:barrel];
// set the ancor point
[barrel setAnchorPoint:ccp(0.5, 0.3)];
[barrel setPosition:pPoint];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是对锚点工作方式的常见误解。锚点与节点的位置无关。相反,它是节点对象纹理的偏移量!
默认的anchorPoint为0.5,0.5表示纹理以节点位置为中心。如果将anchorPoint 设置为0,0,则纹理的左下角与节点的位置对齐。纹理有效地向右和向上移动。然而节点的位置没有改变。
由于形状与节点的位置对齐,因此当您更改锚点时它不会移动。解决此问题的最简单方法是更改图像,例如通过仅使用透明颜色将其尺寸增加到一侧。我希望这是可以理解的。
This is a common misconception of how anchorPoints work. The anchorPoint has nothing to do with the node's position. Instead, it is the offset for the texture of the node object!
The default anchorPoint is at 0.5, 0.5 which means the texture is centered on the node's position. If you set the anchorPoint to 0,0 then the lower left corner of the texture aligns with the node's position. Effectively the texture moves to the right and up. The node's position has not changed however.
Since the shape is aligned with the node's position, it will not move when you change the anchorPoint. The easiest way to fix this is to change the image, for example by increasing its dimenions to one side, using only the transparent color. I hope that's understandable.