我正在通过tutorial 指导我们创建一个简单的游戏,用户可以在其中拍摄忍者。
在本教程中拍摄弹丸,已经实现follws:拍摄目标 - Cocos2D
- (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]];
}
所有什么是混淆了有关这部分比例的计算及其purpose.Why我们要计算在上述情况下的比例还是让我这样说,比例是什么意思?
问候,
石
你是完全正确的Davbryn。感谢您的回答。上面的代码的底线是要让点到达抛射物(武器)的位置。尽管点可能不在屏幕上。同样在这种情况下,从内存中释放弹丸非常重要,以避免内存泄漏。 – Nitish 2011-03-22 10:15:27
美好的一天,关于上述话题,你如何改变它,使用户可以在各个方向上开枪?我的意思不只是在右侧,当用户触摸左侧/向下/向上时,射弹将在当前触摸上射击?谢谢 – NoobMe 2012-01-10 01:40:05