Touch is Currently Moving - Cocos2d - iphone

Is there a way to tell if the touch is currently moving? If so could someone lead me to the right place?

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
location = [touch locationInView:[touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
//Here urSprite is your CCSprite which you want to move
CGRect urSpriteRect = CGRectMake([urSprite position].x - [urSprite contentSize].width/2,
[urSprite position].y - [urSprite contentSize].height/2,
[urSprite contentSize].height, [urSprite contentSize].width);
if (CGRectContainsPoint(urSpriteRect, location))
{
checkFlag = TRUE;
NSLog(#"You Touched the sprite!");
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if(checkFlag){
urSprite.position = location;
NSLog(#"You are moving your sprite!");
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
checkFlag = FALSE;
NSLog(#"Sprite stopped!");
}

If your layer has touches enabled, you can implement the following method in your layer to handle move events for touches...
-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

Related

Multi object drag

While dragin multi objects using the following code the the object is shaking when i touch the object,
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if ([touch view] == img1)
{
CGPoint location = [touch locationInView:touch.view];
img1.center = location;
return;
}
if (touch.view == img2)
{
CGPoint location = [touch locationInView:touch.view];
img2.center = location;
return;
}
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[self touchesBegan:touches withEvent:event];
}
What is problem in my code, is there any other method to drag and if the view contain one more object?please help me to solve this issue
I think if you are talking about more object to drag all at once then you should remove the return from you touch begain method

image should follow previous point touches in iphone?

When touch began-it shows the image,when touchmoved the image follows the touch events, when touch ended the image disappear
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
newgicg.hidden = NO;
NSArray *allTouches = [touches allObjects];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
int count = [allTouches count];
if (count == 1) {
newgicg.center = touchLocation;
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
newgicg.center = touchLocation;
[self.view addSubview:newgicg];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
newgicg.hidden = YES;
}
I want something like ,to follow the collection of stars when the cursor moved on the screen.i want the stars to be blinks and vanished wherever the cursor goes on the screen
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
[super touchesBegan:touches withEvent:event];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesMoved:touches withEvent:event];
UITouch *touch = [touches anyObject];
CGPoint touchLocation = [touch locationInView:touch.view];
UIImageView *imageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"simple-apple.png"]];
imageView.center = touchLocation;
[self.view addSubview:imageView];
[imageView release];
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[super touchesEnded:touches withEvent:event];
for (UIView *view in [self.view subviews]) {
[view removeFromSuperview];
}
}
try this code instead, just replace the name of the image.
EDIT:
one thing I forgot to mention is that you can keep a counter to the no. of subviews added this way and remove them , otherwise it may also remove other views which were there on the view before.

Disable UIView on TouchesMoved

I have an application in which i have to move images, problem is arising when mouse touches the images it gets moved but if it touches the main View, whole view is also moving.
I tried view.userInteractionEnabled=NO;
but after one move whole view gets freeze.
I want my view to be static(not moving)
Help !!!
here is the code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
UIImageView *imgPimples = [[UIImageView alloc]init];
imgPimples = (UIImageView*)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimples];
imgPimples.center = touchLocation;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
Just check whether touched class is of kind UIImageView.
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
if ([[touch view] isKindOfClass:[UIImageView class]]) {
UIImageView *imgPimple;
imgPimple = (UIImageView *)[touch view];
CGPoint touchLocation = [touch locationInView:imgPimple];
imgPimple.center = touchLocation;
}
}
I think it will serve your purpose.
if ([touch.view isKindOfClass:[UIImageView class]]){
imgPimples.center = touchLocation;
}
assuming your parent view is not also a UIImageView.

Move a UIImageView in y axis

I am working on a app and i want to move a UIImage in the Y axis only, not in Xaxis, here's what i did till now
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
stretchPoint = [[touches anyObject]locationInView:self.view];
arrowImage.center = stretchPoint;
}
The above code is moving the arrowImage in both the axis,
stretchPoint is an instance of CGPoint, My app is in portrait mode, can anyone give me a basic idea to how to do this, as the arrowImage is a instance of UIImageView.
this should work fine:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.x = arrowImage.center.x;
arrowImage.center = currentPoint;
}
Subclass the UIImage and implement the following code
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:[self superview]];
CGPoint newCenter = CGPointMake(self.center.x, currentPoint.y);
self.center = newCenter;
}
I am not sure as have not tested,but you can implement something like this :
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
CGPoint currentPoint = [touch locationInView:self.view];
currentPoint.y = 20;
}
Something like:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
stretchPoint = [[touches anyObject] locationInView:self.view];
arrowImage.frame = CGPointMake(arrowImage.origin.x,
stretchPoint.y,
arrowImage.size.width,
arrowImage.size.height);
}

Converting beginTouches and endTouches to ccBeginTouch and ccEndTouch

I'm converting the game I'm working on from UIkit to coocos2d. Using UIKIT I would use the code below code to pass the touch evens to a method. What would be the equivalent in Cocos2d?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Save the position
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil];
}
}
// Saves the first position for reference when the user lets go.
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
beginTouchPoint = touchPoint;
}
// Handles the continuation of a touch.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]];
}
}
-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{ id sender;
int directionSwiped;
int row,column;
CGFloat xDelta = position.x - beginTouchPoint.x;
CGFloat yDelta = position.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
What would be the equivalent using cocos2d?
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// Save the position
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchFirstTouchAtPoint:[touch locationInView:boardView] forEvent:nil];
}
}
// Saves the first position for reference when the user lets go.
- (void) dispatchFirstTouchAtPoint:(CGPoint)touchPoint forEvent:(UIEvent *)event
{
beginTouchPoint = touchPoint;
}
// Handles the continuation of a touch.
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
for (UITouch *touch in touches) {
// Send to the dispatch method, which will make sure the appropriate subview is acted upon
[self dispatchTouchEndEvent:[touch view] toPosition:[touch locationInView:boardView]];
}
}
-(void) dispatchTouchEndEvent:(UIView *)theView toPosition:(CGPoint)position
{ id sender;
int directionSwiped;
int row,column;
CGFloat xDelta = position.x - beginTouchPoint.x;
CGFloat yDelta = position.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}
I've tried to figure this out on my own, and I've spent hours on google, but I haven't come up with a workable solution.
I figured it out. I discovered that, what I was forgetting to do, was to add:
self.isTouchEnabled = YES;
to the Layer's init method.
After I did that the following code worked for me (beginTouchPoint and endTouchPoint are properties of the class):
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView: [myTouch view]];
beginTouchPoint = [[CCDirector sharedDirector]convertToGL:location];
}
// Handles the continuation of a touch.*
-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
static BOOL isFirstTouch = YES;
UITouch* myTouch = [touches anyObject];
int row,column;
int directionSwiped;
CGPoint location = [myTouch locationInView: [myTouch view]];
endTouchPoint = [[CCDirector sharedDirector]convertToGL:location];
CGFloat xDelta = endTouchPoint.x - beginTouchPoint.x;
CGFloat yDelta = endTouchPoint.y - beginTouchPoint.y;
[self findSwipeDirectionWith: xDelta and: yDelta];
}