如何使用cocos2d制作特定方向的箭头?

发布于 2024-12-15 05:03:01 字数 829 浏览 6 评论 0原文

我想要两个物体排成一排,如下所示,并让一个物体从下方拍摄它们,如下所示。

                0 0 0 0 0 0 




                     0

较低的物体始终处于其位置,并将拍摄到上面的一个, 我想制作箭头,因为用户将在 iPhone 的屏幕上移动手指,如下所示,

                 0 0 0 0 0 0 

                   \
                    \
                     \
                      0

如果移动手指,则

                  0 0 0 0 0 0                             
                      |
                      |
                      | 
                      0

在屏幕上向右移动一个手指,然后箭头应分别移动,如

                  0 0 0 0 0 0                             
                          /
                         /
                        /
                      0

我如何实现此逻辑,如果我有多个数组的图像并根据角度或其他方式使用它们,但我想要它的准确性。

谢谢

I want two few objects in one row, like below and have an object shooting them from downwards, like as below

                0 0 0 0 0 0 




                     0

The lower be always on its position, and will shoot to uper one,
I want to make arrow as user will move its finger on the screen of iPhone, like below

                 0 0 0 0 0 0 

                   \
                    \
                     \
                      0

if move finger then

                  0 0 0 0 0 0                             
                      |
                      |
                      | 
                      0

In move one finger on screen more towards right, then arrow should move respectively as

                  0 0 0 0 0 0                             
                          /
                         /
                        /
                      0

How can I implement this logic, should I have multiple images of arrays and use them according to angle, or what, but I want accuracy in it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2024-12-22 05:03:01

不需要多个图像......只需在 TouchMoved 方法中旋转箭头图像

即可尝试该代码。

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];


// Set up initial location of projectile
CGSize winSize = [[CCDirector sharedDirector] winSize];

// Rotate player to face shooting direction
CGPoint shootVector = ccpSub(location, sprite.position);
CGFloat shootAngle = ccpToAngle(shootVector);
CGFloat cocosAngle = CC_RADIANS_TO_DEGREES(-1 * shootAngle);

CGFloat curAngle = _player.rotation;
CGFloat rotateDiff = cocosAngle - curAngle;    
if (rotateDiff > 180)
    rotateDiff -= 360;
if (rotateDiff < -180)
    rotateDiff += 360;    

CGFloat rotateSpeed = 360; // degrees per second
CGFloat rotateDuration = fabs(rotateDiff / rotateSpeed);

[yoursprite runAction:[CCSequence actions:
                    [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
                    [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)],
                    nil]];

There is no need of multiple images.... just rotate your arrow image in touchesMoved method

Try that code.

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];


// Set up initial location of projectile
CGSize winSize = [[CCDirector sharedDirector] winSize];

// Rotate player to face shooting direction
CGPoint shootVector = ccpSub(location, sprite.position);
CGFloat shootAngle = ccpToAngle(shootVector);
CGFloat cocosAngle = CC_RADIANS_TO_DEGREES(-1 * shootAngle);

CGFloat curAngle = _player.rotation;
CGFloat rotateDiff = cocosAngle - curAngle;    
if (rotateDiff > 180)
    rotateDiff -= 360;
if (rotateDiff < -180)
    rotateDiff += 360;    

CGFloat rotateSpeed = 360; // degrees per second
CGFloat rotateDuration = fabs(rotateDiff / rotateSpeed);

[yoursprite runAction:[CCSequence actions:
                    [CCRotateTo actionWithDuration:rotateDuration angle:cocosAngle],
                    [CCCallFunc actionWithTarget:self selector:@selector(finishShoot)],
                    nil]];
如痴如狂 2024-12-22 05:03:01

检查以下链接以在触摸移动时旋转图像

,您必须限制所需角度之间的角度

旋转图像

check the following link to rotate image on touch move

here you have to restrict angle between your desire angles

rotate image

静待花开 2024-12-22 05:03:01

正如 Anshul 和 Nikhil 所说,旋转箭头应该足以解决您的问题。

另外请注意,您可能需要创建箭头精灵,并在其底部中心设置一个锚点( 0.5, 0.5 ),以避免注意到箭头左右移动,具体取决于您的设置。

最好的,

As Anshul and Nikhil stated, rotating the arrow should adequately answer your problem.

Note additionally that you will probably need to create your arrow sprite with an anchor set at its bottom center ( 0.5, 0.5 ), to avoid noticing your arrow shift left and right, depending on your setup.

Best,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文