Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

Graves Robber
($1.99)

African Adventure
($0.99)

iTazer
($0.99)

ArtStudio
($3.99)

Pigs Vs Wolves
($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

View Poll Results: Was this thread helpful to other than me?? (LeopardDevX)
Yes, it helped me to!! 4 57.14%
No, it was a waste of space in the forum...... 3 42.86%
Voters: 7. You may not vote on this poll

Reply
 
LinkBack Thread Tools Display Modes
Old 03-27-2009, 06:48 PM   #26 (permalink)
iPhone Developer
 
Join Date: Mar 2009
Posts: 219
Exclamation Yes, Yes, Yes!!

OMG.. It works..

Thank you so, so, so much..
Cant believe i missed that line of code...

Still learning...
__________________
Thank You.

Last edited by LeopardDevX; 03-28-2009 at 04:07 AM.
LeopardDevX is offline   Reply With Quote
Old 03-27-2009, 07:06 PM   #27 (permalink)
iPhone Developer
 
Join Date: Mar 2009
Posts: 219
Exclamation Wait...

Wait...
How do i get every newEnemy collideable??

Almost solved...
__________________
Thank You.
LeopardDevX is offline   Reply With Quote
Old 03-28-2009, 02:05 AM   #28 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

I've been following this thread for a while, and I have a question. How would you only target once instance of the newEnemy? For instance if I wanted to just be able to touch one and delete that one.
.mb is offline   Reply With Quote
Old 03-28-2009, 05:51 PM   #29 (permalink)
iPhone Developer
 
Join Date: Mar 2009
Posts: 219
Exclamation Ok..

So i found out how to get the enemies collidable...

But when i speed up my enemy spawn timer... and move timer..
to make more enemies show up and go faster.. if i collide with them when there speeded up..
The app crashes...
Any ideas???

__________________
Thank You.
LeopardDevX is offline   Reply With Quote
Old 03-28-2009, 06:55 PM   #30 (permalink)
New Member
 
Join Date: Jan 2009
Location: Los Angeles
Posts: 13
Default

Quote:
Originally Posted by LeopardDevX View Post
Code:
Sorry.... dont have one...

Should i add one now??
Hi, I'm doing something similar. I'm trying to use CGRectIntersectsRect to determine when a Quartz animated object intersects with a CGRect object. Is this a bad thing to try to do? I've heard that Quartz stuff is on a different coord system. Do I need to apply a CGAffineTransformMakeRotation on initializing my Quarts objects to make the coord systems in sync?
patch is offline   Reply With Quote
Old 03-28-2009, 09:16 PM   #31 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,540
Default

Quote:
Originally Posted by LeopardDevX View Post
So i found out how to get the enemies collidable...

But when i speed up my enemy spawn timer... and move timer..
to make more enemies show up and go faster.. if i collide with them when there speeded up..
The app crashes...
Any ideas???

Nothing special about collision that should cause crashes. Is there an error message in the console?

Use NSLog messages or breakpoints and the debugger to figure out what line it's crashing on. That should give you a clue what the problem is. You should also run instruments to see if you're using too much memory- that will crash the app too.
__________________
smasher is offline   Reply With Quote
Old 03-28-2009, 09:27 PM   #32 (permalink)
iPhone Developer
 
Join Date: Mar 2009
Posts: 219
Arrow How??

Quote:
Originally Posted by smasher View Post
Nothing special about collision that should cause crashes. Is there an error message in the console?

Use NSLog messages or breakpoints and the debugger to figure out what line it's crashing on. That should give you a clue what the problem is. You should also run instruments to see if you're using too much memory- that will crash the app too.
How do i add those and where?
And.. i have checked the crash report and it says something like "image not found" or something....
What could this mean...?
__________________
Thank You.
LeopardDevX is offline   Reply With Quote
Old 03-29-2009, 01:20 PM   #33 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,540
Default

Quote:
Originally Posted by .mb View Post
I've been following this thread for a while, and I have a question. How would you only target once instance of the newEnemy? For instance if I wanted to just be able to touch one and delete that one.
Short question, long answer - to get the UIImageView to respond to touches, you'll have to subclass UIImageView. Inside your subclass, you'll want to do self.userInteractionEnabled=YES and implement the "touchesEnded:withEvent:" method from UIResponder.

You could also use UIButtons instead, which would let you define a button action - but long-term, subclassing UIImageView will allow you to add other variables you might need (like separate direction/velocity) to each enemy / ball / whatever.

EDIT: you don't have to subclass. Your viewController will get the touchesEnded:withEvent: message, and you could loop through the objects to see which one the touch intersects.
__________________

Last edited by smasher; 12-11-2009 at 01:42 AM. Reason: touchesEnded:withEvent:
smasher is offline   Reply With Quote
Old 03-29-2009, 03:14 PM   #34 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

Ahh, that makes sense. One more question, if I could. I'm trying to destroy objects dynamically, but I'm drawing a blank on how to remove it from memory... Trying to use [object dealloc] just freezes the application, and removing it from the array does the same.
.mb is offline   Reply With Quote
Old 03-29-2009, 03:53 PM   #35 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,540
Default

Quote:
Originally Posted by .mb View Post
Ahh, that makes sense. One more question, if I could. I'm trying to destroy objects dynamically, but I'm drawing a blank on how to remove it from memory... Trying to use [object dealloc] just freezes the application, and removing it from the array does the same.
Objects get destroyed when their "retain count" hits zero. Usually, that means you intit the object (for a count of one) and it gets destroyed when you release it. You never call dealloc yourself.

The confusion comes in when you *don't* init the objects - many classes have "convenience methods" that give you "autorelease" objects, that will release themselves at the end of the current event. If you release an object you did not init, you usually get a crash.

Properties and arrays/dictionaries also retain objects when you add them, and release them once when you remove them.

This is the page that put it all together for me:
Very simple rules for memory management in Cocoa

PS- views also retain their subviews. So you'll have to call [deadView removeFromSuperview] too, if it's a view.
__________________
smasher is offline   Reply With Quote
Old 03-29-2009, 07:15 PM   #36 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

Last post here, because I've sort of hijacked the thread, but here's my code.
Code:
@implementation SiegeViewController
@synthesize maxEnemies, newBall,origin,squaretest;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self touchesMoved:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	newBall.center = location;
}

- (void)spawn1sec
{
	UIImage *enemyImage = [UIImage imageNamed:@"Green_Square.png"];
	newBall = [[UIImageView alloc] initWithImage: enemyImage];
	
	//add to array
	[enemies addObject:newBall];
	
	//add to the view, so it gets displayed.
	[self.view addSubview: newBall];
	newBall.center = CGPointMake(1, 1);
	newBall.userInteractionEnabled = YES;
	
}
- (void)gameLoop
{
	for (newBall in enemies)
	{
		int newX = newBall.center.x;
		int newY = newBall.center.y +1;
		newBall.center = CGPointMake(newX, newY);
	}
}
This doesn't work. I want to move the newBall object to the touchpoint, but it doesn't work. I probably haven't implemented the subclass properly.

That link was useful though. I've given it a read, and I think I know how to fix my problem.
.mb is offline   Reply With Quote
Old 03-30-2009, 09:49 AM   #37 (permalink)
iPhone Developer
 
Join Date: Mar 2009
Posts: 219
Thumbs up Timer?

Quote:
Originally Posted by .mb View Post
Last post here, because I've sort of hijacked the thread, but here's my code.
Code:
@implementation SiegeViewController
@synthesize maxEnemies, newBall,origin,squaretest;
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	[self touchesMoved:touches withEvent:event];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	newBall.center = location;
}

- (void)spawn1sec
{
	UIImage *enemyImage = [UIImage imageNamed:@"Green_Square.png"];
	newBall = [[UIImageView alloc] initWithImage: enemyImage];
	
	//add to array
	[enemies addObject:newBall];
	
	//add to the view, so it gets displayed.
	[self.view addSubview: newBall];
	newBall.center = CGPointMake(1, 1);
	newBall.userInteractionEnabled = YES;
	
}
- (void)gameLoop
{
	for (newBall in enemies)
	{
		int newX = newBall.center.x;
		int newY = newBall.center.y +1;
		newBall.center = CGPointMake(newX, newY);
	}
}
This doesn't work. I want to move the newBall object to the touchpoint, but it doesn't work. I probably haven't implemented the subclass properly.

That link was useful though. I've given it a read, and I think I know how to fix my problem.

Do you have a timer to call your gameloop and spawn1sec.....
PS. You need that....


And do you set newY and newX when the app starts....
like:
Code:
-(void)awakeFromNib {
speedX = 5.0;
speedY = 5.0;
// it may be speedX = 5; and speedY = 5;.........
and you need to set your timer.....
gametimer = [NSTimer schedueledTimer...ect..ect...ect..
//Im shure you know how to set a timer with a target and stuff...

}
Or you can do...
Code:
-(void)awakeFromNib {
speedX = 5.0;
speedY = 5.0;
// it may be speedX = 5; and speedY = 5;.........
and you need to set your timer.....
gametimer = [NSTimer schedueledTimer...ect..ect...ect..
//Im shure you know how to set a timer with a target and stuff...

}


OOOOOOPS sorry didnt read your question properly...
Ill let it stay to help other people.....


But in your case.. You dont need a touchesBegan.....
You need an touchesEnded!!!!!
__________________
Thank You.

Last edited by LeopardDevX; 03-30-2009 at 09:53 AM. Reason: didnt read his question....
LeopardDevX is offline   Reply With Quote
Old 03-31-2009, 11:42 PM   #38 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

Agh. I feel stupid. It was working the whole time. The problem is that it was affecting the center of the object, which is used for spawning, not movement. So, how could I affect the already made object with my move to touch code?
.mb is offline   Reply With Quote
Old 04-01-2009, 01:36 AM   #39 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,540
Default

Quote:
Originally Posted by .mb View Post
Agh. I feel stupid. It was working the whole time. The problem is that it was affecting the center of the object, which is used for spawning, not movement. So, how could I affect the already made object with my move to touch code?
You want to touch an object an move it? Like dragging?

You can either set up the objects themselves to respond to touches, like I said above, or handle it all here in the SiegeViewController.

To handle it here in the SiegeViewController, you'll have to set up so:

(1)You have a pointer to store the currently selected object.
(2)on touchesBegan, you look through your array of objects and find the nearest one to the touch point (pythaorean theorem.) Make that the currently selected object.
(3)On touchesMoved, move the currently selected object to the touch point.
__________________
smasher is offline   Reply With Quote
Old 04-01-2009, 07:50 PM   #40 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	for(newBall in enemies)
	{
		newBall.center = CGPointMake(location.x,location.y);
	}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	for(newBall in enemies)
	{
		newBall.center = CGPointMake(location.x,location.y);
	}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	for(newBall in enemies)
	{
		newBall.center = CGPointMake(location.x,location.y);
	}
}
That's what I have now. It works, and it moves them to the touchpoint when released, but it moves all of them.

Last edited by .mb; 04-01-2009 at 07:52 PM.
.mb is offline   Reply With Quote
Old 04-02-2009, 10:44 AM   #41 (permalink)
iPhone Developer
 
Join Date: Mar 2009
Posts: 219
Arrow yes..

Quote:
Originally Posted by .mb View Post
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	for(newBall in enemies)
	{
		newBall.center = CGPointMake(location.x,location.y);
	}
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	for(newBall in enemies)
	{
		newBall.center = CGPointMake(location.x,location.y);
	}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	for(newBall in enemies)
	{
		newBall.center = CGPointMake(location.x,location.y);
	}
}
That's what I have now. It works, and it moves them to the touchpoint when released, but it moves all of them.

When your doing for(newBall in enemies)
all the newBalls on the screen moves.. i guess you spawn some enemies... and you spawn those with the name "newBall" then all they will move!!!
__________________
Thank You.
LeopardDevX is offline   Reply With Quote
Old 04-02-2009, 07:52 PM   #42 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

Yes, I know that. The problem is that I want to be able to create all these with the same name, for easy handling with collision and stuff, but I don't know how to target one specific instance of newBall.
.mb is offline   Reply With Quote
Old 04-02-2009, 08:08 PM   #43 (permalink)
Registered Member
 
Join Date: Jan 2009
Posts: 239
Default

Quote:
Originally Posted by LeopardDevX View Post
I have made a game where a ball is bouncing around and to not loose you move a paddle at the bottom of the screen,, and i have bricks to hit at the top of the screen.....
Based on your description it sounds like you may be making a Breakout clone. You may find this interesting:

Atari's Legal Team Attacking iPhone "Breakout" Clones | Touch Arcade

Hopefully it's not a deal breaker for you.
rpstro02 is offline   Reply With Quote
Old 04-03-2009, 12:06 PM   #44 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,540
Default

Quote:
Originally Posted by .mb View Post
Yes, I know that. The problem is that I want to be able to create all these with the same name, for easy handling with collision and stuff, but I don't know how to target one specific instance of newBall.
"for(newBall in enemies)" is a loop - it loops through all of the enemies, one at a time, and the code between the brackets changes their position.

You need to do something like this - loop through the enemies and find the one closest to where the touch began. That's your selected enemy.

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
        float foundDistance=1000;
	for(checkEnemy in enemies)
	{
		distanceToEnemy = //pythagorean theorem here (distance btwn two points)
		if (distanceToEnemy<foundDistance){
			selectedEnemy = checkEnemy;
			foundDistance = distanceToEnemy;
		}
	}
}
"selectedEnemy" should be declared in your .h file, so you can use it in the other two touch methods. The other two touch methods should just set selectedEnemy.center.

How does that sound?
__________________
smasher is offline   Reply With Quote
Old 04-03-2009, 02:27 PM   #45 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

So I would use the standard

a2+b2 = c2

So.... distanceToEnemy = touchpoint + object center?

I get what your saying, but I'm just not sure what to measure.
.mb is offline   Reply With Quote
Old 04-03-2009, 06:33 PM   #46 (permalink)
.mb
New Member
 
Join Date: Mar 2009
Posts: 32
Default

Success! I figured it out. I ended up subclassing the UIImageView and then finding the position like smasher recommended. Thanks a ton!
.mb is offline   Reply With Quote
Old 04-04-2009, 02:44 AM   #47 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 2,540
Default

Quote:
Originally Posted by rpstro02 View Post
Based on your description it sounds like you may be making a Breakout clone. You may find this interesting:

Atari's Legal Team Attacking iPhone "Breakout" Clones | Touch Arcade

Hopefully it's not a deal breaker for you.
It's not clear yet, but It sounds like this request was because of trademark violations- Atari owns the trademark for the name "Breakout." If you call your game "Super Paddle Whack," and you don't use their trade dress, you are not in violation.
__________________
smasher is offline   Reply With Quote
Old 12-11-2009, 12:35 AM   #48 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 125
Default

where to declare this
enemies = [[NSMutableArray alloc] init];

inside Spawn() or in move()??
rahul7star is offline   Reply With Quote
Old 12-11-2009, 04:15 AM   #49 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 125
Default

i am not much clear about collusion part
rahul7star is offline   Reply With Quote
Old 12-11-2009, 04:23 AM   #50 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 125
Default

i am doing this to check colslsion on touchBegan()

UITouch *toucha = [[event allTouches] anyObject];
CGPoint locationa = [toucha locationInView:toucha.view];




// rn d
if(CGRectContainsPoint(newEnemy.frame, locationa))
{

printf("HIT");





[newEnemy removeFromSuperview];
}
rahul7star 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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Stats
Members: 23,659
Threads: 38,415
Posts: 168,799
Top Poster: smasher (2,540)
Welcome to our newest member, suwimol
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 07:18 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0