How to get last touch in ccTouchesMoved? - objective-c

I'm working with cocos2d. How to get last touch in ccTouchesMoved?

Add a global Variable in your ccTouchesBegan,
CGPoint touchPoint = [touch locationInView:[touch view]];
startPosition = [[CCDirector sharedDirector] convertToGL:touchPoint];
in your ccTouchesMoved just use startPosition to check whether your hand moved

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[touches allObjects] objectAtIndex: 0];
// The last location you touch
CGPoint lastLocation = [touch previousLocationInView:self.view];
// Current location
CGPoint currentLocation = [touch locationInView:self.view];
}
You can do like that :)

Related

Ability to move more than one image with 'touches'

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
basketView.center = CGPointMake(point.x, point.y);
}
I currently have this code that works to allow the user to move an image around with their finger. However, if I wanted to add multiple moveable images to the screen (say I also wanted to be able to move UIImages basketView2 and basketView3), how would I alter the code to allow for this?
you just need to modify your code as below
UITouch *touch = [[event touchesForView:self.view] anyObject];
CGPoint point = [touch locationInView:self.view];
UIImageView *currentView=(UIImageView *)touch.view;
currentView.center = CGPointMake(point.x, point.y);

Drag multiple UIImageViews in an Array

This is what I have so far
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
for (UIImageView *imageView in _imageViewArray) {
CGPoint Location = [touch locationInView:touch.view];
imageView.center = Location;
}
}
The problem I am facing is when I move one image they all jump to the same place.
Thanks to cyberpawn this it what I did it get it to work
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint oldPoint = [touch previousLocationInView:touch.view];
CGPoint newPoint = [touch locationInView:touch.view];
CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);
for (UIImageView *imageView in _imageViewArray) {
if (CGRectContainsPoint(imageView.frame, newPoint)) {
CGPoint cntr = [imageView center];
[imageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)];
}
}
}
Thats because you are moving them all to same location, you need to calculate the difference between touch locations and add that displacement to all views. Below code should solve your problem! Forget touchesBegan and Just override touchesMoved method like that.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint oldPoint = [touch previousLocationInView:touch.view];
CGPoint newPoint = [touch locationInView:touch.view];
CGPoint diff = CGPointMake(newPoint.x - oldPoint.x, newPoint.y - oldPoint.y);
for (UIImageView *imageView in _imageViewArray) {
CGPoint cntr = [imageView center];
[imageView setCenter:CGPointMake(cntr.x + diff.x, cntr.y + diff.y)];
}
}
If You want to move them separately when any of them is clicked than use below code instead!
float oldX, oldY;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint pt = [touch locationInView:touch.view];
for (UIImageView *imageView in _imageViewArray) {
if(CGRectContainsPoint(imageView.frame, pt)) {
oldX = imageView.center.x - imageView.frame.origin.x - pt.x;
oldY = imageView.center.y - imageView.frame.origin.y - pt.y;
break;
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint pt = [touch locationInView:touch.view];
for (UIImageView *imageView in _imageViewArray) {
if (CGRectContainsPoint(imageView.frame, pt)) {
[self setCenter:CGPointMake(pt.x+oldX, pt.y+oldY)];
}
}
Enjoy Programming!
Here you coded like that only. If you want to move a single image, then you have to find that image and you should move that image alone.

Cocos2D touch-to-move-to-destination controls?

I have a spaceship. I want the spaceship to move to the area that the player has touched. So far I have this code:
- (void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
// initialize touch
UITouch *touch = [touches anyObject];
// point out location of touch
CGPoint location = [touch locationInView:[touch view]];
// move to location
[player runAction:[CCMoveTo actionWithDuration:0.5 position:location]];
// log action
CCLOG(#"player moved");
}
However, this has a quirk: When I touch in certain places the shuttle will move slow, up instead of down, and other weird movements. Is there any way to correct this code?
After the line :
CGPoint location = [touch locationInView:[touch view]];
Add this line:
location = [[CCDirector sharedDirector] convertToGL: location];
Then try. :)

Prevent view object from jumping when moving with finger touch

I've created a 200x200 circle in a custom UIView. I am using the following script to move the view object around the screen on the iPad.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if([touch view] == newShape)
{
newShape.center = currentPoint;
}
[self.view setNeedsDisplay];
}
Everything works fine, I can move the circle anywhere on the screen. However, if I don't touch dead center on the circle object, it jumps slightly. By reading the code, this is quite obvious because newShape.center is being set to wherever the touch happens and ultimately snapping quickly to that position.
I'm looking for a way to move an object without snapping to the touch position. I'm thinking I would use xy coordinates to achieve this, but I'm not sure how to implement it.
Thanks!
Declare CGPoint prevPos; in .h file.
Here my view is _rectView.
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
prevPos = currentPoint;
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
if([touch view] == _rectView)
{
float delX = currentPoint.x - prevPos.x;
float delY = currentPoint.y - prevPos.y;
CGPoint np = CGPointMake(_rectView.frame.origin.x+delX, _rectView.frame.origin.y+delY);
//_rect.center = np;
CGRect fr = _rectView.frame;
fr.origin = np;
_rectView.frame = fr;
}
//[self.view setNeedsDisplay];
prevPos = currentPoint;
}
Use the above code. You will not get that 'jump' effect.
An obvious way to do it is to store the offset of the touch from the shape's center in -touchesBegan:withEvent: and apply the offset in -touchesMoved:withEvent:.

Move a UIView only Horizontally

I have 2 UIViews (placardView, and PlacardView2) that I would like to constrain them when touched to move horizontally but not vertically using the touchesMoved Method. My thought was to make the Y co-ordinate equal to itself, but I cannot get that to work syntactically in Xcode as I am new to this sort of thing.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
// If the touch was in the placardView, move the placardView to its location
if ([touch view] == placardView) {
CGPoint location = [touch locationInView:self];
placardView.center = location;
placardView.center.y = placardView.center.y;//error is here
return;
}
if ([touch view] == placardView2) {
CGPoint location = [touch locationInView:self];
placardView2.center = location;
placardView2.center.y = placardView2.center.y;//error is here
return;
}
}
You won't change anything by assigning center.y to itself - plus it is already changed by placardView2.center = location; assignment. Instead, you should probably assign just the new x coordinate:
CGPoint placardCenter = placardView2.center;
placardCenter.x = location.x;
placardView2.center = placardCenter;