我正在通過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