0

I am having issue with collision behavior.I have 2 types of objects falling from up to bottom of the screen and collisioning with image in the bottom of the screen. Collision are working great, but soon as I move the image, it gets rescaled and blinks very often.Thanks for advice.

Code for moving the image.

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    //CGPoint svpt = [[touches anyObject] locationInView:self.view];
    CGPoint pt = [[touches anyObject] locationInView:self.playerMove];

CGRect frame = [self.playerMove frame];
frame.origin.x += (pt.x - self.startLocation.x);
frame.origin.y += (pt.y - self.startLocation.y);
frame.origin.x = MIN(MAX(frame.origin.x, -10), 240);
frame.origin.y = MIN(MAX(frame.origin.y, 430), 430);

self.playerMove.frame = frame;
[_animator updateItemUsingCurrentState:self.playerMove];

}

Code for collision.

_collision = [[UICollisionBehavior alloc]initWithItems:@[self.hammerImage,self.playerMove]];
[_animator addBehavior:_collision];
Jozef Vrana
  • 309
  • 1
  • 5
  • 14
  • when you change the frame on movement does the collision code get access to the new coordinates/frame? you don't put the code in for uicollisionbehavior but i can see it grabbing frame area at a single point and not updating potentially. – LanternMike Nov 24 '13 at 17:34

2 Answers2

1

Although I don't know where the exact problem is, I will tell you this: UIKitDynamics works by changing the view's frame and transform. So, the problem is probably that you are messing with transform by explicitly modifying frame. Try using CGAffineTransformTranslate instead.

Roshan
  • 1,937
  • 1
  • 13
  • 25
  • Can you please tell me how to you use it....I just need to move the image in the certain area of the screen...thanks a lot – Jozef Vrana Nov 24 '13 at 20:33
  • `self.playerMove.transform = CGAffineTransformTranslate(self.playerMove.transform, dx, dy)` – Roshan Nov 25 '13 at 00:13
1

If you want to move an object that has been added to an animator, I think you want to add an attachment behavior to playerMove, and drag the anchor point in your touchesMoved method. So first, create the attachment behavior, and add it to the animator:

    self.ab = [[UIAttachmentBehavior alloc] initWithItem:self.playerMove attachedToAnchor:self.playerMove.center];
    [_animator addBehavior:self.ab];

Then in your touchesBegan and touchesMoves, do something like this:

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    CGPoint pt = [[touches anyObject] locationInView:self];
    self.currentLocation = pt;
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        CGPoint pt = [[touches anyObject] locationInView:self];
        self.ab.anchorPoint = CGPointMake(self.ab.anchorPoint.x + (pt.x - self.currentLocation.x), self.ab.anchorPoint.y + (pt.y - self.currentLocation.y));
        self.currentLocation = pt;
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218