Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-11-2010, 01:39 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 7
orangepie is on a distinguished road
Default Shoot Projectile Question.

Hey guys,

I was reading How To Make A Simple iPhone Game with Cocos2D Tutorial | Ray Wenderlich

on how to shoot an object to the location of your touch using cocos2d.

Unfortunatley, i'm not using cocos for my current project, and am really having trouble being able to shoot an object to the location of my touch.

I've written the code to create the bullet, load it into an array, and move it along it's path. Unfortunatley, i am unable to shoot it towards the direction of my touch.

Can someone help me be able to devise a method to do so, without using cocos2d?

Thanks,

Jeff.
orangepie is offline   Reply With Quote
Old 09-11-2010, 03:25 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Post the code that moves the bullet along the path. The general idea is to:

(1) get the direction from the object to the touch. This will generally be in the form of a slope, like dx/distance and dy/distance.

(2) figure out how far the bullet will move this frame; you'll need the slope above, the time elapsed, and the speed of the bullet. Something like this:

newx = oldx + (directionx * speed / time)
newy = oldy + (directiony * speed / time)

Once you get it working you can decide how to optimize - for example (directionx * speed) never changes over the life of the bullet, so you could calculate that once and store it as speedx and speedy instead.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-11-2010, 04:24 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 7
orangepie is on a distinguished road
Default

Thanks for the help:

here is the code to make the bullet:



[-(void)createBullet
{
UIImageView *Bullet = [[UIImageView alloc] initWithFrame:CGRectMake(10, 160, 20, 5)];
[Bullet setBackgroundColor:[UIColor orangeColor]];
[self.view addSubview:Bullet];
[BulletArray addObject:Bullet];
}

and to move the bullet:

-(void)step
{
for(int i = 0; i < [BulletArray count]; i++)
{
UIImageView *CurrentBullet = (UIImageView *)[BulletArray objectAtIndex:i];

CurrentBullet.center = CGPointMake(CurrentBullet.center.x + 50, CurrentBullet.center.y);

if(CGRectIntersectsRect(CurrentBullet.frame, Wall.frame))
{
printf("Bullet hits wall.");
[CurrentBullet removeFromSuperview];
CurrentBullet = nil;
[BulletArray removeObjectAtIndex:i];
}
}

}

Thanks for your help!
orangepie is offline   Reply With Quote
Old 09-11-2010, 06:04 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Firstly you need somewhere to store the direction or speed of each bullet. You could subclass UIImageView to create a Bullet class*, give it directionx and directiony properties, and then do something like this. You'll have to pass in the location of the touch:

Code:
-(void)createBullet:(CGPoint)touchPoint{
     Bullet *myBullet = [[Bullet alloc] initWithFrame:CGRectMake(10, 160, 20, 5)];
     [myBullet setBackgroundColor:[UIColor orangeColor]];
     [self.view addSubview:myBullet];
     [bulletArray addObject:myBullet];
     float dx = touchPoint.x - gun.center.x;
     float dy = touchPoint.y - gun.center.y;
     float distance = sqrt(dx*dx+dy*dy);
     myButton.directionx= dx / distance; 
     myButton.directiony= dy / distance; 
}
Note I changed your variable name to "myBullet" and named the class "Bullet." Classes usually start with an uppercase, and variables with a lowercase. Now you can move each bullet in the right direction:

Code:
CurrentBullet.center = CGPointMake(CurrentBullet.center.x + CurrentBullet.directionx*50, CurrentBullet.center.y + 
CurrentBullet.directiony*50);
I kept your current speed of 50 pixels per frame.

*subclassing UIImageView is bad long-term practice, but probably fine for your first few games. Eventually you'll want to separate the model from the view.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-12-2010, 03:39 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 7
orangepie is on a distinguished road
Default

Hey, thanks for the helpful reply.

...Quick question though,

Where does the mybutton come from?

Code:
"myButton.directionx= dx / distance; 
     myButton.directiony= dy / distance;  "
orangepie is offline   Reply With Quote
Old 09-12-2010, 10:13 PM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Sorry, that was a typo. Should have been myBullet.

Dictated but not compiled,
Smasher
__________________

Free Games!
smasher is offline   Reply With Quote
Old 09-13-2010, 11:24 AM   #7 (permalink)
Registered Member
 
Join Date: Aug 2010
Posts: 7
orangepie is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
Sorry, that was a typo. Should have been myBullet.

Dictated but not compiled,
Smasher
Pardon my ignorance,

But i am honestly having serious trouble implementing this function. I feel i may have tried to implement this the wrong way.

I'm looking to do something like this: (..I just can't for the life of my get it to run)

1: Fire the bullet from the position of my character.center ,

Code:
// if the cannonball is on the screen
   if (bulletOnScreen)
   {
      // update cannonball position
      bullet.x += TIME_INTERVAL * bulletlVelocity.x;
      bullet.y += TIME_INTERVAL * bulletVelocity.y;
..Then destroy the bullet if it goes off screen, so i can create another one.

I'm stuck.

However, i really appreciate your time and effort so far, Smasher.

Thanks!
orangepie is offline   Reply With Quote
Old 09-14-2010, 12:08 AM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Did you create a "Bullet" class that inherits from UIImageView like I suggested? That would allow you to give each bullet a different velocity variable. Then you should be able to write a nice little "moveBullet" method for the Bullet class:

Code:
-(void)moveBullet{
    CGPoint position = self.center;
    center.x += TIME_ELAPSED * velocityx;
    center.y += TIME_ELAPSED * velocityy;
    self.center=position;
}
You can't have a single variable bulletVelocity, since each bullet has its own velocity. Now you can just call [myBullet moveBullet] to move the bullet.

If you want to store velocity instead of direction then createBullet is pretty much the same except for the last two lines:
Code:
     myBullet.velocityx= dx / distance * 50; 
     myBullet.velocityy= dy / distance * 50; 
}
__________________

Free Games!

Last edited by smasher; 09-14-2010 at 12:12 AM.
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 342
11 members and 331 guests
bignoggins, carlandrews, cgokey, givensur, hzwegjxg, jenniead38, linkmx, mraalex, PixelInteractive, Trickphotostudios
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,657
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jenniead38
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:57 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0