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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2011, 08:13 AM   #1 (permalink)
iPhone SDK fanatics!
 
Join Date: Aug 2009
Location: Malaysia
Posts: 405
KennyChong is on a distinguished road
Default Cocos2D - How to make a sprite auto fire a bullet as a 10 seconds interval?

Hi All,

I am creating a game something like Plant Vs Zombies, with multiple sprites (the plants). Then,

I want them to auto-fire a bullet into incoming enemy (the zombies).

What is the best way of doing this?

I am not really good at object oriented, I tried the following (just pseudo to illustrate my approach):

in my init method of my code:

// schedule a repeating game loop
[self schedule:@selector(myGameLoop];



in myGameLoop method:

for (CCSprite thePlantSprite in thePlantSpriteArray) {

...(retrieve thePlantSprite position x and y)
[self fireBullet]; // call this method to fire a bullet from current sprite

}



I have logic problems:
1. this myGameLoop will get executed many times in a second and caused hundreds of bullets shot.
2. how can I limit to say a bullet can be shot by any sprite in every 2 seconds.

Thanks.
__________________
KennyChong
iPhone SDK Fanatic!

Last edited by KennyChong; 02-07-2011 at 09:09 AM.
KennyChong is offline   Reply With Quote
Old 02-07-2011, 02:55 PM   #2 (permalink)
Registered Member
 
missing_no's Avatar
 
Join Date: Feb 2011
Posts: 41
missing_no is on a distinguished road
Default

Quote:
Originally Posted by KennyChong View Post
Hi All,

I am creating a game something like Plant Vs Zombies, with multiple sprites (the plants). Then,

I want them to auto-fire a bullet into incoming enemy (the zombies).

What is the best way of doing this?

I am not really good at object oriented, I tried the following (just pseudo to illustrate my approach):

in my init method of my code:

// schedule a repeating game loop
[self schedule:@selector(myGameLoop];



in myGameLoop method:

for (CCSprite thePlantSprite in thePlantSpriteArray) {

...(retrieve thePlantSprite position x and y)
[self fireBullet]; // call this method to fire a bullet from current sprite

}



I have logic problems:
1. this myGameLoop will get executed many times in a second and caused hundreds of bullets shot.
2. how can I limit to say a bullet can be shot by any sprite in every 2 seconds.

Thanks.

I'm sure there is a better way but I always employ an int that will count up every game loop, and I use this as a timer (so when i = 60 I know a second passed [depending on you NStimer interval])

so you can easily say

int i = 0;

-(void)theGameLoop{

i ++;
...(retrieve thePlantSprite position x and y)

if(i%1000 == 0){
[self fireBullet]; // call this method to fire a bullet from current sprite
i = 0; //reset counter
}

}
missing_no is offline   Reply With Quote
Old 02-08-2011, 06:51 AM   #3 (permalink)
iPhone SDK fanatics!
 
Join Date: Aug 2009
Location: Malaysia
Posts: 405
KennyChong is on a distinguished road
Default

Thanks missing_no for your help. Will try this approach. Cheers.
__________________
KennyChong
iPhone SDK Fanatic!
KennyChong is offline   Reply With Quote
Old 02-08-2011, 02:03 PM   #4 (permalink)
Registered Member
 
Join Date: Jan 2010
Posts: 342
flamingliquid is on a distinguished road
Default

Or you can just use:
Code:
[self schedule:@selector(myGameLoop:) interval:2];
__________________
17 year old nerd.
Send me a PM. Maybe I can help.
flamingliquid is offline   Reply With Quote
Old 02-11-2011, 03:50 AM   #5 (permalink)
Indie Dev
 
hm50's Avatar
 
Join Date: May 2009
Location: South Bend, Indiana
Posts: 162
hm50 is on a distinguished road
Talking

I use the dt or delta time parameter passed to your update method or gameloop. If your app is set to run at 60fps, then you could just do:

Code:
 
myFloat += dt;
 
if (myFloat >= yourDelay){
   //fire bullet
   myFloat = 0;
}
Using this method is more likely to fire at the exact interval you want, because even if your app skips a frame do to low memory or whatever issue, the delta time will still indicate the time since the method was last fired.

Good Luck!!
__________________
iPhone 3G

Support Indie Devs!! (that goes for newbs too!)

Apps:
Spell Blocks
See Read Say
iStatus
myVIP
myMVP
hm50 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: 418
9 members and 409 guests
7twenty7, chemistry, ChrisYates, hussain1982, Retouchable, skrew88, SLIC, walex, xzoonxoom
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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