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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 05-19-2010, 06:20 AM   #76 (permalink)
Registered Member
 
Join Date: May 2010
Posts: 2
Red face source code

Quote:
Originally Posted by iSdkDev View Post
@SMASHER OR ANY1ELSE "Ok soo i did this code and i have those images falling but there's a bug!I have a label that turns to 1 from 0 when that falling image collides with an image below BUT there is a bug when i starts falling down it just automatically adds 1 to my label where as i want tht 1 to add only when it collides!!Please help"
Here is the code



need the full source code for falling blocks(tetris)
sunilhs86 is offline   Reply With Quote
Old 07-01-2010, 12:44 AM   #77 (permalink)
Registered Member
 
Join Date: Jul 2009
Posts: 140
Default

Hey there,
I was wondering if anyone had a "final project" of random rocks. There are just too many bits and pieces of code for me to put it together.
ybrikeeg is offline   Reply With Quote
Old 07-15-2010, 12:52 PM   #78 (permalink)
Registered Member
 
thecyberduck's Avatar
 
Join Date: Jun 2010
Posts: 58
Default

Quote:
Originally Posted by ybrikeeg View Post
Hey there,
I was wondering if anyone had a "final project" of random rocks. There are just too many bits and pieces of code for me to put it together.
yeah bump for the final project
thecyberduck is offline   Reply With Quote
Old 01-21-2011, 09:56 AM   #79 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default Collision won't work

Hi I am a real newbie (hence the delay in replying to this thread) but would really appreciate some help.

I can't get my code to collide with an IBOutlet called UIImageView* lightsabergreenImage;
I think I've confused myself! So how can I get it to collide?
Thanks in advance.





-(void) checkCollision {
for(int i = 0; i<[laserArray i]; i++){
UIImageView* redlaserImage = [laserArray objectAtIndex:i];

if(CGRectIntersectsRect(redlaserImage.frame, lightsabrgreenImage.frame)){

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Collision" message:@"the images collide" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
}


-(void) accelerometerUIAccelerometer *)accelerometer didAccelerateUIAcceleration *)accel
{
float newX = lightsabergreenImage.center.x + (accel.x * 22);
if(newX > 30 && newX < 290) lightsabergreenImage.center = CGPointMake(newX, lightsabergreenImage.center.y);
}

-(void) createLasers
{
if(laserArray == nil) {
laserArray = [[NSMutableArray alloc] init];
}
UIImage *redlaserImage = [UIImage imageNamed:@"redlaser.png"];
myredLaser = [[UIImageView alloc] initWithImage:redlaserImage];
UIImageView *newView;

for(int i = 0; i<1; i++){
newView = [[UIImageView alloc] initWithImage:redlaserImage];
// use the arc4random() function to randomize up our flake attributes

int x = arc4random() % 300;
int y = -arc4random() % 480;
double scale = 1 / round(arc4random() % 100) + 1.0;
double speed = 1 / round(arc4random() % 100) + 1.0;

newView.center = CGPointMake(x, y);

// set the flake start position
newView.frame = CGRectMake(x, -100.0, 18.0 * scale, 50.0 * scale);
newView.alpha = 1.0;


[self.view addSubview:newView];
[laserArray addObject:newView];
[newView release];
}
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

UIAccelerometer *theAccel = [UIAccelerometer sharedAccelerometer];
theAccel.updateInterval = 1.0f / 30.0f;
theAccel.delegate = self;


[self.view addSubview:newView];
redlaserImage = [UIImage imageNamed:@"redlaser"];
[NSTimer scheduledTimerWithTimeInterval0.05) target:self selector:@selector(moveLasers) userInfo:nil repeats:YES];
[self createLasers];
}


-(void) moveLasers {

for(UIImageView* myredLaser in laserArray){
CGPoint newCenter = myredLaser.center;
newCenter.y = newCenter.y + 10; //10 changes the speed

if(newCenter.y > 480){
newCenter.y=0;
newCenter.x = arc4random()%300;
}
myredLaser.center = newCenter;
}
}
David Axe is offline   Reply With Quote
Old 01-21-2011, 12:47 PM   #80 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Who is calling checkCollision? Does it every get called? Movelasers should probably call checkCollision at the end of the method.

If it's still not working, NSLog redlaserImage and lightsabrgreenImage in the checkCollision method.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-21-2011, 01:59 PM   #81 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default Collide problems

Thanks for the suggestion smasher but it didn't work - the app runs but still with no collisions, even with Movelasers calling checkCollisions.

I can't understand what the output means. I hope you can help. Thanks
Here is the output:


[Session started at 2011-01-21 18:50:02 +0000.]
Loading program into debugger…
Program loaded.
target remote-mobile /tmp/.XcodeGDBRemote-140-47
Switching to remote-macosx protocol
mem 0x1000 0x3fffffff cache
mem 0x40000000 0xffffffff none
mem 0x00000000 0x0fff none
run
Running…
[Switching to thread 11523]
[Switching to thread 11523]
sharedlibrary apply-load-rules all
continue

kill
quit

The Debugger has exited with status 0.

Last edited by David Axe; 02-18-2011 at 04:10 PM.
David Axe is offline   Reply With Quote
Old 01-22-2011, 12:47 AM   #82 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

There's nothing useful in that console log. I missed this part though:

Code:
for(int i = 0; i<[laserArray i]; i++){
That should be [laserArray count]; it'll crash otherwise, so you must have already fixed this.

The code you poster looks OK, it ought to work unless I'm missing something, or unless lightsabergreenImage is empty or has a different parent than self.view.

I'd add a few NSLogs to that method, just to see that it's getting called and that the array and lightsabrgreenImage are not empty.

Code:
-(void) checkCollision {

	NSLog(@"The array contains: %@", laserArray);
	NSLog(@"lightsabrgreenImage is: %@", lightsabrgreenImage);

	for(int i = 0; i<[laserArray count]; i++){
		UIImageView* redlaserImage = [laserArray objectAtIndex:i];

		if(CGRectIntersectsRect(redlaserImage.frame, lightsabrgreenImage.frame)){

			UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Collision" message:@"the images collide" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
}
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-22-2011, 06:34 AM   #83 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

Hi Smasher I did as you suggested and the log wasn't any different to the previous one I posted. What does that mean? Are the array and lightsabrgreenImage empty? Or is it something else? Does the fact that the lightsabergreenImage is on IBOutlet make any difference? Thanks again.
David Axe is offline   Reply With Quote
Old 01-22-2011, 11:02 AM   #84 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Quote:
Originally Posted by David Axe View Post
Hi Smasher I did as you suggested and the log wasn't any different to the previous one I posted. What does that mean? Are the array and lightsabrgreenImage empty? Or is it something else? Does the fact that the lightsabergreenImage is on IBOutlet make any difference? Thanks again.
If you did not see "The array contains:" in your console after adding those NSLog lines then checkCollision is still not getting called. Post your new moveLasers and checkCollision methods In [code] [ /code] tags.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-22-2011, 11:17 AM   #85 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

Hi, I didn't see "The array contains:" in the console. Here is the code:

Code:
-(void) moveLasers {
	
	for(UIImageView* myredLaser in laserArray){
		CGPoint newCenter = myredLaser.center;
		newCenter.y = newCenter.y + 10; //10 changes the speed
		
		if(newCenter.y > 480){
		newCenter.y=0;
		newCenter.x = arc4random()%300;
	}
	myredLaser.center = newCenter;
}
}

-(void) checkCollision {
	
	NSLog(@"the array contains: %@", laserArray);
	NSLog(@"lightsabergreenImage is: %@", lightsabrgreenImage);
	
	for(int i = 0; i<[laserArray count]; i++){
		UIImageView* redlaserImage = [laserArray objectAtIndex:i];
		
		if(CGRectIntersectsRect(redlaserImage.frame, lightsabrgreenImage.frame)){
			
			UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Collision" message:@"the images collide" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
}
David Axe is offline   Reply With Quote
Old 01-22-2011, 11:34 AM   #86 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Ok, who is calling checkCollision? It doesn't look like anyone is calling checkCollision.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-22-2011, 12:17 PM   #87 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

Hi

Yes the lightsabergreenImage is null

Code:
lightsabergreenImage is: (null)
 the array contains: ("<UIImageView: 0x150ec0; frame = (209.875 454.653; 18.25 50.6944); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x150fa0>>")
What does the null mean?
I have not allocated and initiated lightsabergreenImage if that is what I need to do. I created it from within interface builder, would this be the problem?

Please excuse my newbie-ness
Thanks
David Axe is offline   Reply With Quote
Old 01-22-2011, 12:35 PM   #88 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

If an IBOutlet is null then you probably forgot to hook up that outlet in interface builder. Go back to interface builder, right-click file's owner, and drag the (+)circle to the image. Then save the xib file and rebuild.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-22-2011, 12:42 PM   #89 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

Hi

The image was all set up in interface builder, what other reason would it come back as null?
David Axe is offline   Reply With Quote
Old 01-22-2011, 12:58 PM   #90 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

I'll be be null until after; that's normal. If it's still null during gameplay, while you see it on the screen, then either it's not connected to the image in interface builder or, less likely, you set it back to nil somehow.

Post your .h file for this class - In the code you've posted you've switched back and forth between the names "lightsabergreenImage" and "lightsabrgreenImage" - I wonder if you misspelled it in either the variable or the property.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-22-2011, 01:17 PM   #91 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

That's it - well done smasher! I had incorrectly spelt lightsabeImage. Thank you so much.
Now that is working I am going to try and get the lasers to deflect the lightsaber.
David Axe is offline   Reply With Quote
Old 01-23-2011, 11:34 AM   #92 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

Hi again

I have been testing my code and the accuracy of the collision is not great. I have just added rotation to the lightsaber. The collision alert only happens when the laser hits the saber at a bottom of the image and I need it to alert when it hits any part.

Should I add a holder? Is there a timer issue? Any ideas greatly appreciated.
David Axe is offline   Reply With Quote
Old 01-23-2011, 12:34 PM   #93 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

How are you doing the rotation? There's a warning in the docs that if you use view.transform then the value of view.frame is undefined and therefore should be ignored.

Either way CGRectIntersectsRect is only good for checking if two unrotated rectangles are touching. You need something else - you need a way to represent your light saber as a line, and figure out if it's intersecting the lasers. The problem you're working on is harder than random rocks, and will require some trig.

I have some thoughts but no code. I'd probably write a method to tell me which side of the saber a laser is on - left or right. If moving the laser down the screen changes which side of the saber it's on (and distance < saber length) that's a collision, and you can reverse the direction of the laser.

Because you want to do deflections, you probably need to combine the movement and collision checking into one loop. If you do collision checking AFTER you move the laser into a new position you don't have enough information about the old position.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-23-2011, 12:55 PM   #94 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

Code:
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)accel
{
	float newX = lightsabergreenImage.center.x + (accel.x * 22);
	if(newX > 30 && newX < 290) lightsabergreenImage.center = CGPointMake(newX, lightsabergreenImage.center.y);

	float x = [accel x];
	float y = -[accel y];
	float angle = atan(x);
	if([accel x] < 0.75 && [accel x] > 0.0);
	if([accel y] > 0.75 && [accel y] < 1.0);
		
	[lightsabergreenImage setTransform:CGAffineTransformMakeRotation(angle)];
	
	
	for(int i = 0; i<[laserArray count]; i++){
		UIImageView* redlaserImage = [laserArray objectAtIndex:i];
		
		if(CGRectIntersectsRect(redlaserImage.frame, lightsabergreenImage.frame)){
			
			UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Collision" message:@"the images collide" delegate:self cancelButtonTitle:@"dismiss" otherButtonTitles:nil];
			[alert show];
			[alert release];
		}
	}
Here's the code.
I put the collision in with the movement an the first collision gives an alert that keeps popping up even after I press dismiss.
I am not sure about how to do the deflections.
David Axe is offline   Reply With Quote
Old 01-23-2011, 01:12 PM   #95 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

You're using setTransform; everything I said above still applies.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-23-2011, 05:21 PM   #96 (permalink)
Registered Member
 
Join Date: Jan 2011
Posts: 11
Default

I've added a holder in IBOutlets that could be used to represent the saber as a line.

Could you advise me about the deflection, I know about the trig from other posts but can't seem to implement the equation.
http://www.iphonedevsdk.com/forum/ip...-ricochet.html

Once that is in place I should be able to write a method to tell which side of the saber a laser is to be deflected - left or right.

Thanks
David Axe 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: 238
22 members and 216 guests
ADY, Alsahir, beleg_1998, Dani77, diyora, FAED, fredidf, iDifferent, iph_s, JamesCahall, JasonR, mer10, prchn4christ, Robiwan, Rudy, smithdale87, Speed, spiderguy84, stekki, tgjorgoski, timle8n1, twerner
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,755
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

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