problem with game code!!!!
I am making a basic game that shoots when i touch the screen!! It runs fine except for one little detail the bullet moves faster each time i tap the screen.
Here is the source if anyone can help
Thanks
Chris
Here is the source if anyone can help
Code:
#import "GameTestViewController.h"
@implementation GameTestViewController
@synthesize character;
@synthesize bullet;
@synthesize count;
- (void)viewDidLoad {
count=0;
bulletpos = CGPointMake(20.0,20.0);
characterpos = CGPointMake(0,0);
bullet.hidden=YES;
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent :(UIEvent *)event {
bullet.hidden=NO;
count=1;
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
-(void) onTimer {
if(count==1){
character.center = CGPointMake(character.center.x+characterpos.x,220+220);
bullet.center= CGPointMake(bullet.center.x+bulletpos.x,220+220);
if(bullet.center.x > 320 || bullet.center.x < 0){
bullet.center= CGPointMake(character.center.x+characterpos.x,220+220);
bullet.hidden=YES;
count=0;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
bullet.hidden=YES;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)dealloc {
[bullet release];
[character release];
[super dealloc];
}
@endChris
You're creating a new NSTimer every time -touchesBegan:withEvent: is called. You need to either recycle or invalidate the old one.
Can you please point me into the right direction on how to do that?
Thanks
Chris
Thanks
Chris
Actually i think i might have it does this look better
Does this look better? It works fine now!! But i was just making sure there isn't anything else you see!!
Thanks
Chris
Code:
- (void)viewDidLoad {
count=0;
bulletpos = CGPointMake(20.0,20.0);
characterpos = CGPointMake(0,0);
bullet.hidden=YES;
[NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent :(UIEvent *)event {
bullet.hidden=NO;
count=1;
}
-(void) onTimer {
if(count==1){
character.center = CGPointMake(character.center.x+characterpos.x,220+220);
bullet.center= CGPointMake(bullet.center.x+bulletpos.x,220+220);
if(bullet.center.x > 320 || bullet.center.x < 0){
bullet.center= CGPointMake(character.center.x+characterpos.x,220+220);
bullet.hidden=YES;
count=0;
}
}
}Does this look better? It works fine now!! But i was just making sure there isn't anything else you see!!
Thanks
Chris
That'll more or less work, though on the iPhone, you'll want to be very careful about leaving NSTimers running. They consume power unnecessarily, which is undesirable anywhere but particularly of concern on the phone, since it's a mobile device with limited battery.
This is a fairly basic question compared to what it looks like you're trying to do with the code. I'd recommend you study the fundamentals of programming, write very simple test programs, and read lots of Apple documentation before you get too deeply into unknown territory.
This is a fairly basic question compared to what it looks like you're trying to do with the code. I'd recommend you study the fundamentals of programming, write very simple test programs, and read lots of Apple documentation before you get too deeply into unknown territory.
oh i have read beginning iphone development, and learn c on the mac. so i know some stuff about proggrmaing i am just a basic noob at game dev. Can you please post some code to stop the timer?
Thanks
Chris
Thanks
Chris
[timer invalidate];
Its right there in the docs, there are only 8 instance methods for NSTimer, I find it hard to believe you read the docs for NSTimer. Slow down and take your time, you can't rush programming.
You need to slow down when you post here and read over what you are writing as well.
Its right there in the docs, there are only 8 instance methods for NSTimer, I find it hard to believe you read the docs for NSTimer. Slow down and take your time, you can't rush programming.
You need to slow down when you post here and read over what you are writing as well.
hello Chris,
To make this work you need to declare bulletTimer as an NSTimer in your .h file.
Code:
- (void)viewDidLoad {
count = 0;
bullet.hidden = YES;
[super viewDidLoad];
}
- (void)touchesBegan:(NSSet *)touches withEvent :(UIEvent *)event {
if(count == 0) {
bullet.center = CGPointMake(character.center.x,220+220);
bulletTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
count = 1;
}
}
- (void)onTimer {
CGFloat bulletSpeedX = 10;
bullet.center = CGPointMake(bullet.center.x+bulletSpeedX,220+220);
if(bullet.center.x > 320 || bullet.center.x < 0){
bullet.hidden = YES;
count = 0;
[bulletTimer invalidate];
}
}To make this work you need to declare bulletTimer as an NSTimer in your .h file.
iPlayful : games for iPhone and iPod Touch
Thansk, but if i invalidate it there won't that mean that i cant shoot anymore?
chrism Wrote:Thansk, but if i invalidate it there won't that mean that i cant shoot anymore?
Read the docs and understand what you have written the code to do.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Difficult Game to Code? | mickyg | 10 | 4,958 |
Mar 12, 2012 04:31 PM Last Post: sealfin |
|
| Problem with making a game with the iPhone Game Development book | MrPenguin9 | 2 | 4,369 |
Feb 1, 2010 09:13 AM Last Post: MrPenguin9 |
|

