views:

154

answers:

1

Hello, i am trying to build an iphone app by using cocos2d. and i would like to set an image to another fixed position from a fixed position by using touch as my wish(speedy, or slowly). i have got some code but it does not work properly.

so freinds it will more helpful to me if i get any solution.

A: 

The question is a little fuzzy, but if you want to set the position of a CocosNode you do:

[myNode setPosition:cpv(x,y)];

If you want the node to be offset from a touch location, you can do this by implementing ccTouchesBegan:withEvent

-(BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  CGPoint location = [touch locationInView: [touch view]];
  CGPoint convertedLocation = [[Director sharedDirector] convertCoordinate:location];

  [myNode setPosition: cpv(convertedLocation.x - 100, convertedLocation.y - 100)];
  return kEventHandled;
}

That will offset the CocosNode by -100,-100 to where the touch occurred.

The ccTouchesBegan:withEvent: should be implemented in your Layer, and isTouchesEnabled should be set to YES to enable touches.

David Higgins