Question

ok i am new to coding and cocos2d i have this shooting code that will fire a projectile and when i try to fire on the left side of the screen it the projectile is fired down and right from the position of the ball?

heres my 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

this is the shooting method (i think it has something to do with the x & y offset?)

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!");
}    
Was it helpful?

Solution 2

I found the answer here Projectiles/Bullets direction Cocos2d you needed to do this,

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

OTHER TIPS

The best resource is here

Just give it a try. Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top