Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7458811
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:30:10+00:00 2026-05-29T13:30:10+00:00

ok i am new to coding and cocos2d i have this shooting code that

  • 0

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!");
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T13:30:11+00:00Added an answer on May 29, 2026 at 1:30 pm

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm coding a new website that will need users to enter their mobile phone
We implemented new coding standards, which call for our private members to have a
New to iOS coding, so this is probably more of a syntax question than
I am new to coding (never taken a CS class and have a very
When coding new javascript heavy websites, which order or web browser do you code
I'm pretty new to coding with streams but now I have to do it
I have implemented a quadtree in Mathematica. I am new to coding in a
I'm new to coding in general, and I'm wanting to build a website that
I'm fairly new to coding in T-SQL and have run into an issue I
Quite new to coding for android but this issue has me tearing my hair

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.