02-07-2010, 06:44 AM
#1 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
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.
02-07-2010, 11:50 AM
#2 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
It might help if you post your image moving code.
02-07-2010, 03:01 PM
#3 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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];
}
02-07-2010, 03:06 PM
#4 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
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.
02-07-2010, 04:31 PM
#5 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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?
02-07-2010, 04:33 PM
#6 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
Deleting the code will mean "one" doesn't move at all. If that's what you want, then yes, delete that bit of code.
02-07-2010, 04:34 PM
#7 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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
02-07-2010, 04:36 PM
#8 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
I'd put the code in touchesMoved, but check if the touch is on "one" before moving it.
02-07-2010, 04:40 PM
#9 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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.
02-07-2010, 04:50 PM
#10 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
it works but when I click somewhere and drag the image moves to there do you know what I can do?
02-07-2010, 04:55 PM
#11 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
Like I said in my last post, check that the touch is on the object before moving it.
02-07-2010, 04:56 PM
#12 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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.
02-07-2010, 04:58 PM
#13 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
Has the person got their finger on the object?
Check the coordinates of the touch, then compare them with the object.
02-07-2010, 05:00 PM
#14 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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?
02-07-2010, 05:04 PM
#15 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
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.
02-07-2010, 05:08 PM
#16 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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?
02-07-2010, 05:12 PM
#17 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
I would put it in the touchesMoved function. From the looks of it, you don't need anything in touchesBegan.
02-07-2010, 05:14 PM
#18 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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;
}
02-07-2010, 05:15 PM
#19 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
That's because you're not checking to see if the touch is on the object.
02-07-2010, 05:17 PM
#20 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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.
02-07-2010, 05:25 PM
#21 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
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.
02-07-2010, 05:29 PM
#22 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
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.
02-07-2010, 05:33 PM
#23 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
What are "pos" and "nana"?
02-07-2010, 05:34 PM
#24 (permalink )
Registered Member
Join Date: Dec 2009
Location: Seaham UK
Age: 17
Posts: 257
Quote:
Originally Posted by
harrytheshark
What are "pos" and "nana"?
the image is the images i have moving around and the pos is the CGPoint for them
02-07-2010, 05:36 PM
#25 (permalink )
Registered Member
Join Date: Dec 2008
Location: UK
Posts: 1,886
So you still need to compare where the touch is.
Thread Tools
Display Modes
Linear Mode
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
» 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