开发者

Cocos2D set sprite position in relation to another sprite

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.

To figure this out I have been attempting to add (10.0, 10.0) to the location of my projectile sprite every step.开发者_如何学Go 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 is a held location property of the player sprite. Property is atomic and assigned. If that is wrong, please let me know.

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.

In summary:

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.

EDIT:

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 is a subclass of CCSprite so self.position is the position of the sprite on the screen.
  • _lastKnownPlayerLocation should be obvious but this is the position of the player sprite the last time it was checked.
  • _direction is a passed in value from a "analog stick". I get the angle and pass it to the class. The class uses ccpForAngle(float angle) to get a CGPoint and then uses ccpMult(CGPoint, float) to multiply it into whatever direction step I want (meaning what speed you want your shot to move at).

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

I hope this helps someone :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜