Cocos2d拍摄方法

时间:2012-01-20 23:55:17

标签: xcode cocos2d-iphone position touches

好吧我是编码和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&amp; 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!");
}    

2 个答案:

答案 0 :(得分:1)

最佳资源是here

试一试。 干杯

答案 1 :(得分: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));