Hi I am trying to store the move points in the NSMutableArray so I have tries like this
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *move = [[event allTouches] anyObject];
CGPoint MovePoint = [move locationInView:self.view];
if (MovePointsArray==NULL) {
MovePointsArray=[[NSMutableArray alloc]init];
}
[MovePointsArray arrayWithObjects:[NSValue valueWithCGPoint:MovePoint]];
}
but this doesn't work how can i store these points in NSMutableArray
You should use addObject: in the last line:
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *move = [[event allTouches] anyObject];
CGPoint MovePoint = [move locationInView:self.view];
if (MovePointsArray==NULL) {
MovePointsArray=[[NSMutableArray alloc]init];
}
[MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]];
}
You should do like this :
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *move = [[event allTouches] anyObject];
CGPoint MovePoint = [move locationInView:self.view];
if (MovePointsArray == NULL) {
MovePointsArray = [[NSMutableArray arrayWithObjects:[NSValue valueWithCGPoint:MovePoint, nil];
}
else {
[MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]];
}
}
don't forget to retain / relase the array as you don't see to use a property accessor.
Best, you should alloc/init the array in your init method and then only do here :
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *move = [[event allTouches] anyObject];
CGPoint MovePoint = [move locationInView:self.view];
[MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]];
}
If you want to get an array using method arrayWithObjects you must also add nil as the last element of the array.
like so:
[MovePointsArray arrayWithObjects:[NSValue valueWithCGPoint:MovePoint], nil];
but to add an object to an existing array you should use addObject method
[MovePointsArray addObject:[NSValue valueWithCGPoint:MovePoint]];
Related
I have 20 sprites in my scene which I've added to a NSMutableArray. My problem is that when I drag one sprite over another, the other also moves. How do I restrict the movement of untouched sprites?
Please help me with code (I am new to Cocos2d).
if( (self=[super init])) {
collection=[[NSMutableArray alloc]init];
CCLayer *base=[CCSprite spriteWithFile:#"Base.png"];
base.position=ccp(512,384);
[self addChild:base];
x=0;
for(int i=1;i<=7;i++)
{
CCSprite *hole=[CCSprite spriteWithFile:#"ball.png"];
hole.position=ccp(140+x,318);
hole.tag=i;
[self addChild:hole];
hole.visible=YES;
[collection addObject:hole];
x=x+75;
}
self.isTouchEnabled=YES;
}
return self;
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(#"count:%i",[collection count]);
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
for(CCSprite *s in collection)
{
if(CGRectContainsPoint([s boundingBox], location))
s.position=ccp(location.x,location.y);
return;
}
}
You can do that:
Declare this in interface .h file
CCSprite *mSpriteOnHand;
CGPoint mLastPos;
Inside init assign it to nil.
mSpriteOnHand = nil;
In touchesBegan method check like this
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
for(CCSprite *s in collection)
{
if(CGRectContainsPoint([s boundingBox], location))
{
mLastPos = s.position;
s.position=ccp(location.x,location.y);
mSpriteOnHand = s;
break;
}
}
}
- (void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch=[touches anyObject];
CGPoint location=[touch locationInView:[touch view]];
location=[[CCDirector sharedDirector]convertToGL:location];
location=[self convertToNodeSpace:location];
if(mSpriteOnHand)
{
mSpriteOnHand.position = location;
}
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
if(mSpriteOnHand)
{
mSpriteOnHand.position = mLastPos;
mSpriteOnHand = nil;
}
}
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
I am using cocos2d to build a game. I have an array of CCSprites and I want to be able to touch them and delete the one that was touched.
Right now I have this...
-(void) spawn {
mySprite = [CCSprite spriteWithFile:#"image.png"];
mySprite.position = ccp(positionX,positionY);
[myArray addObject:mySprite];
[self addChild:mySprite];
}
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
NSUInteger i, count = [myArray count];
for (i = 0; i < count; i++) {
mySprite = (CCSprite *)[myArray objectAtIndex:i];
if (CGRectContainsPoint([mySprite boundingBox], location)) {
[self removeChild:mySprite cleanup:YES];
}
}
I have never done this before. Does anyone have a solution?
Thanks,
Michael
- (void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch* touch = [touches anyObject];
CGPoint location = [touch locationInView: [touch view]];
NSMutableArray *spritesToDelete = [[NSMutableArray alloc] init];
for(CCSprite* mySprite in myArray) {
if (CGRectContainsPoint([mySprite boundingBox], location))
[spritesToDelete addObject:mySprite];
for(CCSprite* deadSprite in spritesToDelete) {
[self removeChild:deadSprite cleanup:YES];
[myArray removeObject:deadSprite];
}
}
This code uses a for-each to create an array of the objects that meet your condition, and then removes them.
For various reasons, I've moved these methods from a UIView subclass to my viewcontroller. And I finally got it working, except for one thing. Not only am I able to drag the UIImageviews I've programmatically created, but the actual view controllers view is draggable too. Creating this much undesired effect. I guess it's the fact that it's touches anyobject, and the background itself is an object. I'm just not sure how exclude the background. I would think that it would need the "UserInteraction enabled", but I guess not? I only want it to make UIImageViews draggable. Please forgive my noobness. I'm still learning.
I have all the imageviews i'd want "touchable" in an NSMutableDictionary called "letterDictionary". Would it be possible to only have touch apply to what's in the dictionary?
http://imgur.com/W08dI
- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
UITouch *touch = [touches anyObject];
touchPoint = [touch locationInView:self.view];
movingLetter = [touch view];
CGPoint pointInside = [touch locationInView:[touch view]];
if ([movingLetter pointInside:pointInside withEvent:event]) touchedInside = YES;
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchedInside) {
UITouch *touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self.view]; // get the new touch location
movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);
touchPoint = newPoint;
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (touchedInside) {
UITouch *touch = [touches anyObject];
CGPoint newPoint = [touch locationInView:self.view];
movingLetter.center = CGPointMake(movingLetter.center.x + newPoint.x - touchPoint.x, movingLetter.center.y + newPoint.y - touchPoint.y);
if (CGRectIntersectsRect([movingLetter frame], [placeHolder frame]))
{
movingLetter.center = placeHolder.center;
}
}
touchedInside = NO;
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
touchedInside = NO;
}
You have the view that was touched,
UITouch *touch = [touches anyObject];
touchPoint = [touch locationInView:self.view];
movingLetter = [touch view];
just test to see if it is the class you are looking for (e.g. a UIImageView) then return
UITouch *touch = [touches anyObject];
if (![[touch view] isKindOfClass:[UIImageView class]])
{
return;
}
In this touchesBegen Method :
-(void)touchesBegan:(NSSet* )touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
UITouch *touch = [touches anyObject];
_previousPoint1 = [touch previousLocationInView:self.main_uiview];
_previousPoint2 = [touch previousLocationInView:self.main_uiview];
_currentPoint = [touch locationInView:self.main_uiview];
[self touchesMoved:touches withEvent:event];
self.bezierPath = [UIBezierPath bezierPath];
[self.bezierPath moveToPoint:_currentPoint];
}
TouchesMove Method
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent* )event
{
UITouch *touch = [touches anyObject];
_previousPoint2 = _previousPoint1;
_previousPoint1 = [touch previousLocationInView:self.main_uiview];
_currentPoint = [touch locationInView:self.main_uiview];
lastPoint = _currentPoint;
[_bezierPath addLineToPoint:lastPoint];
// calculate mid point
CGPoint mid1 = midPoint4(_previousPoint1, _previousPoint2);
CGPoint mid2 = midPoint4(_currentPoint, _previousPoint1);
UIGraphicsBeginImageContextWithOptions(self.bg_imageview.frame.size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context,brush);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineJoin(context,kCGLineJoinRound);
[self.bg_imageview.image drawInRect:CGRectMake(0, 0, self.bg_imageview.frame.size.width, self.bg_imageview.frame.size.height)];
CGContextMoveToPoint(context, mid1.x, mid1.y);
// Use QuadCurve is the key
CGContextAddQuadCurveToPoint(context, _previousPoint1.x, _previousPoint1.y, mid2.x, mid2.y);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetStrokeColorWithColor(context,[UIColor blackColor].CGColor);
CGContextSetLineWidth(context, 3.0);
CGContextStrokePath(context);
self.bg_imageview.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();}
I'm making a simple dial that rotates as you drag your finger across it. It rotates great, but it also rotates when i touch anywhere on the screen and drag my finger.
How can i restrict the first touches to be only inside my imageview object? or where am i going wrong?
this is my code of trouble:
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
UIImage *image1 = [UIImage imageNamed:#"nav#2x.png"];
wheelfrom = [[UIImageView alloc] initWithImage:image1];
wheelfrom.frame =CGRectMake(10, -130, 300, 300);
[self addSubview:wheelfrom];
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
firstLoc = [touch locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch =[[[event allTouches] allObjects] lastObject];
CGPoint curLoc = [touch locationInView:self];
float fromAngle = atan2( firstLoc.y-wheelfrom.center.y,
firstLoc.x-wheelfrom.center.x );
float toAngle = atan2( curLoc.y-wheelfrom.center.y,
curLoc.x-wheelfrom.center.x );
float newAngle = angle + (toAngle - fromAngle);
CGAffineTransform cgaRotate = CGAffineTransformMakeRotation(newAngle);
wheelfrom.transform = cgaRotate;
angle = newAngle;
}
Thanks for your help!
You try like this,
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
CGPoint location = [touch locationInView:self.view];
if(CGRectContainsPoint(wheelfrom.frame, location))
{
//do your things
}
}
You can try by checking if the point of touch is within the frame of the image view.Do what you want only if its yes.
Inside -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event, check the firstLoc is within your range.