Reputation: 14123
I was going through the tutorial
which guides us to create a simple game in which user can shoot a ninjas.
In the tutorial the shooting projectile has been implemented as follws :
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// Choose one of the touches to work with
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];
CCSprite *projectile = [CCSprite spriteWithFile:@"Projectile.png"
rect:CGRectMake(0, 0, 20, 20)];
projectile.position = ccp(20, winSize.height/2);
// Determine offset of location to projectile
int offX = location.x - projectile.position.x;
int offY = location.y - projectile.position.y;
// Bail out if we are shooting down or backwards
if (offX <= 0) return;
// Ok to add now - we've double checked position
[self addChild:projectile];
// Determine where we wish to shoot the projectile to
int realX = winSize.width + (projectile.contentSize.width/2);
float ratio = (float) offY / (float) offX;
int realY = (realX * ratio) + projectile.position.y;
CGPoint realDest = ccp(realX, realY);
// Determine the length of how far we're shooting
int offRealX = realX - projectile.position.x;
int offRealY = realY - projectile.position.y;
float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY));
float velocity = 480/1; // 480pixels/1sec
float realMoveDuration = length/velocity;
// Move projectile to actual endpoint
[projectile runAction:[CCSequence actions:
[CCMoveTo actionWithDuration:realMoveDuration position:realDest],
[CCCallFuncN actionWithTarget:self selector:@selector(spriteMoveFinished:)],
nil]];
}
All what is confusing about this part is the calculation of ratio and its purpose.Why we have to calculate the ratio in the above case or let me put it in this way, what does ratio signify?
Regards,
Stone
Upvotes: 0
Views: 1104
Reputation: 7176
If you look at the diagram in the tutorial it might help clear things up a little. Basically, when the user touches the screen you will have a location {x,y} where that touch took place. You also have a position of the Ninja.
using the offset between these points (xoff
and yoff
) you can create a right angled triangle, which gives the total distance between the two points (as its hypotenuse). You can use pythagoras theorem to calculate this.
However; the touch point isn't used for a 'destination value' in this case - instead the aim is that a ninja star should fly out in the direction of the touch, and continue moving until it is off screen.
(Typically, you would calculate difference between touch and ninja position as a vector, but I think the tutorial is trying to introduce Cocos2D functions so is keeping it simpler).
As Ray wants to use a CCMoveTo
on the ninja star sprite, he can't supply vector/speed directly so has to give a distinct endpoint. The star sprite will then move in a straight line until it reaches this point.
So, in this case the ratio is the ratio of the width of the smaller triangle to the height of the smaller triangle (ninja to touch). When he moves the projectile's target x position to the edge of the screen
int realX = winSize.width + (projectile.contentSize.width/2);
to ensure the star will shoot off the screen, he can use the same ratio to ensure the target's y position is as far up/down as it needs to be for the path of flight to remain at the same angle.
Upvotes: 2