如何对齐精灵?

时间:2021-11-13 20:00:02

In cocos2d does anyone know how to center a sprite? Right now, I have a sprite that moves to where you touch on the screen. The problem is that the sprite is aligned to the lower left corner. This means that instead of moving down, if you touch just a little over the bottom the sprite will move up. Thanks in advance!

在cocos2d中有没有人知道如何集中精灵?现在,我有一个精灵移动到你在屏幕上触摸的地方。问题是精灵与左下角对齐。这意味着不是向下移动,如果你只是在底部稍微触摸一下,精灵就会向上移动。提前致谢!

Here is my code...

这是我的代码......

(BOOL) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *) event {
    UITouch *touch = [touches anyObject];
    CGPoint point = [touch locationInView: [touch view]];

    [mainSprite do:[MoveTo actionWithDuration:0.5 position:ccp(point.x, 480 - point.y)]];

    return YES;
}

2 个解决方案

#1


The default transformation anchor for sprites in Cocos2D is the center of the sprite, so it should be moving such that the center of the sprite ends up in the touched location as you have it now. Have you changed the sprite's transform anchor?

Cocos2D中精灵的默认转换锚点是精灵的中心,所以它应该移动,使精灵的中心最终在你现在拥有的触摸位置。你有没有改变精灵的变换锚?

The only other thing I can think of is that if your mainSprite is a child of another CocosNode then you may need to convert the touch coordinates to node space using this method:

我唯一能想到的是,如果你的mainSprite是另一个CocosNode的子节点,那么你可能需要使用这个方法将触摸坐标转换为节点空间:

- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;

...on the parent node. However, I doubt that is the problem. Sorry if this is unhelpful.

...在父节点上。但是,我怀疑这是问题所在。对不起,如果这是无益的。

EDIT: OP, if you read this, what version of Cocos2D are you using? I believe 0.8 (currently in the svn trunk) changes the way that anchoring works; for future reference it may be useful to others to know what you're working with.

编辑:OP,如果你读到这个,你使用的是哪个版本的Cocos2D?我相信0.8(目前在svn主干中)改变了锚定的工作方式;为了将来参考,了解您正在使用的内容可能对其他人有用。

#2


I got it working! For anyone that wants to know here is the code...

我搞定了!对于任何想知道这里是代码的人......

[mainSprite setTransformAnchor:ccp(24.0, 64.5)];

24 is half of the sprites width

24是精灵宽度的一半

64.5 is half of the sprites height

64.5是精灵高度的一半

#1


The default transformation anchor for sprites in Cocos2D is the center of the sprite, so it should be moving such that the center of the sprite ends up in the touched location as you have it now. Have you changed the sprite's transform anchor?

Cocos2D中精灵的默认转换锚点是精灵的中心,所以它应该移动,使精灵的中心最终在你现在拥有的触摸位置。你有没有改变精灵的变换锚?

The only other thing I can think of is that if your mainSprite is a child of another CocosNode then you may need to convert the touch coordinates to node space using this method:

我唯一能想到的是,如果你的mainSprite是另一个CocosNode的子节点,那么你可能需要使用这个方法将触摸坐标转换为节点空间:

- (CGPoint)convertToNodeSpace:(CGPoint)worldPoint;

...on the parent node. However, I doubt that is the problem. Sorry if this is unhelpful.

...在父节点上。但是,我怀疑这是问题所在。对不起,如果这是无益的。

EDIT: OP, if you read this, what version of Cocos2D are you using? I believe 0.8 (currently in the svn trunk) changes the way that anchoring works; for future reference it may be useful to others to know what you're working with.

编辑:OP,如果你读到这个,你使用的是哪个版本的Cocos2D?我相信0.8(目前在svn主干中)改变了锚定的工作方式;为了将来参考,了解您正在使用的内容可能对其他人有用。

#2


I got it working! For anyone that wants to know here is the code...

我搞定了!对于任何想知道这里是代码的人......

[mainSprite setTransformAnchor:ccp(24.0, 64.5)];

24 is half of the sprites width

24是精灵宽度的一半

64.5 is half of the sprites height

64.5是精灵高度的一半