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 Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-07-2010, 06:44 AM   #1 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Question Moving an Object With a Single Touch

I am currently making an image dodging game and when you click anywhere on the screen the movable image goes to there does anyone know how I can stop this from happening.
wilky94 is offline   Reply With Quote
Old 02-07-2010, 11:50 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

It might help if you post your image moving code.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 03:01 PM   #3 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
It might help if you post your image moving code.
My code:
Code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	
	
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	one.center = location;

[self Collision];

}
Code:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	[self touchesBegan:touches withEvent:event];

	
	
}
wilky94 is offline   Reply With Quote
Old 02-07-2010, 03:06 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Right, well you've programmed it to do so.
Code:
one.center = location;
That's telling "one" to go to where the screen was tapped.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 04:31 PM   #5 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
Right, well you've programmed it to do so.
Code:
one.center = location;
That's telling "one" to go to where the screen was tapped.
should I just delete that code?
wilky94 is offline   Reply With Quote
Old 02-07-2010, 04:33 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Deleting the code will mean "one" doesn't move at all. If that's what you want, then yes, delete that bit of code.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 04:34 PM   #7 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
Deleting the code will mean "one" doesn't move at all. If that's what you want, then yes, delete that bit of code.
I need one to move when the user drags it but not is a random part of the screen is clicked
wilky94 is offline   Reply With Quote
Old 02-07-2010, 04:36 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

I'd put the code in touchesMoved, but check if the touch is on "one" before moving it.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 04:40 PM   #9 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
I'd put the code in touchesMoved, but check if the touch is on "one" before moving it.
thank you for all your help it worked.
wilky94 is offline   Reply With Quote
Old 02-07-2010, 04:50 PM   #10 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

it works but when I click somewhere and drag the image moves to there do you know what I can do?
wilky94 is offline   Reply With Quote
Old 02-07-2010, 04:55 PM   #11 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Like I said in my last post, check that the touch is on the object before moving it.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 04:56 PM   #12 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
Like I said in my last post, check that the touch is on the object before moving it.
what do you mean by the touch is on the object.
wilky94 is offline   Reply With Quote
Old 02-07-2010, 04:58 PM   #13 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Has the person got their finger on the object?

Check the coordinates of the touch, then compare them with the object.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 05:00 PM   #14 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
Has the person got their finger on the object?

Check the coordinates of the touch, then compare them with the object.
where can I check the coordinates could you post some code?
wilky94 is offline   Reply With Quote
Old 02-07-2010, 05:04 PM   #15 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Check the coordinates in the touchesMoved event.

Code:
//Taken from what you posted
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
"location" has the coordinates of the touch, and if you're unsure of how to get the coordinates for "one", the documentation should have everything you need.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 05:08 PM   #16 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
Check the coordinates in the touchesMoved event.

Code:
//Taken from what you posted
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:touch.view];
"location" has the coordinates of the touch, and if you're unsure of how to get the coordinates for "one", the documentation should have everything you need.
do I put that code in the TouchesMoved section and what do I do with the code in the TouchesBegan?
wilky94 is offline   Reply With Quote
Old 02-07-2010, 05:12 PM   #17 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

I would put it in the touchesMoved function. From the looks of it, you don't need anything in touchesBegan.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 05:14 PM   #18 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
I would put it in the touchesMoved function. From the looks of it, you don't need anything in touchesBegan.
I now have this code but it still happens
Code:
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
	

	
	[self Collision];
	

}
Code:
- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
	
	
	UITouch *touch = [[event allTouches] anyObject];
	CGPoint location = [touch locationInView:touch.view];
	one.center = location;
	
	
	
	
	
}
wilky94 is offline   Reply With Quote
Old 02-07-2010, 05:15 PM   #19 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

That's because you're not checking to see if the touch is on the object.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 05:17 PM   #20 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
That's because you're not checking to see if the touch is on the object.
how can I check this, what should I search for in the documentation sorry I am very new to iPhone development.
wilky94 is offline   Reply With Quote
Old 02-07-2010, 05:25 PM   #21 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

Well, look up CGPoint, to see how you can get x values and y values for the touch. Then look up whatever your object is to see how you can get x values and y values for that.

Then use some simple if statements to compare them.
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 05:29 PM   #22 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
Well, look up CGPoint, to see how you can get x values and y values for the touch. Then look up whatever your object is to see how you can get x values and y values for that.

Then use some simple if statements to compare them.
Code:
	image.center = CGPointMake(image.center.x+pos.x, image.center.y+pos.y);
	if (image.center.x > 295 || image.center.x < 25)
		pos.x = -pos.x;
	if (image.center.y > 375 || image.center.y < 30)
		pos.y = -pos.y;
is this what you mean.
wilky94 is offline   Reply With Quote
Old 02-07-2010, 05:33 PM   #23 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

What are "pos" and "nana"?
harrytheshark is offline   Reply With Quote
Old 02-07-2010, 05:34 PM   #24 (permalink)
Registered Member
 
wilky94's Avatar
 
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Send a message via AIM to wilky94 Send a message via Skype™ to wilky94
Default

Quote:
Originally Posted by harrytheshark View Post
What are "pos" and "nana"?
the image is the images i have moving around and the pos is the CGPoint for them
wilky94 is offline   Reply With Quote
Old 02-07-2010, 05:36 PM   #25 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: UK
Posts: 1,886
Default

So you still need to compare where the touch is.
harrytheshark is offline   Reply With Quote
Reply

Bookmarks

Tags
dodging, game, image

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: 267
20 members and 247 guests
@sandris, AdamL, ADY, Dani77, diyora, FAED, F_Bryant, GHuebner, HDshot, headkaze, mer10, Oral B, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, twerner, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,748
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 12:56 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0