2012-01-20 45 views
1

好的我是新來的編碼和cocos2d 我有這個射擊代碼,將射擊彈丸,當我嘗試在屏幕左側射擊時,射彈被擊倒並從右邊的位置球?Cocos2d拍攝方法

我的繼承人GamePlay.m

#import "GamePlay.h" 

CCSprite *player; 
CCSprite *grass; 
CCSprite *gameBg; 

@implementation GamePlay 

+(CCScene *) scene 
{ 
    // 'scene' is an autorelease object. 
    CCScene *scene = [CCScene node]; 

    // 'layer' is an autorelease object. 
    GamePlay *layer = [GamePlay node]; 

    // add layer as a child to scene 
    [scene addChild: layer]; 

    // return the scene 
    return scene; 
} 

-(id) init 
{ 
    // always call "super" init 
    // Apple recommends to re-assign "self" with the "super" return value 
    if((self=[super init])) 
    { 
     self.isTouchEnabled = YES; 

     gameBg = [CCSprite spriteWithFile:@"backgroundGame1.png"]; 
     gameBg.position = ccp(240,160); 
     [self addChild:gameBg]; 

     grass = [CCSprite spriteWithFile:@"grass.jpg"]; 
     grass.position = ccp(240,25); 
     [self addChild:grass]; 

     player = [CCSprite spriteWithFile:@"ball.png"]; 
     player.position = ccp(27,95); 
     [self addChild:player]; 

     x = 5; 
     y = 5; 
    } 
    return self; 
} 

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *myTouch = [touches anyObject]; 
    CGPoint point = [myTouch locationInView:[myTouch view]]; 
    point = [[CCDirector sharedDirector] convertToGL:point]; 

    if (point.x > 240 && point.y < 150) 
    { 
     [self unschedule:@selector(moveLeft)]; 
     [self schedule:@selector(moveRight) interval:.01]; 
    } 
    if (point.x < 240 && point.y < 150) 
    { 
     [self unschedule:@selector(moveRight)]; 
     [self schedule:@selector(moveLeft) interval:.01]; 
    } 
    NSLog(@"Touch Began"); 
    // Choose one of the touches to work with 

    if (point.y > 150) 
    { 
     UITouch *touch = [touches anyObject]; 
     CGPoint location = [touch locationInView:[touch view]]; 
     location = [[CCDirector sharedDirector] convertToGL:location]; 

     CGSize winSize = [[CCDirector sharedDirector]winSize]; 
     CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"]; 
     projectile.position = ccp(player.position.x,player.position.y); 

     int offX = location.x - projectile.position.x; 
     int offY = location.y - projectile.position.y; 

     [self addChild:projectile]; 

     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); 

     int offRealX = realX - projectile.position.x; 
     int offRealY = realY - projectile.position.y; 
     float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY)); 
     float velocity = 480/1; 
     float realMoveDuration = length/velocity; 

     [projectile runAction:[CCMoveTo actionWithDuration:realMoveDuration position:realDest]]; 

     NSLog(@"Shoot!"); 
    }  
} 

-(void)ccTouchesEnded:(NSSet *) touches withEvent:(UIEvent *)event 
{ 
    UITouch *myTouch = [touches anyObject]; 
    CGPoint point = [myTouch locationInView:[myTouch view]]; 
    point = [[CCDirector sharedDirector] convertToGL:point]; 

    [self unschedule:@selector(moveLeft)]; 
    [self unschedule:@selector(moveRight)]; 

    NSLog(@"Touch Ended"); 
} 

-(void) spriteMoveFinished: (id) sender 
{ 
} 


-(void)moveLeft 
{ 

    player.position = ccp(player.position.x - x, player.position.y); 
    if (player.position.x < 15) 
    { 
     player.position = ccp(16,player.position.y); 
    } 
} 

-(void)moveRight 
{ 
    player.position = ccp(player.position.x + x, player.position.y); 
    if (player.position.x > 465) 
    { 
     player.position = ccp(464,player.position.y); 
    } 
} 

@end 

這是拍攝方式(我認爲它有事情做與x & y偏移?)

if (point.y > 150) 
    { 
    UITouch *touch = [touches anyObject]; 
    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    CGSize winSize = [[CCDirector sharedDirector]winSize]; 
    CCSprite *projectile = [CCSprite spriteWithFile:@"projectile.png"]; 
    projectile.position = ccp(player.position.x,player.position.y); 

    int offX = location.x - projectile.position.x; 
    int offY = location.y - projectile.position.y; 

    [self addChild:projectile]; 

    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); 

    int offRealX = realX - projectile.position.x; 
    int offRealY = realY - projectile.position.y; 
    float length = sqrtf((offRealX*offRealX)+(offRealY*offRealY)); 
    float velocity = 480/1; 
    float realMoveDuration = length/velocity; 

    [projectile runAction:[CCMoveTo actionWithDuration:realMoveDuration position:realDest]]; 

    NSLog(@"Shoot!"); 
}  

回答

0

我在這裏找到了答案Projectiles/Bullets direction Cocos2d 你需要做到這一點,

// After adding the projectile: 
[self addChild:projectile]; 

// Add a scalar float: 
float scalarX = 1.0f; 

// And make it negative if the touch is left of the character: 
if (offX < 0.0f) scalar = -1.0f; 

// Then just multiply the realX by this scalar to make it point the correct way 
int realX = scalar * (winSize.width + (projectile.contentSize.width/2)); 
1

最好的資源是here

只要試一試。 乾杯

+0

這是我從得到這個代碼,但它仍然拍攝下來,權當我嘗試拍攝左 – mattblessed

+0

哪個教程它? –

+0

如何使用Cocos2D教程製作簡單的iPhone遊戲 - 您向我發送的鏈接與x和y偏移有關 – mattblessed