2012-03-26 40 views
7

Tôi đang phát triển "Paper Toss' cho iphone sử dụng cocos2d & Tôi muốn biết rằng làm thế nào để thực hiện 3D phối cảnh vào điều này, bởi vì khi chúng tôi đang ném bóng giấy vào thùng, chúng tôi có để có được những 3D feel.I'am gắn mã mà tôi đã làm, sử dụng này tôi đã có một đường thẳng motion.Please giúp tôi ..phối cảnh 3D trong iphone trò chơi sử dụng 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:@"ball.png" 
              rect:CGRectMake(0, 0, 40, 40)]; 
projectile.position = ccp(winSize.width/2,20); 

// 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 (offY <= 0) return; 

// Ok to add now - we've double checked position 
[self addChild:projectile]; 

// Determine where we wish to shoot the projectile to 
int realY = winSize.height + (projectile.contentSize.width/2); 
float ratio = (float) offX/(float) offY; 
int realX = (realY * ratio) + projectile.position.x; 
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]]; 
//add to the projectiles array 
projectile.tag = 2; 
[_projectiles addObject:projectile]; 

} *

Trả lời

7

Cuối cùng tôi đã hoàn thành tung giấy sử dụng cocos2d.I thực hiện đường cong Bezier và ở đây nó là,

// Bezier curve control points 
    bezier.controlPoint_1 = ccp(location.x-CONTROL_POINT1_X, CONTROL_POINT1_Y); 
    bezier.controlPoint_2 = ccp(location.x-CONTROL_POINT2_X, CONTROL_POINT2_Y); 
    bezier.endPosition = ccp(location.x-CONTROL_POINT1_X,distance); 

    // Motion along bezier curve and finally call a function 
    [projectile runAction:[CCSequence actions: 
          [CCAutoBezier actionWithDuration:DEFAULT_ACTION_DURATION bezier:bezier], 
          [CCCallFuncN actionWithTarget:self selector:@selector(collisionCheck:)], nil]]; 
+5

Tôi đã thêm một fan hâm mộ ở 3 cấp độ và bây giờ trò chơi của tôi luks tuyệt vời :) –

+0

Bạn có thể vui lòng cho tôi biết thêm về CONTROL_POINT – Muniraj

+0

@TonyMac: - Chúng tôi có thể sử dụng đường cong Bezier cho loại chuyển động. Nhưng trong giấy quăng tôi đã thấy kích thước của giấy cũng khác nhau. Bạn làm như thế nào ? u cũng có thể cho tôi biết làm thế nào chúng ta có thể thực hiện hiệu ứng nảy khi giấy va chạm các cạnh của thùng rác? cảm ơn – Tornado

3

Chỉ cần mở rộng hình ảnh sprite của bạn do đó, nó sẽ nhỏ hơn "xa hơn" nó được.

+2

vâng, chia tỷ lệ phải được thực hiện.Nhưng nó sẽ không cung cấp góc nhìn chính xác .. –

+0

Ý của bạn là gì với "chế độ xem phối cảnh chính xác"? –

1

Hey bạn có thể sử dụng bezier curve cho 3d phối cảnh trong cocos2d.

bezier.controlPoint_1 = ccp(location.x-CONTROL_POINT1_X, CONTROL_POINT1_Y); 
    bezier.controlPoint_2 = ccp(location.x-CONTROL_POINT2_X, CONTROL_POINT2_Y); 
Các vấn đề liên quan