Cocos2D设置精灵相对于另一个精灵的位置

Cocos2D设置精灵相对于另一个精灵的位置

本文介绍了Cocos2D设置精灵相对于另一个精灵的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信这对于任何知道数学和编程(Cocos2D)的人来说都是一个简单的问题,但是当我谈到数学时,我是村里的白痴。

I am sure this is an easy question for anyone who knows anything about math and programming (Cocos2D), but when it comes to math I'm the village idiot.

我的游戏有一艘在太空飞行的船。船停留在屏幕的中心,摄像机随船移动。我试图添加从船上射出并且以相同速度移动的射弹,无论船舶在移动的速度如何。

My game has a ship that flies around in space. The ship stays in the center of the screen and the camera moves with the ship. I am trying to add projectiles that fire from the ship and move at the same speed no matter what speed the ship is "moving" at.

为了解决这个问题,已经尝试添加(10.0,10.0)到我的弹道精灵的位置每一步。这工作(虽然它总是到左上角),但是如果我试图得到也添加船舶或相机的运动到精灵位置以及(10.0,10.0),我得到它完全错了。

To figure this out I have been attempting to add (10.0, 10.0) to the location of my projectile sprite every step. This works (although it always goes to the upper left), however if I try to get also add the movement of the ship or camera to the sprite position as well as the (10.0, 10.0) I get it completely wrong.

这里是我目前正在做的:

Here is what I am currently doing at the moment:

CGPoint tempStep = ccpAdd(self.position, ccp(10.0, 10.0));
CGPoint tempSubStep = ccpSub(self.position, player.currentLocation);
NSLog(@"Difference:  (%fx, %fy)", tempSubStep.x, tempSubStep.y);
CGPoint finalStep = ccpAdd(tempStep, tempSubStep);
//  CGPoint tempSubStep = ccpSub(tempStep, player.currentLocation);
//  CGFloat diff = ccpDistance(tempStep, player.currentLocation);
//  CGPoint tempSubStep = ccpAdd(tempStep, ccp(diff, diff));

self.position = finalStep;

注释掉的东西是我一直在尝试的一些东西)。

Things that are commented out are some of the things I have been trying (although there are many other things).


  • self:Subclassed CCSprite

  • player.currentLocation是播放器的位置属性精灵。属性是atomic和assign。如果这是错误的,请让我知道。

我已经尝试添加投射到一个新的图层,播放器精灵,但是当播放器精灵移动(因此层)我的弹丸消失的那一刻。我已尝试过CCLayerColor和CCParallaxNode。

I have attempted to add the projectiles to a new layer and move the layer with the player sprite, but the moment the player sprite moves (and thus the layer) my projectiles disappear. I have tried CCLayerColor and CCParallaxNode.

总结

以发射源自船的位置的射击。

I would like to fire a shot originating from the position of the ship. I would like it to move away from the ship at a constant speed, regardless of the player ship's movement after the shot has been fired.

示例:玩家正在向着移动的方向移动屏幕右上角(可以这么说)和射击在同一个方向。无论玩家飞多快,玩家都不会超过射弹。或者,如果玩家突然向相反的方向,弹丸似乎不突然加快,但以看似相同的速度移动。

Example: Player is moving toward the top right of the screen (so to speak) and fires a shot in the same direction. The player does not overtake the projectile no matter how fast the player flies. Alternatively, if the player suddenly goes the opposite direction, the projectile does not seem to suddenly speed up but moves at seemingly the same speed.

感谢您的任何帮助提供。

Thank you for any help you can provide.

编辑:

Ok我想我可能已经得到了。真的很简单。

Ok I think I may have gotten it. Pretty simple really.

CGPoint tempStep = ccpAdd(self.position, _direction);
CGPoint playerDiff = ccpSub(player.currentLocation, _lastKnownPlayerLocation);
CGPoint finalStep = ccpAdd(tempStep, playerDiff);

self.position = finalStep;

_lastKnownPlayerLocation = player.currentLocation;

现在我只是想弄清楚如何在一个范围内获取半径。

Now I just have to figure out how to get a radius within a range.

推荐答案

确认这是对将来可能有此问题的任何人的完整解决方案:

Confirmed this is the full solution for anyone that might have this question in the future:

// Add the direction you want the sprite to move in to it's current position
CGPoint tempStep = ccpAdd(self.position, _direction);
// Subtract the player's current location from it's last known position
CGPoint playerDiff = ccpSub(player.currentLocation, _lastKnownPlayerLocation);
// Add the projectile sprite's desired new position to the change in the player's location
CGPoint finalStep = ccpAdd(tempStep, playerDiff);
// Set the position of the projectile sprite
self.position = finalStep;

// Store the new location of the player in the buffer variable
_lastKnownPlayerLocation = player.currentLocation;

// Move the Box2D body to match the sprite position.
// Not that this will break physics for this body, but I have
// my body set as a sensor so it will still report to the contact
// listener.
b2Vec2 moveToPosition = b2Vec2(self.position.x/PTM_RATIO, self.position.y/PTM_RATIO);
_body->SetTransform(moveToPosition, 0.0);




  • self是CCSprite的子类,所以self.position是

  • _lastKnownPlayerLocation应该是显而易见的,但这是播放器sprite上次检查时的位置。

  • _direction是从模拟棒传递的值。我得到角度,并把它传递给类。该类使用ccpForAngle(float angle)获取一个CGPoint,然后使用ccpMult(CGPoint,float)将它乘以我想要的任何方向步长(意味着你想要你的镜头移动的速度)。

  • 我的情况是使用Box2D来处理很多事情,所以最后两行与之相关。

    My situation is using Box2D for many things so the last 2 lines pertain to that.

    这有助于某人:)

    这篇关于Cocos2D设置精灵相对于另一个精灵的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:40